Simple Example of task scheduling using quartz2.1.6 + spring3.0

Source: Internet
Author: User

Environment: myeclipse10 + jdk1.6 + spring3.0 (included in myeclipse) + quartz2.1.6 + tomcat7

Task Scheduling is required in the project. If you use the timer provided by spring, the scheduling rules are not flexible enough. Headers decided to use quartz and sent two links for my reference. The two links have a preliminary understanding of quartz, but the two links are for version 1.6, version 1.8 has undergone major changes, including the initialization method. Refer toArticleI have read the official documentation and debugged it for nearly two days. The official documentation is not very similar and there are not many examples.

 

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

The following problems have been solved:

1. For the sequence s of spring configuration file configuration in Web. XML, the spring configuration file location must be set before other spring configurations.

2. The commons-collections-3.2.jar is missing because it will be used Org/ Apache / Commons / Collections A class below, but not in my project. This is going to be downloaded from the Internet.

3. applicationcontext. in some articles on the Internet about job configuration in XML, the actual work class inherits from a certain class of spring. This class implements the job interface of quartz, but according to that method, and the corresponding configuration file, can not work, and later used a friend to give the method, the work class is a common Java class, in the configuration file to develop a specific method, the configuration file will be attached later.

 

Steps:

1: create a web project in myeclipse. After the project is created, right-click the project, select myeclipse, add spring capabilities, and select the package to be added. I have selected all the packages with spring, in fact, not so much.

2: Copy download quartz-2.16 unzip all the jar packages in the quartz-all-2.1.6.jar and lib directory in the root directory, right-click on the project, paste, the same method and then add the commons-collections-3.2.jar to the project, right-click the project, select Properties, Java build path, add jars, select the jar packages you just added, and reference these jar packages to the project. (You can also not copy these jar packages to the project, but directly add external packages to the Java build path interface, but I usually do this to facilitate project replication)

3. writing class.CodeSimple:

 

/*
* @ Author sixi
* @ Version 0.1
* Company: Tsinghua
* Date: 2012-09-15
* Description: This is a specific class for executing scheduling. In the spring configuration file, specify to call this class of work method.
* */
Package Com. Tsinghua. test;

ImportJava. util. date;

/*
* Use spring + quartz to execute task scheduling classes
**/
Public ClassMyjob {

/*
* Description: Method of work. This method only outputs the current time to the console,
* The entered log is in: % atatroot % \ logs \ tomcat7-stdout.yyyy-MM-dd.log,
* Yyyy-mm-dd indicates the deployment date. By default, the test shows that a stdout log file is not generated every day.
* @ Return returns void
**/
Public VoidWork ()
{
System. Out. println ("current time:" +NewDate (). tostring ());
}
}//End of myjob

 

4. Configure 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 >

<! --Note: The configuration file of spring must be set before the spring bean worker can listen. Otherwise, the configuration file of. Net does not appear to be sorted first or later.-->

<! -- Start of spring configuration file -->
< Context-Param >
< Param-name > Contextconfiglocation</ Param-name >
< Param-Value > /WEB-INF/classes/applicationcontext. xml </ Param-Value >
</ Context-Param >
<! -- End of spring configuration file -->

<! -- Start spring Bean Factory listening -->
< Listener >
< Listener-class > Org. springframework. Web. Context. contextloaderlistener </ Listener-class >
</ Listener >
<! -- End of spring Bean Factory listener startup -->
</ Web-app >

 

5. Configure 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" >

<! --Working Bean-->
<BeanID= "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 >
<! -- The job configuration is complete. -->

<! -- Scheduling configuration starts -->
< Bean ID = "Crontestjobtrigger" Class = "Org. springframework. Scheduling. Quartz. crontriggerbean" >
< Property Name = "Jobdetail" >
< Ref Bean = "Myjobdetail"   />
</ Property >
< Property Name = "Cronexpression" >
< Value > 0/1 ****? </ Value >
</ Property >
</ Bean >
<! -- The scheduling configuration is complete. -->

<! -- Start trigger Configuration -->
< Bean Name = "Startquertz" Lazy-init = "False" Autowire = "No"
Class = "Org. springframework. Scheduling. Quartz. schedulerfactorybean" >
< Property Name = "Triggers" >
< List >
< Ref Bean = "Crontestjobtrigger"   />

</ List >
</ Property >
</ Bean >
<! -- The trigger configuration is complete. -->
</ Beans >

 

6. debug Tomcat and you can see the output in the console window.

 

Project code:

Springquartzdemo

 

PS: Why is there always a problem when code snippets are inserted in the blog garden today? Either the top line of the Code or the bottom line of the Code will go out of the code snippet.

 

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.