3

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
   }
 }
Jorge Iten Jr
  • 366
  • 6
  • 21
  • 1
    The timers should run independently using distinct stateless session bean instances. If you add logging to the timeout methods, do you see that both timeout callback methods are starting but finishing sequentially, or does one not even start until the other completes? Are you using any synchronized methods or locks? Are both timeout methods operating on the same DB and blocked on DB locks? Have you configured the stateless bean pool size to only allow a single bean instance? – Brett Kail Jul 15 '13 at 18:59
  • I started to check your questions and I found a specific configuration in Websphere for this. Thanks. – Jorge Iten Jr Jul 16 '13 at 19:36
  • Ah, I completely forgot that setting. Good find. – Brett Kail Jul 16 '13 at 23:02
  • From debugging my own code in JBoss7.2 I found that each bean has its own thread for executing timers, but all of a bean's timeouts will execute in the same thread (this appears so even when the bean is a Stateless non-singleton bean). My bean had 2 separate timers and threw some concurrency exception that it couldn't execute the timeout while another was already running. I was able to allow mine to run in parallel by splitting it up into 2 beans (which I should have done anyway). No idea if websphere behaves the same or not. – Shadow Man Dec 18 '13 at 19:16

2 Answers2

3

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:WAS Config

Jorge Iten Jr
  • 366
  • 6
  • 21
1

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.

Gabriel Aramburu
  • 2,951
  • 2
  • 16
  • 20
  • 1
    First, It was just a example, for my needs (create a schedule for specific week days running in a alternate hours) createCalendarTimer is the most suitable. – Jorge Iten Jr Jul 16 '13 at 19:26
  • 1
    Second, I dont get your point, my code is not transgressing what you wrote. Thanks. – Jorge Iten Jr Jul 16 '13 at 19:35