@Schedule annotation run every few minutes (or seconds)

别等时光非礼了梦想. 提交于 2019-12-03 04:35:41
Brett Kail

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.

Please find the following details for the scheduler configuration.

(1) To run every 1 min

@Schedule(hour = "*", minute = "*/1", persistent = false)

(2) To run every 5 mins

@Schedule(hour = "*", minute = "*/5", persistent = false)

(3) To run every 30 seconds

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

(4) To run every day at 6:00 am

@Schedule(hour = "6", minute = "0", second = "0", persistent = false)

(5) To run on every Friday at 2:00 pm

@Schedule(dayOfWeek = "Fri", hour = "14", persistent = false)

(6) To run on the first day of every month at 5:00 am

@Schedule(dayOfMonth="1", hour = "5", persistent = false)

I hope this information will you to configure the scheduler as per your requirement.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!