views:

1554

answers:

3

I need to create an interval Timer that is set to run once a week automatically. I don't want it to start based on user input, but I want it to be created when the application is deployed to the server. Every example that I have seen has another class starting the timer. I don't want to use a message driven bean to create the timer because the audit should just query a database for a given time period and is not based off of actions which send messages.

I have included an example of a Timer. In the example below, the timer should fire every 10 minutes. As a test I want the timer to fire every 10 minutes so I can test the timer.

@Stateless
public class TimerTest implements
     TimerTestLocal, TimerTestRemote{

    @Resource 
    private TimerService timerService;
    private Logger log = Logger.getLogger(TimerTest.class);
    private long interval = 1000 * 60 * 10;
    private static String TIMER_NAME = "AuditTimer";

    public void scheduleTimer() throws NamingException {
     // TODO Auto-generated method stub
     Calendar cal = Calendar.getInstance();
     //cal.set(Calendar.HOUR_OF_DAY, 23);//run at 11pm
     //cal.set(Calendar.MINUTE, 00);
     //cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
     SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
     log.debug("schedule for: " + sdf.format(cal.getTime()));

     timerService.createTimer(cal.getTime(), interval, TIMER_NAME);
    }

    public void cancelTimer() {
     for(Object obj : timerService.getTimers())
     {
      Timer timer = (Timer)obj;
      if(timer.getInfo().equals(TIMER_NAME))
       timer.cancel();
     }
    }

    @Timeout
    public void timerEvent(Timer timer) {
     log.debug("timer fired");
    }


}

So is there any way that I can start this timer when the application is deployed? I don't think it's a good idea to put the creation of the Timer in a @PostConstruct method because of class loaders on in the server.

+5  A: 

The way that I've done timers in the past is to create a context listener in the web.xml to configure the timer.

That way you can ensure that it is started with the container, and shut down cleanly when the app is taken down.

Jesse
That worked great. Thanks.
scheibk
A: 

But what if your application like mine does not have a front end and i need to automate invoking an ejb method call periodically?

shane lee