Adam Bien's Weblog
Simplest Possible EJB 3.1 Timer
@Singleton
public class TimerService {
@EJB
HelloService helloService;
@Schedule(second="*/1", minute="*",hour="*", persistent=false)
public void doWork(){
System.out.println("timer: " + helloService.sayHello());
}
}
A timer doesn't have to be a singleton - it can be a @Stateless and even a @Stateful bean. The method doWork() will be invoked every second. There is no registration or configuration needed.
How to compile:
You will need the EJB 3.1 API in the classpath, or at least the @Singleton, @Schedule and @EJB annotation.
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 SimpleTimer at /SimpleTimer
INFO: SimpleTimer was successfully deployed in 363 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 10:03AM Feb 09, 2010 by Adam Bien in Real World Java EE Patterns - Rethinking Best Practices | Kommentare[6]
[my tweets]
Rss My book: Real World Java EE - Rethinking Best Practices


Can you please show the Java API for managing timers? In every scenario I can think of, I would need a Java API, not annotations. For example, a user designs an ad-hoc report and wants to have it emailed to three people every Friday at 5:00 PM. Should I have to recompile my source code for this?
Gesendet von Ryan de Laplante am February 09, 2010 at 03:52 PM CET #
@Ryan,
o.k. then I will write a post especially for you :-). The API was always available, you had to use that. Annotation are new in Java EE 6. With EJB 3.1 comes also a fluent-API. Will write a post about that,
thanks for your feedback!,
adam
Gesendet von Adam Bien am February 09, 2010 at 04:24 PM CET #
Hi Adam,
I'd be very interested too in examples on how to use the new timer API dynamically inside the code at runtime!
Moreover I'd like to hear your experience regarding performance and capability of the timer API in JEE 6. I'm thinking about a plan to port a standalone application to JEE 6. The app in question currently uses the Quartz framework to handle jobs with all sorts of scheduling plans. Do you think this could lead to performance problems for the scheduler in JEE 6 to handle a few hundred jobs (just the scheduling, not the work the jobs do)? Maybe Glassfish v3 is already using Quartz under the hood?!?
I've already posted this question some time ago in your Kenai forum but unfortunately didn't get an answer yet.
Thanks in advance!
Marco
Gesendet von Marco Ehrentreich am February 10, 2010 at 03:37 PM CET #
Hi Adam
Have you configured persistent timer for clustered environment? It is very common requirement to execute background job only on single cluster node.
It looks very easy, just set up persistent=true attribute for @Schedule. But I'm wondering how this feature is provided by AS in a portable way. For clustered version of Quartz you have to configure datasource (connection and schema) for jobs store.
I'm really interested in timer support for clustered environment.
Thanks in advance,
Marcin
Gesendet von Marcin am February 16, 2010 at 10:23 AM CET #
Can the timer values be injected so that we don't have hard-coded values in the class?
Gesendet von jkilgrow am February 23, 2010 at 05:32 PM CET #
Hi Adam,
Timers CANNOT be created for stateful session beans.
The spec says so. I am wondering why you said they can : "A timer doesn't have to be a singleton - it can be a @Stateless and even a @Stateful bean. "
Did i miss something ?
Gesendet von Celinio Fernandes am March 11, 2010 at 01:20 PM CET #