Adam Bien's Weblog
Simplest Possible EJB 3.1 Timer - Configured Programmatically
How to compile:
You will need the EJB 3.1 API in the classpath (few kilobytes).
How to deploy:
Just JAR or WAR the interceptor with an EJB and put the archive into e.g: [glassfishv3]\glassfish\domains\domain1\autodeploy
Btw. the initial deployment of the entire WAR took on my machine:
INFO: Loading application ProgrammaticallyCreatedTimer at /ProgrammaticallyCreatedTimer
INFO: ProgrammaticallyCreatedTimer was successfully deployed in 316 milliseconds..
How to use:
Another service can be easily injected to the timer and so invoked periodically:
@Stateless
public class HelloService {
public String sayHello(){
return "Hello from control: " + System.currentTimeMillis();
}
}
And: there is no XML, strange configuration, libraries, additional dependencies needed...You will find the whole executable project (tested with Netbeans 6.8 and Glassfish v3) in: http://kenai.com/projects/javaee-patterns/ [project name: SimpleTimer].
[See also "Real World Java EE Patterns - Rethinking Best Practices"]
Posted at 09:00AM Feb 22, 2010 by Adam Bien in General | Kommentare[3]
[my tweets]
Rss My book: Real World Java EE - Rethinking Best Practices


Thanks for this post. I just looked at the JavaDocs and see that TimerService's create methods can take a TimerInfo object, which can hold a Serializable object that holds contextual information. Some of the create methods can take a Serializable object directly. This is what I was looking for. Thanks
Does anyone with Quartz experience know how it compares with Java EE 6's TimerService in a non-clustered environment, and a clustered environment?
Gesendet von Ryan de Laplante am February 22, 2010 at 11:33 AM CET #
Ryan, normally the scheduler will be kept consistent among the cluster. So every event should only be fired once, not once for every node in the cluster.
I doubt you can fully compare Quartz as a timer service to the JEE6 scheduler, since Quartz is in a league of it's own(more trigger options, calendar options etc etc...).
Gesendet von Lieven Poelman am February 23, 2010 at 02:18 PM CET #
I think souce codes do not fully coresponds each other ...
I can not see how can be HelloService injected to the timer
Gesendet von Petr Klíma am February 26, 2010 at 09:48 PM CET #