Spring Integrated Quartz

Source: Internet
Author: User

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
  1. Package org.crazyit.hrsystem.schedule;
  2. Import Java.util.Date;
  3. Import Org.springframework.scheduling.quartz.QuartzJobBean;
  4. Import Org.quartz.JobExecutionContext;
  5. Import org.quartz.JobExecutionException;
  6. Import Org.crazyit.hrsystem.service.EmpManager;
  7. public class Punchjob
  8. Extends Quartzjobbean
  9. {
  10. Flag that determines whether the job is executed
  11. Private Boolean isrunning = false;
  12. The business logic component on which the job class depends
  13. Private Empmanager empmgr;
  14. public void Setempmgr (Empmanager empmgr)
  15. {
  16. This.empmgr = Empmgr;
  17. }
  18. Define task Execution Body
  19. public void Executeinternal (Jobexecutioncontext ctx)
  20. Throws Jobexecutionexception
  21. {
  22. if (!isrunning)
  23. {
  24. System.out.println ("Start automatic clock-out");
  25. IsRunning = true;
  26. Invoking the business logic method
  27. Empmgr.autopunch ();
  28. IsRunning = false;
  29. }
  30. }
  31. }

3. Write Quartz.xml configuration file

[HTML] View plain copy
  1. <?xml version= "1.0" encoding= "GBK"?>
  2. <!--Specify the schema information for the spring configuration file--
  3. <beans xmlns= "Http://www.springframework.org/schema/beans"
  4. Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
  6. xmlns:p= "http://www.springframework.org/schema/p"
  7. xmlns:tx= "Http://www.springframework.org/schema/tx"
  8. Xsi:schemalocation= "Http://www.springframework.org/schema/beans
  9. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. Http://www.springframework.org/schema/tx
  11. Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  12. Http://www.springframework.org/schema/aop
  13. Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "
  14. Default-lazy-init= "false" >
  15. <!--definition <span style= "font-family: ' Courier New ', ' Courier ';" > A </span> business logic component that inherits the template of a business logic component--
  16. <bean id= "Empmanager"
  17. Class= "Org.crazyit.hrsystem.service.impl.EmpManagerImpl"
  18. parent= "Managertemplate"/>
  19. <!--define triggers to manage task beans--
  20. <bean id= "Crontriggerpunch"
  21. class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
  22. <property name= "Jobdetail" >
  23. <!--define a task bean in a way that uses nested beans--
  24. <bean
  25. class= "Org.springframework.scheduling.quartz.JobDetailBean" >
  26. <!--the implementation class for the specified task bean--
  27. <property name= "Jobclass"
  28. Value= "Org.crazyit.hrsystem.schedule.PunchJob"/>
  29. <!--inject properties into the task bean--
  30. <property name= "Jobdataasmap" >
  31. <map>
  32. <entry key= "Empmgr" value-ref= "Empmanager"/>
  33. </map>
  34. </property>
  35. </bean>
  36. </property>
  37. <!--specify cron expression: Monday to Friday 7 o'clock, 12-point schedule--
  38. <property name= "Cronexpression"
  39. Value= "0 0 7,12? * Mon-fri "/>
  40. </bean>
  41. <!--perform the actual scheduler--
  42. <bean
  43. class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
  44. <property name= "Triggers" >
  45. <list>
  46. <ref bean= "Crontriggerpunch" ></ref>
  47. <!--<ref local= "Crontriggerpunch"/> Both can be used--
  48. </list>
  49. </property>
  50. </bean>
  51. </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
    1. #  configures the main scheduler property   
    2. Org.quartz.scheduler.instancename = quartzscheduler   
    3. org.quartz.scheduler.instanceid  = auto  
    4. #  Configuring the thread pool   
    5. # quartz thread pool implementation class   
    6. org.quartz.threadpool.class =  org.quartz.simpl.simplethreadpool  
    7. the number of threads in the #  thread pool   
    8. org.quartz.threadpool.threadcount = 1  
    9. #  Line constructor thread priority   
    10. org.quartz.threadpool.threadpriority = 10  
    11. #  Configure job store   
    12. org.quartz.jobstore.misfirethreshold = 60000  
    13. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.