In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to correctly use Cron4j expressions, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Cron4j is a lightweight Java task scheduling tool.
Introduce the Jar package:
It.sauronsoftware.cron4j cron4j 2.2.5
Cron4j's cron expression allows a maximum of five parts, each separated by a space, representing "minutes", "hours", "days", "months" and "weeks" respectively from left to right. The specific rules are as follows:
Score: values from 0 to 59: values from 0 to 23 days: values from 1 to 31, the letter L can be used to indicate the last day of the month month: values from 1 to 12, can be expressed by aliases: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, week: values from 0 to 6, 0 means Sunday, 6 means Saturday, aliases can be used to indicate: sun, mon, tue, wed, thu, fri, sat
The minutes, hours, days, months and weeks of the above five parts support the following characters respectively:
The number n: indicates a specific point in time, for example, 5 * means to execute a comma at 5 minutes, and: indicates to specify multiple numeric values, such as 3 minutes and 5 minutes to execute a minus sign -: indicates a range For example, 1-3 * means an asterisk is executed at the three time points of 1, 2, and 3:: indicates each time point. For example, * the division sign /: is performed per minute, indicating the increase of a specified value. For example, * / 5 means to execute every 5 minutes (sequence: 0:00, 0:05, 0:10, 0:15, etc.)
Common error: cron4j is slightly different from linux when using a division sign in an expression to specify the magnitude of increase. For example, in linux, the expression 10 Universe 3 * means that it is scheduled every three minutes starting at the 10th minute, while in cron4j it needs to be expressed using 10-59 Maple 3 *.
Scheduler scheduler = new Scheduler (); / / Writing method 1: in this way, the console prints scheduler.schedule every minute ("10-59 Every Minute Run 1 *", ()-> System.out.println ("Every Minute Run.")); / / Writing method II: in this way, the console will not have any print / / scheduler.schedule ("10 Every Minute Run 1 *", ()-> System.out.println ("Every Minute Run.")). Scheduler.start (); try {Thread.sleep (1000L * 60L * 10L);} catch (InterruptedException e) {e.printStackTrace ();} scheduler.stop ()
Two big questions: the first question is whether the task will be scheduled at the next point in time when an exception is thrown. The answer is yes. Whenever an exception occurs, the schedule will still be executed.
Scheduler scheduler = new Scheduler (); scheduler.schedule ("* / 1 *", ()-> {System.out.println ("Every Minute Run At:" + new Date ()); throw new RuntimeException ("Task scheduling throws an exception"); scheduler.start (); try {Thread.sleep (1000L * 60L * 10L);} catch (InterruptedException e) {e.printStackTrace ();} scheduler.stop ()
The print result is as follows:
Every Minute Run At: Wed Feb 13 10:09:00 CST 2019 java.lang.RuntimeException: task scheduling throws an exception at com.tinytime.demo.cron4j.Demo2.lambda$main$0 (Demo2.java:17) at it.sauronsoftware.cron4j.RunnableTask.execute (Unknown Source) at it.sauronsoftware.cron4j.TaskExecutor$Runner.run (Unknown Source) at java.lang.Thread.run (Thread.java:748) Every Minute Run At: Wed Feb 13 10:10:00 CST 2019 java.lang .RuntimeException: task scheduling throws an exception at com.tinytime.demo.cron4j.Demo2.lambda$main$0 (Demo2.java:17) at it.sauronsoftware.cron4j.RunnableTask.execute (Unknown Source) at it.sauronsoftware.cron4j.TaskExecutor$Runner.run (Unknown Source) at java.lang.Thread.run (Thread.java:748)
Thus it can be seen that even if there is an exception in the last scheduled task, the next task will still be scheduled when the time comes.
The second question is whether this scheduling will continue if a task takes a long time to execute, and if the task has not been executed since the last scheduling, the answer is also yes.
Scheduler scheduler = new Scheduler (); scheduler.schedule ("* / 1 *", ()-> {System.out.println ("start scheduling tasks. At: "+ new Date (); try {/ / wait two minutes Thread.sleep (1000L * 60L * 2L);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println (" Every Minute Run At: "+ new Date ());}); scheduler.start (); try {Thread.sleep (1000L * 60L * 10L);} catch (InterruptedException e) {e.printStackTrace () } scheduler.stop ()
The print result is as follows:
Start scheduling tasks. At: Wed Feb 13 10:22:00 CST 2019 starts scheduling tasks. At: Wed Feb 13 10:23:00 CST 2019 starts scheduling tasks. At: Wed Feb 13 10:24:00 CST 2019Every Minute Run At: Wed Feb 13 10:24:00 CST 2019 starts scheduling tasks. At: Wed Feb 13 10:25:00 CST 2019Every Minute Run At: Wed Feb 13 10:25:00 CST 2019
Thus it can be seen that even if the last scheduled task is not completed, the next task will still be scheduled when the time comes.
Summary: each scheduling is independent. Whether the last scheduling throws an exception and whether the execution is completed have nothing to do with this scheduling.
Thread scheduling:
Public class Quickstart {public static void main (String [] args) {Scheduler scheduler = new Scheduler (); scheduler.schedule ("* *", new Runnable () {@ Override public void run () {System.out.println ("Every Minute Run.");}}); scheduler.start () Try {Thread.sleep (1000L * 60L * 10L);} catch (InterruptedException e) {e.printStackTrace ();} scheduler.stop ();}}
System process scheduling:
Public class ProcessJob {public static void main (String [] args) {ProcessTask task = new ProcessTask ("C:\\ Windows\\ System32\\ notepad.exe"); Scheduler scheduler = new Scheduler (); scheduler.schedule ("* *", task); scheduler.start ();} after reading the above, do you have any further understanding of how to use Cron4j expressions correctly? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.