One: First understand the scene used by the spring timer
For example: Timing statistics report, or login account password verification failed to freeze account, every 12 o'clock in the evening to unlock the account and so on ...
Using JAR Packages: 2.0 different ways to use
<!--timers--
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.6</version>
</dependency>
2. Code examples
1. Define the task class to be executed
Package Com.jk.controller;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Org.quartz.Job;
Import Org.quartz.JobExecutionContext;
Import org.quartz.JobExecutionException;
Task work class to prepare for scheduled scheduling
public class Testding implements Job {
@Override
public void execute (jobexecutioncontext arg0)
Throws Jobexecutionexception {
System.out.println (New
SimpleDateFormat ("Yyyy-mm-dd"). Format (new Date ()) + "-
=-=-=-=-=");
}
}
2. Run through the Factory startup task main function
Package Com.jk.controller;
Import java.text.ParseException;
Import Org.quartz.CronTrigger;
Import Org.quartz.JobDetail;
Import Org.quartz.Scheduler;
Import org.quartz.SchedulerException;
Import Org.quartz.SchedulerFactory;
Import Org.quartz.impl.StdSchedulerFactory;
public class Testdingshiqi {
public static void Main (string[] args) throws
Schedulerexception, ParseException {
Instantiate timer factory
Schedulerfactory gschedulerfactory =
New Stdschedulerfactory ();
Get the Task Scheduler from the factory
Scheduler Sche =
Gschedulerfactory.getscheduler ();
Set task class Task name for timed execution
Jobdetail jobdetail = new Jobdetail ("Testjob ren
The best name is business-related "," Job_group_new1 ",
Testding.class);//Task Name, Task Group, task execution class
Get trigger adds a task to the specified trigger
Crontrigger trigger = new Crontrigger
("Testtrigger any name is best and business related",
"Trigger_group_name1");//trigger name, trigger group
Set the time for the trigger
Trigger.setcronexpression ("0/1 * * * *?"); /
Trigger Time setting
Putting triggers and tasks into the scheduler
Sche.schedulejob (Jobdetail, Trigger);
Start
if (!sche.isshutdown ()) {
Sche.start ();
}
}
}
3. Encapsulated Timing Task management tool class
Package Com.jk.controller;
Import Org.quartz.CronTrigger;
Import Org.quartz.JobDetail;
Import Org.quartz.Scheduler;
/**
* Quartz Dispatch Manager
*
* @author Administrator
*
*/
public class Testdingmanger {
private static String Job_group_name =
"Extjweb_jobgroup_name";
private static String Trigger_group_name =
"Extjweb_triggergroup_name";
/**
* @Description: Add a Timer task, use the default task group name
, trigger name, trigger group name
*
* @param sched
* Scheduler
*
* @param jobn AME
* Task Name
* @param CLS
* Task
* @param time
* Timing settings, refer to Quartz documentation
*
* @Title: quartzmanager.java< br> */
public static void AddJob (Scheduler sched, String
JobName, @SuppressWarnings ("Rawtypes") Class cls,
Str ing time) {
try {
Jobdetail jobdetail = new Jobdetail
(JobName, Job_group_name, CLS);//Task Name, Task Group, task hold
Line class
/ /trigger
Crontrigger trigger = new Crontrigger
(JobName, trigger_group_name);//trigger name, trigger group
Trigger.setcronexpression (time);//trigger
between
Sched.schedulejob (Jobdetail, trigger);
//Start
if (! Sched.isshutdown ()) {
Sched.start ();
}
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Add a Scheduled Task
*
* @param sched
* Scheduler
*
* @param jobName
* Task Name
* @para M jobgroupname
* Task Group name
* @param triggername
* Trigger name
* @param triggergroupname
* Trigger Group name
* @param JOBCL The
* Task
* @param time
* settings, refer to Quartz documentation
*
* @Title: Quartzmanager.java
*/
public static VO ID addjob (Scheduler sched, String
JobName, String jobgroupname, String triggername,
string triggergroupname, @Sup Presswarnings ("Rawtypes")
Class Jobclass, String time) {
try {
Jobdetail jobdetail = new Jobdetail
(jobName , Jobgroupname, Jobclass);//Task Name, Task group, any
Service execution class
//Trigger
Crontrigger trigger = new Crontrigger
(Triggername, Trig Gergroupname);//trigger name, trigger group
Trigger.setcronexpression (time);//Trigger
Set
Sched.schedulejob (Jobdetail, Trigger);
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Modifies the trigger time for a task (using the default
service group name, trigger name, trigger group name)
*
* @param sched
* Scheduler
* @param jobName
* @param time
*
* @Title: Quartzmanager.java
*/
@SuppressWarnings ("Rawtypes")
public static void Modifyjobtime (Scheduler sched,
string jobName, String time) {
try {
Crontrigger trigger = (Crontrigger)
S Ched.gettrigger (JobName, trigger_group_name);
if (trigger = = null) {
return;
}
String oldtime =
Trigger.getcronexpression (),
if (!oldtime.equalsignorecase (time)) {
Jobdetail Jobdetail =
Sched.getjobdetail (jobName, job_group_name);
Class Objjobclass =
Jobdetail.getjobclass ();
Removejob (Sched, jobName);
AddJob (Sched, JobName, Objjobclass ,
Time);
}
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Modify the trigger time for a task
*
* @param sched
* Scheduler *
* @param sched
* Scheduler
* @param tr Iggername
* @param triggergroupname
* @param time
*
* @Title: Quartzmanager.java
*/
public static void Modifyjobtime (Scheduler sched,
string triggername, String triggergroupname, String
time) {
try {
Cro Ntrigger trigger = (Crontrigger)
Sched.gettrigger (Triggername, triggergroupname);
if (trigger = = null) {
Retu Rn
}
String oldtime =
Trigger.getcronexpression ();
if (!oldtime.equalsignorecase (time)) {
Crontrigger ct = (Crontrigger)
Trigger;
//Modified
Ct.setcronexpression (time);
//Restart trigger
Sched.resumetrigger ( Triggername,
Triggergroupname);
}
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Remove a task (using the default task group name, touch
Sender name, trigger group name)
*
* @param sched
* Scheduler
* @param jobName
*
* @Title: Quartzmanager.java
*/
public static void Removejob (Scheduler sched,
String jobName) {
Try {
Sched.pausetrigger (jobName,
Trigger_group_name);//Stop Trigger
Sched.unschedulejob (jobName,
Trigger_ group_name);//Remove Trigger
Sched.deletejob (JobName,
Job_group_name);//delete task
} catch (Exception e) {
throw new Ru Ntimeexception (e);
}
}
/**
* @Description: Remove a task
*
* @param sched
* Scheduler
* @param jobName
* @param jobgroupname
* @param triggername
* @param triggergroupname
*
* @Title: Quartzmanager.java
*/
public static void Removejob (Scheduler sched,
String JobName, String jobgroupname, String
Triggername, String triggergroupname) {
try {
Sched.pausetrigger (Triggername,
Triggergroupname);//Stop Trigger
Sched.unschedulejob (Triggername,
Triggergroupname);//Remove Trigger
Sched.deletejob (JobName, jobgroupname);//
Delete a task
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Start all Scheduled Tasks
*
* @param sched
* Scheduler
*
* @Title: Quartzmanager.java
*/
public static void Startjobs (Scheduler sched) {
try {
Sched.start ();
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Turn off all scheduled tasks
*
*
* @param sched
* Scheduler
*
*
* @Title: Quartzmanager.java
*/
public static void Shutdownjobs (Scheduler sched) {
try {
if (!sched.isshutdown ()) {
Sched.shutdown ();
}
} catch (Exception e) {
throw new RuntimeException (e);
}
}
}
Integrated timers in Spring projects
One: Spring-quartz.xml
Injected into the Schedulerfactorybean plant
<bean id= "Schedulerfactorybean" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
Either through attribute injection or automatic injection in the service layer, the automatic injection is used here
@Autowired
Private Schedulerfactorybean Schedulerfactorybean;
public void Test () throws Exception {
Get the Task Scheduler from the factory
Scheduler Sche = Schedulerfactorybean.getscheduler ();
Set task class Task name for timed execution
Jobdetail jobdetail = new Jobdetail ("Testjob any name is best and business related", "Job_group_new1", springtask.class);//Task Name, Task Group, task execution class
Get trigger adds a task to the specified trigger
Crontrigger trigger = new Crontrigger ("Testtrigger any name is best and business related", "trigger_group_name1");//trigger name, trigger group
Set the time for the trigger
Trigger.setcronexpression ("0/3 * * * *?"); /Trigger Time setting
Putting triggers and tasks into the scheduler
Sche.schedulejob (Jobdetail, Trigger);
Start
if (!sche.isshutdown ()) {
Sche.start ();
}
}
It's a hassle to use factory creation every time. You can create a tool class:
Package com.jk.util;
Import Org.quartz.CronTrigger;
Import Org.quartz.JobDetail;
Import Org.quartz.Scheduler;
public class Quartzmanager {
private static String Job_group_name = "Extjweb_jobgroup_name";
private static String Trigger_group_name = "Extjweb_triggergroup_name";
/**
* @Description: Add a scheduled task, using the default task group name, trigger name, trigger group name
*
* @param sched
* Scheduler
*
* @param jobName
* Task Name
* @param CLS
* Mission
* @param time
* Time settings, reference quartz documentation
*
* @Title: Quartzmanager.java
*/
public static void AddJob (Scheduler sched, String jobName, @SuppressWarnings ("Rawtypes") Class cls, string time) {
try {
Jobdetail jobdetail = new Jobdetail (JobName, Job_group_name, CLS);//Task Name, Task Group, task execution class
Trigger
Crontrigger trigger = new Crontrigger (JobName, trigger_group_name);//trigger name, trigger group
Trigger.setcronexpression (time);//Trigger setting
Sched.schedulejob (Jobdetail, Trigger);
Start
if (!sched.isshutdown ()) {
Sched.start ();
}
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Add a Scheduled Task
*
* @param sched
* Scheduler
*
* @param jobName
* Task Name
* @para M jobgroupname
* Task Group name
* @param triggername
* Trigger name
* @param triggergroupname
* Trigger Group name
* @param JOBCL The
* Task
* @param time
* settings, refer to Quartz documentation
*
* @Title: Quartzmanager.java
*/
public static VO ID addjob (Scheduler sched, String jobName, String jobgroupname, String triggername, String triggergroupname, @SuppressWar Nings ("Rawtypes") Class Jobclass, String time) {
try {
Jobdetail jobdetail = new Jobdetail (JobName, Jobgroupname, Jobclass);//Task Name, Task Group, Task execution class
//Trigger
Crontrigger Trigger = new Crontrigger (triggername, triggergroupname);//Trigger Name, Trigger group
Trigger.setcronexpression (time);//Trigger Timing
Sched.schedulejob (jobdetail, trigger);
} catch (Exception E) {
throw new RuntimeException (e);
}
}
/**
* @Description: Modify the trigger time for a task (using the default task group name, trigger name, trigger group name)
*
* @param sched
* Scheduler
* @param jobName
* @param time
*
* @Title: Quartzmanager.java
*/
@SuppressWarnings ("Rawtypes")
public static void MoD Ifyjobtime (Scheduler sched, String jobName, String time) {
try {
Crontrigger trigger = (Crontrigger) sched.gettrig Ger (JobName, trigger_group_name);
if (trigger = = null) {
return;
}
String oldtime = Trigger.getcronexpression ();
if (!oldtime.equalsignorecase (time)) {
Jobdetail jobdetail = Sched.getjobdetail (JobName, job_group_name);
Class Objjobclass = Jobdetail.getjobclass ();
Removejob (Sched, jobName);
AddJob (Sched, JobName, Objjobclass, time);
}
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Modify the trigger time for a task
*
* @param sched
* Scheduler *
* @param sched
* Scheduler
* @param tr Iggername
* @param triggergroupname
* @param time
*
* @Title: Quartzmanager.java
*/
public static void Modifyjobtime (Scheduler sched, String triggername, String triggergroupname, String time) {
try {
Crontrigger Trigger = (Crontrigger) sched.gettrigger (triggername, triggergroupname);
if (trigger = = null) {
return;
}
String oldtime = Trigger.getcronexpression ();
if (!oldtime.equalsignorecase (time)) {
Crontrigger ct = (Crontrigger) trigger;
//Modified
Ct.setcronexpress Ion (time);
//Restart trigger
Sched.resumetrigger (triggername, triggergroupname);
}
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Remove a task (using the default task group name, trigger name, trigger group name)
*
* @param sched
* Scheduler
* @param jobName
* * @Title: Quartzmanager.java
*/
public static void Removejob (Scheduler sched, String jobName) {
try {
S Ched.pausetrigger (JobName, trigger_group_name);//Stop Trigger
Sched.unschedulejob (JobName, trigger_group_name);// Remove Trigger
Sched.deletejob (JobName, job_group_name);//delete task
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Remove a task
*
* @param sched
* Scheduler
* @param jobName
* @param jobgroupname
* @param triggername
* @param triggergroupname
*
* @Title: Quartzmanager.java
*/
public static void Removejob (Scheduler sched, String jobName, String jobgroupname, String triggername, String Triggergroup Name) {
try {
Sched.pausetrigger (Triggername, triggergroupname);//Stop Trigger
Sched.unschedulejob (Triggername, triggergroupname);//Remove Trigger
Sched.deletejob (JobName, jobgroupname);//Delete task
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Start all Scheduled Tasks
*
* @param sched
* Scheduler
*
* @Title: Quartzmanager.java
*/
public static void Startjobs (Scheduler sched) {
try {
Sched.start ();
} catch (Exception e) {
throw new RuntimeException (e);
}
}
/**
* @Description: Turn off all scheduled tasks
*
*
* @param sched
* Scheduler
*
*
* @Title: Quartzmanager.java
*/
public static void Shutdownjobs (Scheduler sched) {
try {
if (!sched.isshutdown ()) {
Sched.shutdown ();
}
} catch (Exception e) {
throw new RuntimeException (e);
}
}
}
Spring integrated quartz for dynamic timers