Spring combined with quartz for multi-tasking timed calls

Source: Internet
Author: User
Tags java se

http://blog.csdn.net/oracle_microsoft/article/details/4412502

The Quartz framework provides rich task scheduling support, such as when to perform what tasks, which is an open source, Opensymphony-maintained project that developers can use in Java EE, or in a separate Java SE application. Whether it's a simple task Scheduler or a complex enterprise application, quartz is well-suited to the job. Among them, these tasks can be ordinary pojo, and can even be EJB 3.0 components.

Quartz is the ideal choice if developers need to develop applications such as the following.
Drive the workflow: for example, if the newly created process task needs to be processed within 2 hours, quartz will check whether the order was processed successfully after 2 hours. If not, quartz will process the order according to the rules defined by the workflow, destroy it, or perform other processing.
System maintenance: For example, export the contents of an RDBMS to an XML file at a fixed time of day.
Spring 2.0 provides the Org.springframework.scheduling.quartz package to support quartz task scheduling integration. To provide the user's task, the developer-implemented class must inherit from the quartz. Quartzjobbean abstract class. Quartzjobbean is a simple implementation (subclass) of the Org.quartz.Job interface in quartz, and Spring provides a Quartzjobbean class for simplifying the implementation of the job interface. Quartzjobbean is similar to TimerTask in the Java 2 SDK to define the task itself. where the Executeinternal () method defines the task to be performed, which is similar to run () in TimerTask.
To specify more complex task scheduling rules, developers use the spring-provided Crontriggerbean, which is a subclass of Org.quartz.CronTrigger in quartz, and spring 2.0 provides a crontriggerbean for simplifying the development of Crontrigger subclasses. Crontriggerbean is more powerful than Simpletriggerbean, which controls the exact time of task execution, for example, 9:30 A.M. needs to perform a given task in a quartzjobbean. By using the Cronexpression attribute in Crontriggerbean, you can set the timing of the task execution.
Quartz is a powerful enterprise-level task scheduling framework that inherits and simplifies quartz in spring, and looks at how quartz is configured in spring: First we write two scheduled classes quartzjob, and Quartzjobtwo, more than two similar:
Package Com.writchie.quartz;
public class Quartzjob
{
public void work ()
{
System.out.println ("Wulichi prompts you: Spring Quartz task Dispatch 1 is called! ");
Business logic for timed calls
}
}

Package Com.writchie.quartz;
public class Quartzjobtwo
{
public void work ()
{
System.out.println ("Wulichi prompts you: Spring Quartz task Dispatch 2 is called! ");
Business logic for timed calls
}
}

Spring's configuration file Applicationcontext.xml:
<?xml version= "1.0" encoding= "UTF-8"?>
<beans
Xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--the work class to invoke--
<bean id= "Quartzjob" class= "Com.writchie.quartz.QuartzJob" ></bean>
<bean id= "Quartzjobtwo" class= "Com.writchie.quartz.QuartzJobTwo" ></bean>
<!--can continue adding new tasks--
<!--end of work class to invoke

<!--define methods for calling objects and calling objects--
<bean id= "Jobtask" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<!--called Class--
<property name= "TargetObject" >
<ref bean= "Quartzjob"/>
</property>
<!--calling methods in class--
<property name= "Targetmethod" >
<value>work</value>
</property>
</bean>

<bean id= "Jobtask2" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<!--called Class--
<property name= "TargetObject" >
<ref bean= "Quartzjobtwo"/>
</property>
<!--calling methods in class--
<property name= "Targetmethod" >
<value>work</value>
</property>
</bean>
<!--can continue adding new--
<!--define the call object and the method to call the object ends--

<!--define trigger time-
<bean id= "Dotime" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<property name= "Jobdetail" >
<ref bean= "Jobtask"/>
</property>
<!--cron expressions are defined here to always trigger the execution of tasks--
<property name= "Cronexpression" >
&LT;VALUE&GT;10,15,20,25,30,35,40,45,50,55 * * * *?</value>
</property>
</bean>

<bean id= "doTime2" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<property name= "Jobdetail" >
<ref bean= "Jobtask2"/>
</property>
<!--cron expressions are defined here from Monday to Sunday 13:15 trigger---
<property name= "Cronexpression" >
<value>0 15 13? * sun-sat</value>
</property>
</bean>
<!--can continue adding new--
<!--define the end of the trigger time-

<!--general Management class if you lazy-init= ' false ' then the container starts executing the scheduler--
<bean id= "Startquertz" lazy-init= "false" autowire= "no" class= " Org.springframework.scheduling.quartz.SchedulerFactoryBean ">
<property name= "Triggers" >
<list>
<ref bean= "Dotime"/>
<ref bean= "DoTime2"/>
<!--can continue adding new--

</list>
</property>
</bean>
<!--total Management class ended-
</beans>

Test procedure:
Package Com.writchie.quartz;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Quartzservice {
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("Test Task Scheduler starts ...");
ApplicationContext context = new Classpathxmlapplicationcontext (
"Applicationcontext.xml");
If the Startquertz Bean's lazy-init is set to False in the configuration file, it will not be instantiated
Context.getbean ("Startquertz");
System.out.print ("Test task dispatch end!/n");
}
}

Test results:
Test Task Scheduler started ...
-Scheduler defaultquartzscheduler_$_non_clustered started.
The test task is scheduled to end!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 2 is called!
Wulichi prompts you: the Spring Quartz Task Scheduler 1 is called!

About cron Expressions (resources):
The Cron expression consists of the following 7 fields:
    • Seconds
    • Score of
    • Hours
    • Date within the month
    • Month
    • Week Date
    • Year (optional field)
Special Characters
Cron triggers take advantage of a series of special characters, as follows:
    • The backslash (/) character represents the increment value. For example, in the seconds field, "5/15" represents the beginning of the 5th second, once every 15 seconds.
    • The question mark (?) character and the letter L character are available only in the date and week date fields within the month. A question mark indicates that the field does not contain a specific value. Therefore, if you specify a date within a month, you can insert "?" in the Date field within the week, which means that the date value in the week does not matter. The letter L character is the last abbreviation. Placed in the Month Date field, indicates that the schedule is scheduled to be executed on the last day of the month. In the week Date field, if "L" exists alone, it is equal to "7", otherwise it represents the last instance of the week date within the month. So "0L" means that it is scheduled to be executed on the last Sunday of the month.
    • The letter (W) character in the Date field in the month arranges the execution at the working day closest to the specified value. Placing "1W" in the Month Date field indicates that the execution is scheduled for the first business day of the month.
    • The pound sign (#) character specifies a specific weekday instance for a given month. Put "mon#2" in the week Date field, indicating that the task is scheduled for the second Monday of the month.
    • The asterisk (*) character is a wildcard character that indicates that the field can accept any possible value.
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,-*/Expression meaning
"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 *

Spring combined with quartz for multi-tasking timed calls

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.