2012-06-02 31 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 –