我想尝试通过以下方式使用@Schedule批注:
public class MyTestServlet extends HttpServlet { private static JcanLogger LOG = JcanLoggerFactory.getLogger(ServiceTestServlet.class); @EJB CronService cronService; public void service(HttpServletRequest req, HttpServletResponse resp) throws .... { .... cronService.iLive(); } --- @Local // because the ejb is in a servlet (there is no other jvm) public interface CronService { public void iLive(); public void runsEveryMinute(); } --- @Singleton public class CronServiceBean implements CronService { private static final JcanLogger LOG = JcanLoggerFactory.getLogger(CronServiceBean.class); @Schedule(minute="*") public void runsEveryMinute() { LOG.info(" runs EveryMinute "); } public void iLive() { LOG.info("iLive"); } --- LOG ... CronServiceBean:34 ] iLive
根据日志,CronService运行良好,但是计划的任务“ runsEveryMinute”不起作用。
使用EJB计划任务如何工作?
按照该Javadoc中的@Schedule注释,默认值是:
@Schedule
*
0
通过minute="*"将小时值保留为默认值0,它要求计时器在午夜之后每分钟运行一小时(即00:00、00:01、00:02,…,00:59),然后不再运行直到第二天。而是使用:
minute="*"
@Schedule(hour="*", minute="*")
要每隔几秒钟(例如10几秒钟)运行一次,可以使用类似cron的语法:
10
@Schedule(hour = "*", minute = "*", second = "*/10", persistent = false)
默认情况下,调度程序保留事件。persistent = false如果需要的话,设置将防止它们随着时间的推移而堆积。
persistent = false