SpringMVC (a) Timing task

In actual use, the timing task is useful, may be performed at a specified time by timer reptiles tasks, data processing, e-mail reminders.

1. By using the timing annotation task

1
2
3
4
5
6
7
8
9
10
11
12
13
@Component // instantiate 
@EnableScheduling # Enable timer task
public class SzZhsjptDdrbbZhsjb {
// The method of labeling a timing
// @Scheduled (the cron = "0. 1 * * 25?")
@Scheduled (the cron = "0 * / 10 * * * ? ") # Set the timer, you need to configure on the method
public void start () throws InterruptedException {
SzZhsjptDdrbbZhsjb zhsj = new new SzZhsjptDdrbbZhsjb (); // start Firefox
zhsj.execute ();
Thread.sleep (3000);
zhsj.quit ( );
}
}

2. Timing task automatically scan

Configuration namespace task in springmvc.xml

1
2
xmlns:task="http://www.springframework.org/schema/task"   
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd

Enable annotation-driven
<task:annotation-driven scheduler="myScheduler"/>
configuration timed task thread pool
<task:scheduler id="myScheduler" pool-size="5"/>

. 1 
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 major Column   SpringMVC (a) a timing task AN>
13 is
Examples of @Component // 
// @ EnableScheduling # scan timer
public class SzZhsjptDdrbbZhsjb {
// The method of labeling a timing
// @Scheduled (the cron = "0. 1 * * 25?")
@Scheduled (the cron = "0 * / 10 * ? * * ") # Set the timer, you need to configure on the method
public void start () throws InterruptedException {
SzZhsjptDdrbbZhsjb zhsj = new new SzZhsjptDdrbbZhsjb (); // start Firefox
zhsj.execute ();
Thread.sleep (3000);
zhsj. quit ();
}
}

3.cron expression

In springmvc, the timing task it used to be to use cron expression. It uses syntax crontab command similar, but there are little differences in the details.

crontab command in common in Unix and Unix operating system, an instruction set to be executed periodically. The command instructions read from the standard input device, and store it in the "crontab" file for read and executed later. The word comes from the Greek chronos (χρόνος), the intent is the time. Typically, the instructions are stored crontab daemon activated, crond often run in the background every minute to check for scheduled jobs need to be performed. Such work is generally known as cron jobs.

The syntax
for example, every ten minutes running time syntax is as follows:
[? 0 * / 10 * * *]

0 */10 * * * * ?
Sec (0 to 59) Min (0 - 59) H (0 to 23) Day (0 to 31) Month (0 to 11) Weeks (1-7) year
  • [,] [*] 10, 20, 10 and 20 represent the time points are run once

  • [-] [?] Represents a 10-20 minute run every 10-20 minutes between the time

  • [?] Do not set this field (omit)

  • [*] [*] Means every 10 minutes

  • [/] [Start time / interval time], for example [0/5] 0 minutes, every 5 min run time; * [/] denotes 5 every five minutes in a position minutes

Cron_ Wikipedia
SpringMVC used Cron expressions timer

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/12407708.html