2015-06-17 100 views
1

我創建了一個包含EJB的測試應用程序,該應用程序應該每秒打印一次當前的Timestamp。現在,在我的wild dep上部署Web應用程序後,我每秒鐘都會收到5條日誌消息...看起來像運行多個調度程序,這將觸發輸出...Wildfly Scheduled EJB-多次執行

您有任何想法,要解決這個問題?如果我只用註釋的@Schedule梅索德,它打印每一秒的結果,而是由定時編程,使問題:

@Singleton 
@Startup 
public class Scheduler { 

    @Resource 
    TimerService timerService; 

    @Inject 
    Logger logger; 

    @Timeout 
    public void doStuff() { 
     logger.info("Programmatic Scheduler: " + System.currentTimeMillis()); 
    } 

    @PostConstruct 
    public void startScheduler() { 

     TimerConfig timerConfig = new TimerConfig(); 
     timerConfig.setPersistent(false); 

     ScheduleExpression expression = new ScheduleExpression(); 
     expression.second("*/1").minute("*").hour("*"); 
     timerService.createCalendarTimer(expression, timerConfig); 
    } 
} 

...和here's您參見輸出每秒打印多份:

16:26:46,574 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 10) Programmatic Scheduler: 143455120657 
    16:26:46,849 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 8) Programmatic Scheduler: 1434551206849 
    16:26:46,869 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 7) Programmatic Scheduler: 1434551206869 
    16:26:46,890 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 4) Programmatic Scheduler: 1434551206890 
    16:26:47,083 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 3) Programmatic Scheduler: 1434551207083 
    16:26:47,145 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 1) Programmatic Scheduler: 1434551207145 
    16:26:47,185 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 9) Programmatic Scheduler: 1434551207185 
    16:26:47,246 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 6) Programmatic Scheduler: 1434551207246 
    16:26:47,274 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 2) Programmatic Scheduler: 1434551207274 
    16:26:47,489 INFO [de.hays.liferay.schedulerprogrammatically.Scheduler] (EJB default - 5) Programmatic Scheduler: 1434551207489 

回答

0

您不顯示您的軟件包導入。 javax.inject.Singleton會解釋被EJB忽略的行爲。 javax.ejb.Singleton是你想要的。

+0

是的,我用的是使用javax。 ejb.Singleton – goblingift

1

我已經知道了 - 我用java任務控件查找野蠻人上運行的定時器,並且有多個Timer。

所以從來就編輯我的代碼,殺應用程序的所有現有的定時器,然後創建一個定時器,以確保,其實只有一個計時:

@PostConstruct 
public void startScheduler() { 

    TimerService timerService = sessionContext.getTimerService(); 

    logger.info("Current running timers: {}", timerService.getTimers().toString()); 

    for (Timer t : timerService.getTimers()) { 
     t.cancel(); 
     logger.info("killed the timer service: {}", t); 
    } 

    TimerConfig timerConfig = new TimerConfig(); 
    timerConfig.setPersistent(false); 

    Timer createdIntervalTimer = sessionContext.getTimerService().createIntervalTimer(0, 5000, timerConfig); 
    logger.info("Created new Timer: {}", createdIntervalTimer); 

}