questions on ejb3.0 timer service

泄露秘密 提交于 2019-12-13 05:41:06

问题


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 hour

How can I do this by using EJB 3.0 timer service?


回答1:


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();
    }

//--



回答2:


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);
  }
}


来源:https://stackoverflow.com/questions/5856775/questions-on-ejb3-0-timer-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!