@Schedule expression rules in EJB 3.1 – wonders never cease

It’s a new feature of EJB 3.1 to use calendar-based EJB Timer expressions in the @Schedule annotation as well as to be able to define automatically created EJB timers. What came as quite a surprise to me was to find out that the defaults for @Schedule cause a timer goes off at midnight when no second, minute or hour are specified. They simply default to 0 – see the 18.2.1.2 Expression Rules in the EJB 3.1 specification.

With that said, guess what

@Schedule(second = "*/5", info="Every 5 seconds")

does? If you think, it’d annotate a method to go off every 5 seconds from the very moment the bean gets deployed, you’re totally off beam. See the following message of GlassFish 3.1 b40 when the bean was deployed onto it.

Feb 6, 2011 6:14:57 PM com.sun.ejb.containers.EJBTimerService createTimer
FINE: @@@ Created timer [1@@1297012497844@@server@@gfembed2529505349261645651tmp] with the first expiration set to: Mon Feb 07 00:00:00 CET 2011

Note the date in the first line – Feb 6, 2011 6:14:57 PM – and the date in the second – Mon Feb 07 00:00:00 CET 2011.

The above @Schedule annotation is expanded to @Schedule(second = “0,5,10,15,20,25,30,35,40,45,50,55”, minute = “0”, hour = “0”, info=”Every 5 seconds”) and the very first time when the timer goes off is…the midnight the next day.

To fire a method every 5 seconds from now on use @Schedule(second = “*/5”, minute = “*”, hour = “*”, info=”Every 5 seconds”) – note the wild card, the asterisks. Another lesson learnt.

While working on it, I found Changing Log Levels in Embedded GlassFish very informative and helpful. Have it around while dealing with GlassFish logging.

Be Sociable, Share!
This entry was posted in Java EE.

Leave a Reply

%d bloggers like this: