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


[my tweets]  Rss My book: Real World Java EE - Rethinking Best Practices

Kommentare:

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 #

Senden Sie einen Kommentar:
  • HTML Syntax: Ausgeschaltet
Meta-stuff / Interviews
My Recent Book
Java One 2009
CommunityOne East N.Y.C
JavaONE 2008 Interview
Search
...the last 150 posts
...the last 10 comments
greenfire.dev.java.net
Links
my.netbeans.org
Visitors
License