2012-06-02 30 views
2

我想每两秒唤醒一次ejb计时器。对于我创造ScheulerExpression如下面的代码...EJB @Timeout不能正常工作

package com.fks.nclp.ejb.timer.service; 

import javax.annotation.PostConstruct; 
import javax.annotation.Resource; 
import javax.ejb.LocalBean; 
import javax.ejb.ScheduleExpression; 
import javax.ejb.Startup; 
import javax.ejb.Timeout; 
import javax.ejb.TimerConfig; 
import javax.ejb.TimerService; 

import com.sun.jersey.spi.resource.Singleton; 


@LocalBean 
@Singleton 
@Startup 
public class HelloWorldService { 


    @Resource 
    TimerService timerService; 

    @PostConstruct 
    public void initialize(){ 
      System.out.println("EJB PRE CONSTRUCT"); 
      ScheduleExpression expression = new ScheduleExpression(); 

      //SET TIMER TO 2 SEC    
      expression.hour("*"); 
      expression.minute("*"); 
      expression.second("*/2"); 

      timerService.createCalendarTimer(expression,new TimerConfig("TimerScheduler",false)); 


    } 


    @Timeout 
    public void startTimer(){ 
     System.out.println("THIS IS EJB TIMER PROGRAM"); 
    } 

} 

但是...... EJB容器不是每两秒钟后醒来的调度。

对此有何建议? 我怎样才能让表达每两秒醒来。

谢谢, Gunjan。

+0

您是否获取了正在调用的方法的任何日志/异常,或者根本没有被创建。 –

+0

ScheduleExpression看起来很合适。 Timer.getNextTimeout()返回什么(通过createCalendarTimer的返回值)?你在使用什么应用程序服务器?你的标签说ejb-3.0,但ScheduleExpression是EJB 3.1;你的应用服务器版本是否支持EJB 3.1? –

+0

我已经使用GlassFish 3.1并且很抱歉...我已经添加了GlassFish服务器库,它默认提供了EJB3.1 –

回答

3

有两件事可能是造成问题:

  1. import语句似乎不正确,这是com.sun.jersey.spi.resource.Singleton,但应该是javax.ejb.Singleton

  2. 尝试删除@LocalBean注释。

+0

#1一个几乎肯定是问题,特别是因为OP澄清说'@ PostConstruct'不是'完全被称为。 #2是多余的,但无害。 –

+0

亲爱的@Nayan Wadekar,我遇到了一个问题:当我使用'@ Timeout'时,即使我使用'@ Startup',我也不能在启动时加载调度程序。请看看:http://stackoverflow.com/questions/42242037/parameterize-ejb-scheduler-with-schedule-expression –