@Schedule annotation run every few minutes (or seconds)

前端 未结 2 855
闹比i
闹比i 2021-02-03 22:06

I would like to try to use the @Schedule annotation in the following way:

public class MyTestServlet extends HttpServlet {
    private static JcanLogger LOG = Jc         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-03 22:32

    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.

提交回复
热议问题