Project to use a timed task, so I studied it, the following on our project to use the Spring-quartz implementation steps to do the following to share.
One: The technology used (MAVEN-built SPRING-MVC project)
Spring-mvc
MyBatis,
Maven
Quartz
Two: Configuration
1: First introduce the quartz jar package in the Pom.xml (the other jar packages in the project are no longer outlined here)
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
2: In spring's configuration file Applicationg.xml (different project names may not be the same), add the following declaration (the red part is the quartz to add).
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:task= "Http://www.springframework.org/schema/task"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://code.alibabatech.com/schema/dubbo
Http://code.alibabatech.com/schema/dubbo/dubbo.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/task
Http://www.springframework.org/schema/task/spring-task-3.1.xsd ">
Again in the configuration file, include Quartz in spring management
<!--timer configuration started-->
<bean id= "Simplejobfactory" class= "Org.quartz.simpl.SimpleJobFactory"/>
<bean id= "Schedulerfactorybean" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "Jobfactory" ref= "Simplejobfactory"/>
</bean>
<!--timer configuration end-->
3): Our Spring quartz configuration is complete
Two: Code implementation
1): To incorporate the timing tasks into the management, we first want to persist the scheduled tasks into the database (the persisted table script is not outlined) the following is the persistence class.
Public class schedulejob{
Private Long id;//uniquely marked
Private String jobid;//timed task unique (UUID)
Private String jobname;//timed Task Name
Private String Jobgroup; Group to which the scheduled task belongs
Private String Jobstatus; Timed task status (0: Open, 1: Disabled, 2: delete)
Private String cronexpression; Timed Task expression
Private String Cronclass; Scheduled Task Execution Class
Private String Descs; Timed Task Description
Setting,getting method omits
}
2): Timing Task Business processing layer (in the way of annotation)
@Component
Public class Quartzjobservice {
@Autowired
Private Schedulerfactorybean Schedulerfactorybean;
Open
Public void startjob (schedulejob job) throws exception{
Stopjob (Job);
Log.info ("Start Job (start) >jobid:" +job.getid ());
Scheduler Scheduler = Schedulerfactorybean.getscheduler ();
Triggerkey Triggerkey = Triggerkey.triggerkey (Job.getjobid (), Job.getjobgroup ());
Get trigger, that is, the bean id= defined in the spring configuration file, "Mytrigger"
Crontrigger trigger = (Crontrigger) scheduler.gettrigger (Triggerkey);
Does not exist, create a
if (null = = trigger) {
Job JB = (Job) class.forname (Job.getcronclass ()). Newinstance ();
Jobdetail Jobdetail = Jobbuilder.newjob (Jb.getclass ())
. Withidentity (Job.getjobid (), Job.getjobgroup ()). build ();
Jobdetail.getjobdatamap (). Put (Job.getjobid (), job);
Expression Scheduler Builder
Cronschedulebuilder Schedulebuilder = cronschedulebuilder.cronschedule (Job
. Getcronexpression ());
Build a new trigger by the new cronexpression expression
Trigger = Triggerbuilder.newtrigger (). Withidentity (Job.getjobid (), Job.getjobgroup ()). Withschedule ( Schedulebuilder). build ();
Scheduler.schedulejob (Jobdetail, Trigger);
} Else {
Trigger already exist, then update the appropriate timing settings
Expression Scheduler Builder
Cronschedulebuilder Schedulebuilder = cronschedulebuilder.cronschedule (Job
. Getcronexpression ());
Rebuilding trigger by the new cronexpression expression
Trigger =trigger.gettriggerbuilder (). Withidentity (Triggerkey)
. Withschedule (Schedulebuilder). build ();
Reset job execution by new trigger
Scheduler.reschedulejob (Triggerkey, Trigger);
}
Log.info ("Start Job (end) >jobid:" +job.getid ());
}
Stop it
Public booleanstopjob (Schedulejob job) {
Log.info ("Stop Job (start) >jobid:" +job.getid ());
String Jobid =job.getjobid ();
String jobgroupid = Job.getjobgroup ();
if (Jobid.equals ("") | | Jobgroupid.equals ("")) {
return false;
}
Scheduler Scheduler = Schedulerfactorybean.getscheduler ();
Triggerkey Triggerkey = Triggerkey.triggerkey (Job.getjobid (), Job.getjobgroup ());
Try {
Crontrigger trigger = (Crontrigger) scheduler.gettrigger (Triggerkey);
if (null != Trigger) {
Scheduler.deletejob (Trigger.getjobkey ());
}
Catch (Schedulerexception e) {
Log.error (E.getmessage (), E);
E.printstacktrace ();
return false;
}
Log.info ("Stop Job (end) >jobid:" +job.getid ());
return true;
}
}
This is the method I will no longer explain the (here reference is to share online (the original text can not find ...) ))。
3): The following is the specific control layer.
@Controller
@RequestMapping ("/job")
Public class Jobcontroller extends basecontroller {
@Autowired
Private Quartzjobservice Quartzjobservice;
@Autowired
Private Schedulejobservice Schedulejobservice;
Private final Logger log = Logger.getlogger (Jobcontroller.class);
/**
* Open timed Task
* @param ID
* @return
* @author zhangzq-a
*/
@RequestMapping (value= "/start", produces = "text/plain;charset=utf-8")
@ResponseBody
Public String StartTask (long[] id) {
if (id==null| | id.length<1) {
return "Redirect:/job/list";
}
for (Long Jid:id) {
Schedulejob job = Schedulejobservice.get (jId);
if (job!=null) {
Log.info ("Open timed task start" Timed task ID: "+jid);"
if ("1". Equals (Job.getjobstatus ())) {
Schedulejob j = new schedulejob ();
J.setid (Job.getid ());
J.setjobstatus ("0");
Schedulejobservice.update (j);
}
Try {
Quartzjobservice.startjob (Job);
Log.info ("Open timed task End" timed task ID: "+jid");
return "True";
Catch (Exception e) {
Log.error (E.getmessage (), E);
}
}
}
return "Scheduled task failed to start, please contact Administrator";
}
/**
* Disable Task
* @param ID
* @return
* @author zhangzq-a
*/
@RequestMapping (value= "/stop", produces = "text/plain;charset=utf-8")
@ResponseBody
Public String Stoptask (long[] id) {
if (id==null| | id.length<1) {
return "Redirect:/job/list";
}
for (Long Jid:id) {
Schedulejob job = Schedulejobservice.get (jId);
if (job!=null) {
Log.info ("Disable timed task start" Timed task ID: "+jid);"
if ("0". Equals (Job.getjobstatus ())) {
Schedulejob j = new schedulejob ();
J.setid (Job.getid ());
J.setjobstatus ("1");
Schedulejobservice.update (j);
}
Try {
Quartzjobservice.stopjob (Job);
Log.info ("Disable timed task End" timed task ID: "+jid);"
return "True";
Catch (Exception e) {
Log.error (E.getmessage (), E);
}
}
}
return "Scheduled task failed to start, please contact Administrator";