Using quartz2.1.6+spring3.0 to implement a simple example of task scheduling _ timed task scheduling

Source: Internet
Author: User

According to the guidance of netizens, especially the analysis of Lk_well friends:

Spring3.2.4 just started supporting quart-2.x.

SPRING3.0 still does not support quartz2.x.
The example of the landlord refers to the Myeclispe Spring 3.0 Misc Libray, which contains Com.springsource.org.quartz-1.6.x.jar
In fact, the landlord's example is still used Quartz1.x.jar to run successfully.

Lk_well's blog also gives the right code: Bowen Address (Spring-3.2.4 + Quartz-2.2.0 Integration Instance)

Please refer to his article to do the test, thank you.

Environment: myeclipse10+jdk1.6+spring3.0 (MyEclipse) +quartz2.1.6+tomcat7

Because task scheduling is used in the project, configuring the scheduling rule is inflexible if you use spring's own timer. Head decided to use quartz, sent two links for my reference, from the two links to Quartz have a preliminary understanding, but those two links are for 1.6 version, 1.8 version after a big change, including the initialization method has changed. Reference to the online article and read the official document debugging for nearly two days still unable to tune, the official documents are not very similar, the sample is not rich.

Later, my friend gave me a remote debugging, the problem solved.

The following issues have been encountered during the resolution:

1.web.xml about the order s of the spring configuration file configuration, spring's profile location formulation must precede other spring other configurations.

2. The lack of Commons-collections-3.2.jar is due to the use of a class below org/apache/commons/collections, which is not in my project. This should be from the Internet.

3. Applicationcontext.xml about Job configuration, some online articles, the actual work class is inherited from a class of spring, this class implements the Quartz job interface, but according to that writing, and the corresponding configuration file, can not work, and later used a friend to the method, the work class is ordinary Java class, in the configuration file to develop a specific method, the configuration file will be posted later.

Steps:

1: Create a Web project in MyEclipse, after the creation of the project on the right button, MyEclipse, add Spring capabilities, select the package to add, I took Spring words are selected, in fact, not so much.

2: Copy Download Quartz-2.16 extract all the jar packages in the Quartz-all-2.1.6.jar and Lib directories in the root directory, right-click on the item, paste, and add the same method Commons-collections-3.2.jar to the project. Then right-click on the item, properties, Java build Path,add JARs, select the jar packages that you just added, and reference the jar packages to the project. (You can also add external to the Java build path interface without copying the jar packages to the project, but I usually do this for easy replication of the project)

3. The class for writing the work. The code is simple:

/*
* @author Sixi
* @version 0.1
* Company:tsinghua
* DATE:2012-09-15
* Description: This is a specific class that executes the schedule, specifying the work method of calling this class in the spring configuration file
*  */
Package com.tsinghua.test;

Import Java.util.Date;

/*
* Specific classes to perform task scheduling using Spring+quartz
*  */
public class Myjob {

/*
* Description: The way to do this is to output the current time to the console only.
* The log entered in:%tomcatroot%\logs\tomcat7-stdout.yyyy-mm-dd.log,
* Among them, YYYY-MM-DD is the date of deployment, the trial found that the default is not to generate a stdout log file every day
* @return return void
*  */
public void work ()
{
System.out.println ("Current time:" + new Date (). toString ());
}
}//End of Myjob

4. Configuration web.xml, the specific configuration file is as follows:

? XML version= "1.0" encoding= "UTF-8"?>

<web-app version= "3.0" xmlns= Http://java.sun.com/xml/ns/javaee "xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "Xsi:schemalocation http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >
<display-name>SpringQuartzDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!--says: Spring's profile settings must not be reported until the spring bean Factory monitoring is inspired. NET configuration file does not seem to be preceded by a sequential-->

<!--spring configuration file starts-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!--spring configuration file-->

<!--start Spring Bean factory monitoring start-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--start Spring Bean Factory monitor End-->
</web-app>

5. Configuration 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" xmlns:p = "http://www.springframework.org/schema/p"
Xsi:schemalocation = "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">

Bean--> for <!--work
< Bean id = "Myjob" class = "Com.tsinghua.test.MyJob"/>

<!--job configuration starts-->
< Bean id = "Myjobdetail"
class = "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
< property name = "TargetObject" >
< ref bean = "Myjob"/>
</Property >
< property name = "Targetmethod" >
< value >work </value >
</Property >
</Bean >
<!--job configuration is over-->


<!--the configuration of the dispatch began-->
< Bean id = "Crontestjobtrigger" class = "Org.springframework.scheduling.quartz.CronTriggerBean" >
< property name = "Jobdetail" >
< ref bean = "Myjobdetail"/>
</Property >
< property name = "Cronexpression" >
< value &GT;0/1 * * * *? </value >
</Property >
</Bean >
<!--configuration end of dispatch-->


<!--start the configuration of the trigger--> <!--General Management class if lazy-init= ' false ' then the container is started and the scheduler executes-->
< bean name = "Startquertz" Lazy-init = "false" Autowire = "No"
class = "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
< property name = "Triggers" >
< list >
< ref bean = "Crontestjobtrigger"/>

</list >
</Property >
</Bean >
<!--start the configuration end of the trigger-->
</Beans >

6. In Tomcat debugging, you can see the output in the console window

Project Code:

Springquartzdemo

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.