EJB Scheduler不是每小时运行一次?

时间:2016-05-14 21:05:49

标签: java ejb scheduler

我有一个EJB Scheduler。我希望Method每小时运行一次。但该方法只运行一次。我不知道为什么。

这是我的EJB类:

@Singleton
public class DBTimeout {

  @PersistenceContext
  private EntityManager em;

  @Inject
  private Logger logger;

  @Schedule(second = "0", minute = "0", hour = "*/1", info = "Every 1 hour", persistent = false)
  public void refreshDBConnectionPool() {
    em.createNativeQuery("SELECT 1");
    logger.info("successfully executed cronejob for refreshing Database Connection Pool.");
  }

}

2 个答案:

答案 0 :(得分:0)

使用Wildfly,请确保您使用standalone-full.xml作为配置。默认的standalone.xml不支持EJB计划。

答案 1 :(得分:0)

你可以试试下面的代码吗?

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

@Singleton
@Startup
public class DBTimeout {

    @PersistenceContext
    private EntityManager em;

    @Inject
    private Logger logger;

    @Resource
    private TimerService timerService;

    @Timeout
    public void timeOut() {
        refreshDBConnectionPool();
    }

    @PostConstruct
    public void initTimer() {
        System.out.println("INIT-CALLED");
        ScheduleExpression exp = new ScheduleExpression();
        exp.hour("*/1").minute("*");
        timerService.createCalendarTimer(exp);
    }

    public void refreshDBConnectionPool() {
        em.createNativeQuery("SELECT 1");
        logger.info("successfully executed cronejob for refreshing Database Connection Pool.");
   }
}