Java EE Singleton计划任务执行两次

时间:2013-10-21 08:32:09

标签: java java-ee design-patterns jboss singleton

我想在预定时间(欧洲中部时间23:59和欧洲中部时间08:00)执行两项任务。我创建了一个EJB单例bean来维护这些方法:

@Singleton
public class OfferManager {

    @Schedule(hour = "23", minute = "59", timezone = "CET")
    @AccessTimeout(value = 0) // concurrent access is not permitted
    public void fetchNewOffers() {
        Logger.getLogger(OfferManager.class.getName()).log(Level.INFO, "Fetching new offers started");

        // ...

        Logger.getLogger(OfferManager.class.getName()).log(Level.INFO, "Fetching new offers finished");
    }

    @Schedule(hour="8", minute = "0", timezone = "CET")
    public void sendMailsWithReports() {
        Logger.getLogger(OfferManager.class.getName()).log(Level.INFO, "Generating reports started");

        // ...

        Logger.getLogger(OfferManager.class.getName()).log(Level.INFO, "Generating reports finished");
    }
}

问题是两个任务都执行了两次。服务器是WildFly Beta1,以UTC时间配置。

以下是一些可能有用的服务器日志:

2013-10-20 11:15:17,684 INFO  [org.jboss.as.server] (XNIO-1 task-7) JBAS018559: Deployed "crawler-0.3.war" (runtime-name : "crawler-0.3.war")
2013-10-20 21:59:00,070 INFO  [com.indeed.control.OfferManager] (EJB default - 1) Fetching new offers started
....
2013-10-20 22:03:48,608 INFO  [com.indeed.control.OfferManager] (EJB default - 1) Fetching new offers finished
2013-10-20 23:59:00,009 INFO  [com.indeed.control.OfferManager] (EJB default - 2) Fetching new offers started
....
2013-10-20 23:59:22,279 INFO  [com.indeed.control.OfferManager] (EJB default - 2) Fetching new offers finished

这种行为的原因可能是什么?

2 个答案:

答案 0 :(得分:1)

我通过使用服务器时间(UTC)指定预定时间来解决问题。 所以

@Schedule(hour = "23", minute = "59", timezone = "CET")

替换为:

@Schedule(hour = "21", minute = "59")

我不知道这种行为的原因,也许早期发布Wildfly是个问题。

答案 1 :(得分:0)

我遇到了与TomEE羽流7.0.4相同的问题。就我而言,解决方案是将@Singleton更改为@Stateless