Catalogue [-]
- One, Spring creates jobdetail two ways
- Ii. Integration Method An example step
- 1. Import the spring core jar packages, Quartz.jar, and Spring-context-support.jar into the classpath.
- 2, write the Job class Punchjob (the class must inherit Quartzjobbean)
- 3. Write Quartz.xml configuration file
- 4. Let the container load Quartz.xml
- 5, configure the Quartz environment: Quartz.properties file (placed under the classpath)
- Third, the matters needing attention
One, Spring creates jobdetail two ways
Timing task two ways, Spring good package use quartz details, the first way is to use the Spring package Quartz class for the implementation of specific methods, the second is through the transparent use of quartz to achieve the purpose of timing task development, generally speaking the second kind of developers more convenient!
Configuring spring's task Scheduling abstraction layer simplifies task scheduling and provides better scheduling objects on a quartz basis. Spring uses the quartz framework to complete task scheduling and create Quartz's job bean (Jobdetail) in two ways:
1: Use Jobdetailbean to wrap an instance of the Quartzjobbean subclass (that is, the job class).
2: Use the Methodinvokingjobdetailfactorybean factory bean to wrap generic Java objects (that is, the job class).
Description
1: Use the first method to create the job class, you must inherit Quartzjobbean, implement Executeinternal (Jobexecutioncontext
Jobexecutioncontext) method, which is the executing body of the scheduled task, and then configures the instance of the job class directly into the Jobdetailbean. This method is the same as in normal quartz programming.
2: Use the second method to create the job class, without inheriting the parent class, directly configure the Methodinvokingjobdetailfactorybean. But you need to specify two properties:
TargetObject: Specifies the bean instance that contains the task execution body.
Targetmethod: Specifies that the method of the specified bean instance is wrapped into the execution body of the task.
Ii. Integration Method An example step
1. Import the spring core jar packages, Quartz.jar, and Spring-context-support.jar into the classpath.
Do not forget to import Spring-context-support-3.2.0.m2.jar: This is because this approach is implemented using the spring-encapsulated quartz class for a specific method.
The two JobDetail:org.springframework.scheduling.quartz.JobDetailBean we used. and Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
Trigger: Org.springframework.scheduling.quartz.CronTriggerBean The Scheduler: Org.springframework.scheduling.quartz.SchedulerFactoryBean is sourced from this jar package.
2, write the Job class Punchjob (the class must inherit Quartzjobbean)
[Java] View plain copy
- Package org.crazyit.hrsystem.schedule;
- Import Java.util.Date;
- Import Org.springframework.scheduling.quartz.QuartzJobBean;
- Import Org.quartz.JobExecutionContext;
- Import org.quartz.JobExecutionException;
- Import Org.crazyit.hrsystem.service.EmpManager;
- public class Punchjob
- Extends Quartzjobbean
- {
- Flag that determines whether the job is executed
- Private Boolean isrunning = false;
- The business logic component on which the job class depends
- Private Empmanager empmgr;
- public void Setempmgr (Empmanager empmgr)
- {
- This.empmgr = Empmgr;
- }
- Define task Execution Body
- public void Executeinternal (Jobexecutioncontext ctx)
- Throws Jobexecutionexception
- {
- if (!isrunning)
- {
- System.out.println ("Start automatic clock-out");
- IsRunning = true;
- Invoking the business logic method
- Empmgr.autopunch ();
- IsRunning = false;
- }
- }
- }
3. Write Quartz.xml configuration file
[HTML] View plain copy
- <?xml version= "1.0" encoding= "GBK"?>
- <!--Specify the schema information for the spring configuration file--
- <beans xmlns= "Http://www.springframework.org/schema/beans"
- Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
- xmlns:p= "http://www.springframework.org/schema/p"
- 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.0.xsd
- Http://www.springframework.org/schema/tx
- Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- Http://www.springframework.org/schema/aop
- Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "
- Default-lazy-init= "false" >
- <!--definition <span style= "font-family: ' Courier New ', ' Courier ';" > A </span> business logic component that inherits the template of a business logic component--
- <bean id= "Empmanager"
- Class= "Org.crazyit.hrsystem.service.impl.EmpManagerImpl"
- parent= "Managertemplate"/>
- <!--define triggers to manage task beans--
- <bean id= "Crontriggerpunch"
- class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
- <property name= "Jobdetail" >
- <!--define a task bean in a way that uses nested beans--
- <bean
- class= "Org.springframework.scheduling.quartz.JobDetailBean" >
- <!--the implementation class for the specified task bean--
- <property name= "Jobclass"
- Value= "Org.crazyit.hrsystem.schedule.PunchJob"/>
- <!--inject properties into the task bean--
- <property name= "Jobdataasmap" >
- <map>
- <entry key= "Empmgr" value-ref= "Empmanager"/>
- </map>
- </property>
- </bean>
- </property>
- <!--specify cron expression: Monday to Friday 7 o'clock, 12-point schedule--
- <property name= "Cronexpression"
- Value= "0 0 7,12? * Mon-fri "/>
- </bean>
- <!--perform the actual scheduler--
- <bean
- class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
- <property name= "Triggers" >
- <list>
- <ref bean= "Crontriggerpunch" ></ref>
- <!--<ref local= "Crontriggerpunch"/> Both can be used--
- </list>
- </property>
- </bean>
- </beans>
Job Data Map (JOBDATAASMAP) can be obtained through jobexecutioncontext (execution-time delivery). Jobdetailbean map the properties of the job data map to the job's properties. As the example shows, if the job class Punchjob contains a empmgr attribute, Jobdetailbean is automatically injected into the instance of the job class Punchjob and can be used to pass parameters. If it is not stated, it will be reported
Java.lang.NullPointerException error, mainly because no bean was injected.
In the above configuration we let the trigger and the task nesting, in fact, we can also separate them, shape such as:
<!--define Jobdetail beans--
<bean id= "Saveprojectjob"
class= "Org.springframework.scheduling.quartz.JobDetailBean" >
<!--define the job's bean--
<property name= "Jobclass" >
<value>
Com.gresoft.fileupload.service.ParseFileQuartz
</value>
</property>
<!--define the other beans referenced in the job's bean--
<property name= "Jobdataasmap" >
<map>
<entry key= "Readxmlservice" >
<ref bean= "Readxmlservice"/>
</entry>
</map>
</property>
</bean>
<!--------------------------------------------------------------->
<!--The bean that defines the trigger--
<bean id= "Savecron"
class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<!--designation Jobdetail --
<property name= "Jobdetail" >
<!--<ref bean= "Saveprojectjob" ></ref> both can be used--
<ref local= "Saveprojectjob"/>
</property>
<!--Specify the time that the task fires--
<property name= "Cronexpression" >
<VALUE>0/30 * * * *?</value>
</property>
</bean>
4. Let the container load Quartz.xml
Add in Web. xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/web-inf/applicationcontext.xml,/web-inf/quartz.xml</param-value>
</context-param>
# # # #其实quartz the contents of the. xml file can be written in applicationcontext.xml, but it will appear cluttered.
5, configure the operating environment of the quartz:
quartz.propertiesFile (placed under Classpath)
File name must be called this name, in fact, this file we can also not configure.
[HTML] View plain copy
The
- # configures the main scheduler property
- Org.quartz.scheduler.instancename = quartzscheduler
- org.quartz.scheduler.instanceid = auto
- # Configuring the thread pool
- # quartz thread pool implementation class
- org.quartz.threadpool.class = org.quartz.simpl.simplethreadpool
- the number of threads in the # thread pool
- org.quartz.threadpool.threadcount = 1
- # Line constructor thread priority
- org.quartz.threadpool.threadpriority = 10
- # Configure job store
- org.quartz.jobstore.misfirethreshold = 60000
- org.quartz.jobstore.class = org.quartz.simpl.ramjobstore
If we do not configure this file, the default is to use the Quartz.properties file in Quartz-2.1.6.jar (under the Org/quartz path of the compressed file), if you need to change its run properties, We can create a quartz.properties file ourselves and place the file under the classpath of the system load, and ClassLoader will automatically load and enable the various properties.
Third, the matters needing attention
Two points to note when you configure and quartz integrated content in spring
1, in <Beans> can not set default-lazy-init= "true", otherwise the scheduled task does not trigger, if not explicitly indicate the value of Default-lazy-init, the default is False.
2, in <Beans> can not set the Default-autowire= "ByName" Properties, Otherwise the org.springframework.beans.factory.BeanCreationException error will be reported in the background, so that it cannot be injected automatically through the bean name and must be injected by explicit reference
Original address: http://blog.csdn.net/yuanlaishini2010/article/details/8874991
Spring Integrated Quartz