admin管理员组文章数量:1794759
scheduled一分钟执行一次,@Schedule注释每隔几分钟(或秒)运行一次
I would like to try to use the @Schedule annotation in the following way:
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
Based on the log, the CronService live and well, but the scheduled task 'runsEveryMinute' doesnt work.
How should it work using an EJB scheduled task?
解决方案
As per the Javadoc for the @Schedule annotation, the default values are:
* for all fields except hour, minute, and second; and
0 for hour, minute, and second, by default.
By specifying minute="*" and leaving hour at its default of 0, it requests that the timer run every minute after midnight for one hour (i.e., 00:00, 00:01, 00:02, ..., 00:59) and then not again until the next day. Instead, use:
@Schedule(hour="*", minute="*")
To run every few seconds (e.g., 10 seconds), you can use a cron-like syntax:
@Schedule(hour = "*", minute = "*", second = "*/10", persistent = false)
By default, the scheduler persists events. Setting persistent = false will prevent them from building up over time, if so desired.
本文标签: 几分钟每隔注释scheduledschedule
版权声明:本文标题:scheduled一分钟执行一次,@Schedule注释每隔几分钟(或秒)运行一次 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686477536a71906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论