The quartz Timer is used in project development. It feels like the Timer class in javaJDK. However, the difference is that the Timer provided by spring can be triggered at a fixed time every day, or at other times. The trigger time is user-friendly.
I wrote a small project and practiced it myself. The development of Spring quartz timer requires spring-context.jar and quartz. jar package, I initially did not import quartz. jar package, resulting in an error.
The maven dependency of quartz is as follows:
[Html]
<Span style = "font-size: 24px;"> <dependency>
<GroupId> org. opensymphony. quartz </groupId>
<ArtifactId> quartz-all </artifactId>
<Version> 1.6.1 </version>
</Dependency> </span>
The timer class does not need to inherit from any class.
[Java]
<Span style = "font-size: 24px;"> package com. jd. service. meeting. worker;
Public class MyWorker {
Public void running (){
System. out. println ("hello, world! ");
}
}
</Span>
Configure the timer in the spring configuration file.
[Html]
<Span style = "font-size: 24px;"> <? Xml version = "1.0" encoding = "GBK"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
Default-autowire = "byName">
<Bean id = "helloWorker" class = "com. jd. service. meeting. worker. MyWorker"> </bean>
<Bean id = "startQuertz" lazy-init = "false" autowire = "no"
Class = "org. springframework. scheduling. quartz. SchedulerFactoryBean">
<Property name = "triggers">
<List>
<Bean class = "org. springframework. scheduling. quartz. CronTriggerBean">
<Property name = "jobDetail">
<Bean
Class = "org. springframework. scheduling. quartz. MethodInvokingJobDetailFactoryBean">
<Property name = "concurrent">
<Value> false </value>
</Property>
<Property name = "targetObject">
<Ref bean = "helloWorker"/>
</Property>
<Property name = "targetMethod">
<Value> running </value>
</Property>
</Bean>
</Property>
<Property name = "cronExpression">
<Value> 0/10 ****? </Value>
</Property>
</Bean>
</List>
</Property>
</Bean>
</Beans>
</Span>
This time expression specifies the trigger time rule of this timer. I specify to run every 10 seconds.
[Html]
<Span style = "font-size: 24px;"> property name = "cronExpression">
<Value> 0/10 ****? </Value>
</Property> </span>
My program runs locally. You can configure the jar package.