Vegetable chicken Self-cultivation, first contact quartz, make a record. -------Jstarseven
Recently in the project, for the first time in the SPRINGMVC to configure the practical quartz, deeply feel the quartz bring convenience, handy to make a record.
Brief introduction:
Quartz is an open-source job scheduling framework that provides a simple yet powerful mechanism for job scheduling in Java applications. Quartz allows developers to schedule jobs based on time intervals (or days). It implements a many-to-many relationship between jobs and triggers, and also associates multiple jobs with different triggers. Applications that integrate Quartz can reuse jobs from different events and can combine multiple jobs for an event. Although you can configure Quartz through the properties file (the data source for the JDBC transaction can be specified in the properties file, the global job and/or trigger listener, plug-in, thread pool, and more), it is not integrated with the context or reference of the application server at all. The result is that jobs cannot access the internal functions of the WEB server, for example, jobs dispatched by Quartz do not affect the server's dynamic cache and data sources when using WebSphere application servers.
Jobs and triggers
The two basic units of a Quartz dispatch package are jobs and triggers. A job is an executable task that can be dispatched, and the trigger provides a schedule for the job. Although the two entities are easy to fit together, it is a reason to separate them in the Quartz, and it is also beneficial.
By separating the work to be performed from its dispatch, Quartz allows the scheduling trigger to be modified without losing the job itself or the context of the job. Also, any single job can have multiple triggers associated with it.
Reference article: http://www.ibm.com/developerworks/cn/java/j-quartz/index.html
Code implementation Simple case:
Frame SPRINGMVC:
Quartz configuration file xml:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/mvc HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC/SPR Ing-mvc-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/s Pring-aop-3.2.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-t X-3.2.xsd "Default-lazy-init= "true"> <Description>Quartz Scheduled Task scheduling configuration</Description> <Context:property-placeholder Location= "Classpath:job.properties"ignore-unresolvable= "true"/> <!--Data Analysis timing issued work class - <BeanID= "Quartzjob"class= "Com.server.job.QuartzJob"/> <!--define methods for calling and invoking objects - <BeanID= "Jobtask"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!--the class that is called - < Propertyname= "TargetObject"> <refBean= "Quartzjob"/> </ Property> <!--calling methods in a class - < Propertyname= "Targetmethod"> <value>Work</value> </ Property> < Propertyname= "Concurrent"value= "false"/> </Bean> <!--Define trigger Time - <BeanID= "Jobtasktrigger"class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean"> < Propertyname= "Jobdetail"> <refBean= "Jobtask"/> </ Property> <!--cron-expression - < Propertyname= "Cronexpression"> <value>${trigger.cron.start_jobtask}</value> </ Property> </Bean> <!--General Management Class if you lazy-init= ' false ' then the container starts executing the scheduler - <BeanID= "TaskManager"Lazy-init= "false"Autowire= "No"class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"> < Propertyname= "Triggers"> <List> <refBean= "Jobtasktrigger"/> </List> </ Property> </Bean></Beans>
View Code
Job.properties:
# #数据定时下发任务每隔5秒钟时间执行一次trigger. CRON.START_JOBTASK=*/5 * * * *?
View Code
Writing test task classes:Quartzjob.java
Package Com.server.job; /** */Publicclass quartzjob {publicvoid Work () { System.out.println ("task is scheduling");} }
View Code
Write Test class: Testquartz.java
PackageCom.server.quartz;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by JUi on 2016/8/30.*/ Public classTestquartz { Public Static voidMain (string[] arg) {System.out.println ("Testquartz start."); ApplicationContext Context=NewClasspathxmlapplicationcontext ("Classpath:quartz/applicationcontext-job.xml"); //if the Startquertz Bean's lazy-init is set to False in the configuration file, it will not be instantiated//Context.getbean ("TaskManager");System.out.print ("Testquartz end ..."); }}
View Code
Quartz requires jar:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
Simple test case.
Springmvc in the configuration of quartz use, to achieve real-time task scheduling.