Adam Bien's Weblog

Monday Feb 22, 2010

Simplest Possible EJB 3.1 Timer - Configured Programmatically

@Singleton @Startup public class PeriodicTimer { @Resource TimerService timerService; @PostConstruct public void initialize(){ ScheduleExpression expression = new ScheduleExpression(); expression.second("*/1").minute("*").hour("*"); timerService.createCalendarTimer(expression); } @Timeout public void execute(){ System.out.println("----Invoked: " + System.currentTimeMillis()); } }

 

A timer doesn't have to be a singleton - it can be a @Stateless and even a @Stateful bean. The method execute() will be invoked every second after the deployment. The programmatic registration is only an option. You could also use annotations for the timer configuration.

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"] 



*NEW* Workshop: "Real World Java EE 6/7 Bootstrap" and book: Real World Java EE Night Hacks--Dissecting the Business Tier

Comments:

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?

Posted by Ryan de Laplante on 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...).

Posted by Lieven Poelman on 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

Posted by Petr Klíma on February 26, 2010 at 09:48 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Meta
My Recent Book
Java One 2009/2011
...the last 150 posts
...the last 10 comments
Links
License