2

I've a requirement which requires 3-6 scheduled task to run at a given time of the day. I am completely new to EJB timers, but have read that EJB timers is the best way to handle scheduled task in a Java EE container.

Design Question:

Let's say I need 10 scheduled tasks. I don't want to have, if possible, 10 EJB timers created. Instead I would like to have a one off EJB timer created and then reuse this for creating as much scheduled jobs as requried, passing the scheduled time to run (as aruguements) for each instance, to is this possible? Can someone please help with a skeleton code on this please?

N.B I am thinking of using non-persistent EJB timers ...

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
stack
  • 115
  • 2
  • 6
  • 19

3 Answers3

9

Another option (in addition to alreay said) is to use singleton with @Schedule annotation for each of your timed methods:

@Singleton
@Startup
public class TimedTaskManager {

  @Schedule(second = "0", minute = "*/5", hour = "*")
  public void runTask1() {
    //
  }

  @Schedule(second = "15", minute = "*/5", hour = "6,7,8")
  public void runTask2() {
    //
  }

  //
  //

  @Schedule(second = "0", minute = "*", hour = "1,2,6")
  public void runTaskN() {
    //
  }
}
andbi
  • 4,426
  • 5
  • 45
  • 70
2

You could define a timer in one of your stateless/message driven bean business methods (you'd still have to call it, though, it's not possible to create a timer that would start off on its own). Then, in the @Timeout method you could recreate the timer based on any logic you find suitable, i.e.

@Stateless
public SomeEJB ... {

     @Resource
     private TimerService timerService;

     public void businessMethod() {
         timerService.createTimer(...);
     }

     @Timeout
     public void timeout(Timer timer) {
         // do some timer-related logic, recreate the timer,
         // perhaps with new duration
         timerService.createTimer(...);
     }
}

This example is EJB 3.0-compatible.

MaDa
  • 10,511
  • 9
  • 46
  • 84
1

AFAIK it isn't possible to create one 'reusable' timer in an EJB beacuse you have to tell each timer which method should be invoked.

Have a look at this:

The 3rd party library Quartz Scheduler should be capable of creating Timer-objects programmatically. Maybe its worth to have a look at this!

Hope this helped, have Fun!

Community
  • 1
  • 1
SimonSez
  • 7,399
  • 1
  • 30
  • 35
  • Thanks for the response guys, SimonSez had a look at Quatz, which looks promising, however, would using Quartz cause unmanaged threads in A j2EE container ? I would like to use this with Websphere, is there a way of using Quartz in WAS and not having the problems with ummanaged threads? – stack Mar 20 '12 at 14:02
  • I've monitored the Quartz threads in my environment (Glassfish 3.1.1) at the beginning and couldn't find any unnessacery/unmanaged threads but I stumbled upon an old bug-report causing this (especially in combination with spring). Seemes to bie fixed since 2.0.2. Cheers! – SimonSez Mar 20 '12 at 14:36