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, write a test-dispatched class: (Quartzhelloworldjob.java)
Package Com.jp.quartz; Public class quartzhelloworldjob { publicvoid dotask () { System.out.println ( "------HelloWorld Task Scheduler!!! "); } }
Spring configuration file:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd"><Beans><!--The work class to invoke-<BeanId= "Quartzjob"Class= "Com.jp.quartz.QuartzHelloWorldJob"></Bean><!--Define methods for calling and invoking objects-<BeanId= "Jobtask"Class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!--The class that is called-<PropertyName= "TargetObject"><RefBean= "Quartzjob"/></Property><!--Calling methods in a class-<PropertyName= "Targetmethod"><Value>dotask</Value></Property></Bean><!--Define Trigger Time-<BeanId= "Dotime"Class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean"><PropertyName= "Jobdetail"><RefBean= "Jobtask"/></Property><!--Cron-expression-<PropertyName= "Cronexpression"><!--0:15 per day execution of procedures-<Value>0 15 0 * *?</Value></Property></Bean><!--General Management Class if you lazy-init= ' false ' then the container starts executing the scheduler-<BeanId= "Startquertz"Lazy-init= "false"Autowire= "No"Class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"><Propertyname= "triggers" > Span style= "color: #0000ff;" ><list> << Span style= "color: #800000;" >ref bean= "Dotime" / > </list> Span style= "color: #0000ff;" ></property> bean> </< Span style= "color: #800000;" >beans>
Test procedure:
PackageCom.jp.quartz;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classmaintest{/** * @paramargs*/ Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring-quartz-config.xml"); //if the Startquertz Bean's lazy-init is set to False in the configuration file, it will not be instantiated//Context.getbean ("Startquertz"); } }
Finally, put the log4j.properties in the SRC directory, start the main class can be tested.
About cron expressions (from the network):
The Cron expression consists of the following 7 fields:
- Seconds
- Score of
- Hours
- Date within the month
- Month
- Week Date
- Year (optional field)
Configuration of quartz in spring