I have an application working with TimerService, I am creating a few timers to run specific tasks. It is working fine. However, I am noticing some delay in the timeouts. I have a timeout "A" scheduled to run every 10 minutes and another "B" to run every 3 minutes. If "A" takes 5 minutes to run, "B" only will run after "A" ends, causing a 2 minutes delay.It is a problem, because the things are not ready when it supposed to be. My question is, if there is a way to TimerService run simultaneously. The pieces of code that I'm using is below. I appreciate any help.
Schedule creation:
// Every schedule extend from this class.
public abstract class Schedule {
@Resource
private TimerService timerService;
public void start() {
TimerConfig timerConfig = new TimerConfig();
timerConfig.setInfo(name);
timerConfig.setPersistent(false);
timerService
.createCalendarTimer(this.calendarSchedule, timerConfig);
}
}
Schedule Implementation:
@Named
@Stateless
public class MyScheduleEJB extends Schedule {
@Timeout
public void timeout(Timer timer) {
// do the work
}
}
It is a very specific situation in a Application Server. But if someone else have the same problem, there is a configuration in Websphere (not sure about others AS) that tells how many threads the timer pools should use. Default is only 1, that's why it was not running simultaneously. The configuration:
I see two problems here:
First, Why are you using createCalendarTimer method? I think a most suitable TimerService's method for your requirements (to execute every 5 or 10 second) is createIntervalTimer.
Second, I think the way you are implementing your EJB Timer doesn't follow the EJB specification.
He says:
18.2Bean Provider's View of the Timer Service
The bean class of an enterprise bean that uses the timer service must provide one or more timeout callback methods.
18.3Bean Provider's Responsibilities
An enterprise bean that is to be registered with the Timer Service must have a timeout callback method.The enterprise bean class may have superclasses and/or superinterfaces. If the bean class has superclasses, the timeout method may be defined in the bean class, or in any of its superclasses.
As you can see there are some limitations about how to implements an EJB Timer. Probably the specification has several interpretations but after read this I would not hesitate to change my code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With