Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
In the above "Quartz" Quartz storage and persistence-based quartz.properties configuration , it is possible to persist by configuring the Quartz.properties file. This article will implement storage and persistence in the way that spring is configured.
1, as above, to create the relevant data table, and a new Java project, and import the relevant package, the entire project directory is as follows:
2. Create Job Class
Package Com.mucfc;import Java.text.simpledateformat;import Java.util.date;import org.apache.log4j.logger;import Org.quartz.job;import Org.quartz.jobexecutioncontext;import Org.quartz.jobexecutionexception;public class MyJob Implements Job{private static final Logger Logger = Logger.getlogger (myjob.class); @Overridepublic void Execute (Jobexecutioncontext context) throws Jobexecutionexception {System.out.println ("Hello Quzrtz "+new simpledateformat (" Yyyy-mm-dd HH:mm:ss "). Format (new Date ()));}}
3. Create a scheduling class
Package Com.mucfc;import Java.text.parseexception;import Java.util.list;import org.apache.commons.lang.StringUtils ; Import Org.quartz.cronschedulebuilder;import Org.quartz.job;import Org.quartz.jobbuilder;import Org.quartz.jobdetail;import Org.quartz.jobkey;import Org.quartz.scheduler;import org.quartz.SchedulerException; Import Org.quartz.schedulerfactory;import Org.quartz.simpleschedulebuilder;import Org.quartz.simpletrigger;import Org.quartz.trigger;import Org.quartz.triggerbuilder;import Org.quartz.triggerkey;import Org.quartz.impl.stdschedulerfactory;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import org.springframework.stereotype.Component; @Componentpublic class Quartztest {@Autowiredprivate Scheduler Scheduler; private static string job_group_name = "Ddlib";p rivate static string trigger_group_name = "Ddlibtrigger";/** * Start a Simples Chedule() Schedule */public void Startschedule () {try {//1, create an Jobdetail instance, specify Quartzjobdetail Jobdetail = Jobbuilder.newjob ( Myjob.class)//Task execution class. Withidentity ("Job1_1", "jGroup1")//Task Name, Task Group. Build ();//2, create Triggersimpleschedulebuilder Builder = Simpleschedulebuilder.simpleschedule ()//Set the number of executions. Repeatsecondlyfortotalcount (10); Trigger Trigger = Triggerbuilder.newtrigger (). Withidentity ("Trigger1_1", "TGroup1"). Startnow (). Withschedule ( Builder). Build ();//3, create Schedulerscheduler.start ();//4, Schedule execution Scheduler.schedulejob (jobdetail, trigger); try { Thread.Sleep (60000);} catch (Interruptedexception e) {e.printstacktrace ();} Scheduler.shutdown ();} catch (Schedulerexception e) {e.printstacktrace ();}} /** * Find the existing job from the database and re-open the account schedule */public void Resumejob () {try {//① get all the trigger groups in the scheduler list<string> triggergroups = Schedul Er.gettriggergroupnames ();//② re-restores the run for (int i = 0; i < triggergroups.size (); i++) {list&) in the Tgroup1 group named Trigger1_1 Trigger Lt string> triggers = Scheduler.gettriggergroupnames (); for (int j = 0; J <Triggers.size (); J + +) {Trigger TG = Scheduler.gettrigger (New Triggerkey (Triggers.get (j), Triggergroups.get (i))),//②-1: Determine if by name (TG instanceof simpletrigger&& tg.getdescription (). Equals ("Tgroup1.trigger1_1")) {//②-1: Restore Run Scheduler.resumejob (new Jobkey (Triggers.get (j), Triggergroups.get (i)));}}} Scheduler.start ();} catch (Exception e) {e.printstacktrace ();}}}
4. beans.xml File Configuration
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" 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.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd Http://www.springframework.org/schema/task Http://www.springframework.org/schema/task/sp Ring-task-3.0.xsd "> <!--automatic scanning of annotated beans--><context:component-scan base-package=" COM.MUCFC "/> <conte Xt:property-placeholder location= "classpath:jdbc.properties"/><bean id= "Quartzdatasource" class= " Org.springframework.jdbc.datasource.DriverManagerDataSource "><!--<property name=" Driverclass"Value=" ${quartz.driverclassname} "/><property name=" Jdbcurl "value=" ${quartz.url} "/><property name=" User "value=" ${quartz.username} "/><property name=" password "" Value= "${quartz.password}"/><property Name= "Minpoolsize" value= "${quartz.minpoolsize}"/><property name= "initialpoolsize" value= "${ Quartz.initialpoolsize} "/>--<property name=" Driverclassname "value=" ${quartz.driverclassname} "/> <property name= "url" value= "${quartz.url}"/> <property name= "username" value= "${quartz.username}"/> <property name= "Password" value= "${quartz.password}"/> </bean><!--Quartz Persistent storage--<bean Name= "Quartzscheduler" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" ><property name= " DataSource "><ref bean=" Quartzdatasource "/></property><property name=" Applicationcontextschedulercontextkey "value=" ApplicationContext "/> <property name=" QuartzPropeRties "> <props> <prop key=" Org.quartz.scheduler.instanceName ">CRMscheduler</prop> <prop key= "Org.quartz.scheduler.instanceId" >AUTO</prop> <!--thread pool configuration--< Prop key= "Org.quartz.threadPool.class" >org.quartz.simpl.SimpleThreadPool</prop> <prop key= "Org.quar Tz.threadPool.threadCount ">20</prop> <prop key=" Org.quartz.threadPool.threadPriority ">5</prop > <prop key= "org.quartz.jobStore.misfireThreshold" >120000</prop> <!--jobstore configuration --<prop key= "Org.quartz.jobStore.class" >org.quartz.impl.jdbcjobstore.JobStoreTX</prop> <!--cluster configuration--<prop key= "org.quartz.jobStore.isClustered" >true</prop> <prop key= "Org.quartz.jobStore.clusterCheckinInterval" >15000</prop> <prop key= "Org.quartz.jobStore.maxMi Sfirestohandleatatime ">1</prop> <!--datasheet set--<prop key= "Org.quartz.jobStore.tablePre Fix ">qrtz_</prop> <prop key=" Org.quartz.jobStore.dataSource ">qzDS</prop> </props > </property></bean> </beans>
Where the data source jdbc.properties:
########### #quartz db######################## quartz.driverclassname=com.mysql.jdbc.driver quartz.url= Jdbc:mysql://127.0.0.1:3306/quartz?useunicode=true Quartz.username=root[email protected]quartz.minPoolSize= 7 quartz.initialpoolsize=12
5. Test class
Package Com.mucfc;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test {public static void main (String [] args) {ApplicationContext context=new classpathxmlapplicationcontext ("Beans.xml"); Quartztest Quartztest=context.getbean ("Quartztest", quartztest.class); Quartztest.startschedule (); Quartztest.resumejob ();}}
Output Result:
View Data Sheet
Stop the program and change the statement to:
Quartztest.startschedule (); Quartztest.resumejob ();
You can then discover that the task is running and automatically delete it from the data table when it finishes running:
Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
"Quartz" Quartz storage and persistence-spring-based configuration