I have 4 methods in my Timersession bean,lets say a()
, b()
, c()
and d()
.
a()
should be executed every 6 hourb()
should be execute every 3 hourc()
should be execute every 1 hour
How can I do this by using EJB 3.0 timer service?
I have 4 methods in my Timersession bean,lets say a()
, b()
, c()
and d()
.
a()
should be executed every 6 hour b()
should be execute every 3 hour c()
should be execute every 1 hourHow can I do this by using EJB 3.0 timer service?
Schedule three separate timers, and use the "info" object to encode which method needs to be called from the @Timeout method. For example:
timerService.createTimer(..., 6 * 60 * 60 * 1000, "a");
...
timerService.createTimer(..., 3 * 60 * 60 * 1000, "b");
...
timerService.createTimer(..., 1 * 60 * 60 * 1000, "c");
...
@Timeout
private void timeout(Timer timer) {
String info = timer.getInfo();
if ("a".equals(info)) {
a();
} else if ("b".equals(info)) {
b();
} else if ("c".equals(info)) {
c();
} else {
throw new IllegalStateException("Unknown method: " + info);
}
}
Definitely, you can create multiple timers & can handle it in a single method.
Sample code :
//--
@Schedules ({
@Schedule(hour="*/1"),
@Schedule(hour="*/3"),
@Schedule(hour="*/6")
})
public void timeOutHandler(){
if(currentHr % 1 == 0) //-- Check for hourly timeout
a();
else if(currentHr % 3 == 0) //-- Similarly
b();
else if(currentHr % 6 == 0) //-- Similarly
c();
}
//--