Simple Example of Spring timer Quartz

Source: Internet
Author: User

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.


 

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.