@Schedule注释每隔几分钟(或秒)运行一次 [英] @Schedule annotation run every few minutes (or seconds)

查看:3766
本文介绍了@Schedule注释每隔几分钟(或秒)运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试以下列方式使用@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 //因为ejb在一个servlet(没有其他jvm)
public interface CronService {

public void iLive();
public void runsEveryMinute();
}
---
@Singleton
public class CronServiceBean实现CronService {
private static final JcanLogger LOG = JcanLoggerFactory.getLogger(CronServiceBean.class);

@Schedule(minute =*)
public void runsEveryMinute(){
LOG.info(running EveryMinute);
}

public void iLive(){
LOG.info(iLive);

}
---
LOG
...
CronServiceBean:34] iLive
pre>

根据日志,CronService活得很好,但是计划的任务'runsEveryMinute'不起作用。



如何使用EJB计划任务工作?

解决方案

根据 @Schedule 注释的.oracle.com / javaee / 7 / api / javax / ejb / Schedule.htmlrel =noreferrer> Javadoc ,默认值为:




  • * 除了小时,分和秒之外的所有字段;和

  • 0 在小时,分钟和秒钟。默认情况下



通过指定 minute =*,并将小时的默认值设置为 0 ,请求定时器在午夜后每分钟运行一小时(即00:00,00:01,00:02,...,00:59),然后再次直到第二天。而是使用:

  @Schedule(hour =*,minute =*)

要运行每隔几秒(例如, 10 秒),您可以使用 cron 类似的语法:

  @Schedule(hour =*,minute =*,second =* / 10,persistent = false)

默认情况下,调度程序会持续存在事件。


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.

这篇关于@Schedule注释每隔几分钟(或秒)运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆