Spring + quartz example.
1. JavaBean class
In test. quartzjob
Package test;
Public class quartzjob {
Public void work ()
{
System. Out. println ("quartz task scheduling !!! ");
}
}
2. Set spring XML
I created a new applicationcontext_quartz.xml with the following content:
<? 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: Jee = "http://www.springframework.org/schema/jee"
Xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd ">
<! -- The job class to be called -->
<Bean id = "quartzjob" class = "test. quartzjob"> </bean>
<! -- Define the 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>
<! -- Call methods in the class -->
<Property name = "targetmethod">
<Value> Work </value>
</Property>
</Bean>
<! -- Define the trigger time -->
<Bean id = "dotime" class = "org. springframework. Scheduling. Quartz. crontriggerbean">
<Property name = "jobdetail">
<Ref bean = "jobtask"/>
</Property>
<! -- Cron expression -->
<Property name = "cronexpression">
<! -- Execute once every 10 seconds -->
<Value> 0/10 ****? </Value>
</Property>
</Bean>
<! -- In the general management class, if lazy-init = 'false' is set, the container starts and runs the scheduling program. -->
<Bean id = "startquertz" lazy-init = "false" autowire = "no" class = "org. springframework. Scheduling. Quartz. schedulerfactorybean">
<Property name = "triggers">
<List>
<Ref bean = "dotime"/>
</List>
</Property>
</Bean>
</Beans>
3. Set web. XML to load spring
<! -- Load spring -->
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>
/WEB-INF/classes/applicationcontext *. xml
</Param-value>
</Context-param>
<Servlet>
<Servlet-Name> context </servlet-Name>
<Servlet-class>
Org. springframework. Web. Context. contextloaderservlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
Note: 1. Import quartz-all-1.5.2.jar to download http://download.csdn.net/source/1026766
2. Import spring. Jar (I'm using a version earlier than spring2.0) download http://download.csdn.net/source/1041819
If you do not import
Org. springframework. Scheduling. Quartz. methodinvokingjobdetailfactorybean
Org. springframework. Scheduling. Quartz. crontriggerbean
Org. springframework. Scheduling. Quartz. schedulerfactorybean
The three classes in spring cannot be found, and spring cannot be loaded normally. Because quartz was used for the first time, I ignored this point and wasted a lot of time for configuration adjustment, in fact, the configuration is correct.
3. You will ask why you don't need to use spring.
Quartz. Properties
Quartz-jobsxml
Are these two files available?
I replied: Yes, it will automatically find the default two files in the quartz package. If you have special needs, you can also define these two files by yourself.
Appendix: quartz. Properties
#
# Configure main scheduler Properties
#
Org. Quartz. schedame. InstanceName = testscheduler
Org. Quartz. schedid. instanceid = one
#
# Configure threadpool
#
Org. Quartz. threadpool. Class = org. Quartz. simpl. simplethreadpool
Org. Quartz. threadpool. threadcount = 5
Org. Quartz. threadpool. threadpriority = 4
#
# Configure jobstore
#
# Org. Quartz. jobstore. misfirethreshold = 5000
# Org. Quartz. jobstore. Class = org. Quartz. simpl. ramjobstore
#===================================================== ==========================================
# Configure schedulerplugins ======================================== ============
#===================================================== ==========================================
# Org. Quartz. plugin. trigghistory. Class = org. Quartz. plugins. History. loggingtriggerhistoryplugin
# Org. quartz. plugin. trigghistory. triggerfiredmessage = trigger {1 }. {0} fired job {6 }. {5} at: {4, date, HH: mm: SS mm/DD/YYYY}
# Org. quartz. plugin. trigghistory. triggercompletemessage = trigger {1 }. {0} completed firing job {6 }. {5} At {4, date, HH: mm: SS mm/DD/YYYY} with resulting trigger instruction code: {9}
Org. Quartz. plugin. jobinitializer. Class = org. Quartz. plugins. xml. jobinitializationplugin
Org. Quartz. plugin. jobinitializer. filename = quartz_jobs.xml
Org. Quartz. plugin. jobinitializer. overwriteexistingjobs = false
Org. Quartz. plugin. jobinitializer. failonfilenotfound = true
Org. Quartz. plugin. shutdownhook. Class = org. Quartz. plugins. Management. shutdownhookplugin
Org. Quartz. plugin. shutdownhook. cleanshutdown = true
The system will find quartz_jobs.xml Based on the configuration file