Quartz+spring instance Application

Source: Internet
Author: User

After a night of research, we finally made an example of Spring+quartz.

1. JavaBean class
In the Test.quartzjob

Package Test;

public class Quartzjob {
public void work ()
{
SYSTEM.OUT.PRINTLN ("Quartz Task Scheduler ... ");
}
}

2. Set the XML for spring
I have created a new applicationcontext_quartz.xml, which reads as follows:

<?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/beans http://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 work class to invoke--
<bean id= "Quartzjob" class= "Test.quartzjob" ></bean>
<!--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>
<!--define trigger time-
<bean id= "Dotime" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<property name= "Jobdetail" >
<ref bean= "Jobtask"/>
</property>
<!--cron Expressions--
<property name= "Cronexpression" >
<!--run once every 10 seconds-
&LT;VALUE&GT;0/10 * * * *?</value>
</property>
</bean>
<!--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"/>
</list>
</property>
</bean>

</beans>

3. Set Web. XML to Spring Load

<!--loading 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 Download http://download.csdn.net/source/1026766
2. Import Spring.jar (I used a previous version of spring2.0) to download the http://download.csdn.net/source/1041819
If you don't import it,
Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
Org.springframework.scheduling.quartz.CronTriggerBean
Org.springframework.scheduling.quartz.SchedulerFactoryBean
Spring does not find these three classes, spring does not load properly, because the first use of quartz, I was then ignored this point, wasted a lot of time to tune the configuration, in fact, there is no error in configuration.
3. You'll ask why it's not necessary to use spring
Quartz.properties
Quartz-jobsxml
These two files.
I replied: Yes, it will automatically find the default two files in the quartz package, and if you have special needs, you can define these two files yourself.

Attached: quartz.properties
#
# Configure Main Scheduler Properties
#

Org.quartz.scheduler.instanceName = Testscheduler
Org.quartz.scheduler.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

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.