Integration of spring and quartz of Scheduled tasks (modified)

Source: Internet
Author: User

Turn: http://www.javaweb1024.com/java/JavaWebzhongji/2015/04/13/548.html

There are two ways to use quartz in spring: the first is that the task class inherits Quartzjobbean, and the second is to define the task class and the method to execute in the configuration file, and the classes and methods are still ordinary classes. Obviously, the second approach is far more flexible than the first.

Packages used:

Quartz-1.8.5.jar

Commons-logging.jar

Spring-core-3.0.5.release.jar

Spring-beans-3.0.5.release.jar

Spring-context-3.0.5.release.jar

Spring-context-support-3.0.5.release.jar

Spring-asm-3.0.5.release.jar

Spring-expression-3.0.5.release.jar

Spring.transaction-3.0.5.release.jar

Spring-web-3.0.5.release.jar

The first way of Java code:

Java code

  1. Package com.ncs.hj;

  2. Import Org.quartz.JobExecutionContext;

  3. Import org.quartz.JobExecutionException;

  4. Import Org.springframework.scheduling.quartz.QuartzJobBean;

  5. Public class Springqtz extends quartzjobbean{

  6. private static int counter = 0;

  7. protected void Executeinternal (Jobexecutioncontext context) throws jobexecutionexception {

  8. System.out.println ();

  9. Long ms = System.currenttimemillis ();

  10. System.out.println ("\t\t" + new Date (ms));

  11. System.out.println (MS);

  12. System.out.println ("(" + counter++ + ")");

  13. string s = (string) context.getmergedjobdatamap (). Get ("service");

  14. System.out.println (s);

  15. System.out.println ();

  16. }

  17. }

The second way of Java code:

Java code

  1. Package com.ncs.hj;

  2. Import Org.quartz.JobExecutionContext;

  3. Import org.quartz.JobExecutionException;

  4. Import Org.springframework.scheduling.quartz.QuartzJobBean;

  5. Import Java.util.Date;

  6. Public class Springqtz {

  7. private static int counter = 0;

  8. protected Void execute () {

  9. Long ms = System.currenttimemillis ();

  10. System.out.println ("\t\t" + new Date (ms));

  11. System.out.println ("(" + counter++ + ")");

  12. }

  13. }

Spring's configuration file:

XML code

  1. <!------------Configure the Scheduler quartz, where Jobdetail is configured in two ways-------------->

  2. <!--method One: Using Jobdetailbean, the task class must implement the job interface--

  3. <bean id="Myjob" class="Org.springframework.scheduling.quartz.JobDetailBean" >

  4. <property name= "name" value="Examplejob" ></property>

  5. <property name="Jobclass" value="Com.ncs.hj.SpringQtz" ></property>

  6. <property name="Jobdataasmap" >

  7. <map>

  8. <entry key="service" ><value>simple is the beat</value></entry>

  9. </map>

  10. </property>

  11. </bean>

  12. <!--run, please comment out the way! -

  13. <!--way two: Using Methodinvokingjobdetailfactorybean, the task class can not implement the job interface, by specifying the calling method by Targetmethod- -

  14. <!--define methods in target beans and beans---

  15. <bean id="Springqtzjob" class="Com.ncs.hj.SpringQtz"/>

  16. <bean id="Springqtzjobmethod" class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">

  17. <property name="TargetObject" >

  18. <ref bean="Springqtzjob"/>

  19. </property>

  20. <property name="Targetmethod" > <!--the method name to execute--

  21. <value>execute</value>

  22. </property>

  23. </bean>

  24. <!--======================== Dispatch trigger ========================--

  25. <bean id="Crontriggerbean" class="Org.springframework.scheduling.quartz.CronTriggerBean" >

  26. <property name="Jobdetail" ref="Springqtzjobmethod" ></property>

  27. <property name="cronexpression" value="0/5 * * * *?" ></property>

  28. </bean>

  29. <!--======================== Dispatch factory ========================--

  30. <bean id="Springjobschedulerfactorybean" class=" Org.springframework.scheduling.quartz.SchedulerFactoryBean ">

  31. <property name="triggers" >

  32. <list>

  33. <ref bean="Crontriggerbean"/>

  34. </list>

  35. </property>

  36. </bean>

If the newspaper caused By:java.lang.IncompatibleClassChangeError:class Org.springframework.scheduling.quartz.CronTriggerBean Has interface Org.quartz.CronTrigger as Super class error description you are spring 3 + Quartz 2.x

Spring 3 is supported quartz 2.x
The second of the above example, just change the Crontriggerbean to Crontriggerfactorybean

Java code

    1. <!--======================== Dispatch trigger ========================--

    2. <bean id= "Crontriggerbean" class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" >

    3. <property name= "Jobdetail" ref= "Springqtzjobmethod" ></property>

    4. <property name= "cronexpression" value= "0/5 * * * *?" ></property>

    5. </bean>


About the cronexpression expression, here's a little bit of explanation:
Special characters allowed for field allowed values
Seconds 0-59,-*/
Sub 0-59,-*/
Hours 0-23,-*/
Date 1-31,-*? /L W C
Month 1-12 or JAN-DEC,-*/
Week 1-7 or Sun-sat,-*? /L C #
Year (optional) leave blank, 1970-2099,-*/
Meaning of an expression
"0 0 12 * *?" trigger 12 o'clock noon every day.
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Daily 10:15 Trigger
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily
"0 0/5 14 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily
"0 0/5 14,18 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55
"0 0-5 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 daily
"0 10,44 14?" 3 WED "2:10 and 2:44 triggers in Wednesday of every March
"0 15 10?" * Mon-fri "Monday to Friday 10:15 trigger
"0 15 10 15 *?" 15th 10:15 per month
"0 L *?" 10:15 on the last day of the month
"0 15 10?" * 6L "Last month of Friday 10:15 Trigger
"0 15 10?" * 6L 2002-2005 "2002 to 2005 the last of the monthly Friday 10:15 trigger
"0 15 10?" * 6#3 "Monthly third Friday 10:15 trigger
Every morning at 6.
0 6 * * *
Every two hours
0 */2 * * *
Every two hours between 11 o'clock and 8 in the morning, eight in the morning.
0 23-7/2,8 * * *
Every month, number 4th and Monday to Sunday, three a.m., 11.
0 11 4 * 1-3
January 1 morning, 4.
0 4 1 1 *
Finally, don't forget to configure Spring in Web. xml:

XML code

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <web-app xmlns="Http://java.sun.com/xml/ns/javaee"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee

  5. Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "

  6. version="2.5" >

  7. <welcome-file-list>

  8. <welcome-file>index.html</welcome-file>

  9. </welcome-file-list>

  10. <context-param>

  11. <param-name>contextconfiglocation</param-name>

  12. <param-value>/web-inf/spring-config.xml</param-value>

  13. </context-param>

  14. <listener>

  15. <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>

  16. </listener>

  17. </web-app>


Operation Result:
Wed Feb 13:58:30 CST 2012
(0)
Wed Feb 13:58:35 CST 2012
(1)
Wed Feb 13:58:40 CST 2012
(2)
Wed Feb 13:58:45 CST 2012
(3)
Wed Feb 13:58:50 CST 2012
(4)
Wed Feb 13:58:55 CST 2012
(5)
Wed Feb 13:59:00 CST 2012
(6)

QQ Group: Focus on Javaweb Development official group (178744906)validation message: JavaWeb1024

Integration of spring and quartz of Scheduled tasks (modified)

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.