Spring4 multiple timer details, spring4 timer details

Source: Internet
Author: User

Spring4 multiple timer details, spring4 timer details

Note: spring4 does not have the org. springframework. scheduling. timer. ScheduledTimerTask class, so the timerTask method before spring3 cannot be used. Two methods are recommended for the spring4 timer method: (you need to import the quartz package separately, only 1.8. +)

The spring code is as follows:

<Span style = "white-space: pre"> </span> <! -- Method 1 --> <bean name = "exampleJob" class = "org. springframework. scheduling. quartz. jobDetailBean "> <property name =" jobClass "value =" com. spring. task. taskOne "/> <property name =" jobDataAsMap "> <map> <entry key =" timeout "value =" 5 "/> </map> </property> </bean> <bean id = "cronTrigger" class = "org. springframework. scheduling. quartz. cronTriggerFactoryBean "> <property name =" jobDetail "ref =" exampleJob "/> <! -- Run every morning at 6 AM --> <! -- <Property name = "cronExpression" value = "0 0 6 **? "/> --> <! -- <Property name = "cronExpression" value = "0 0/1 ***? "/> --> <! -- Per minute --> <property name = "cronExpression" value = "0/2 ****? "/> <! -- Per second --> </bean> <! -- Method 2 --> <bean id = "exampleBusinessObject" class = "com. spring. task. taskTwo "/> <bean id =" jobDetail "class =" org. springframework. scheduling. quartz. methodInvokingJobDetailFactoryBean "> <property name =" targetObject "ref =" exampleBusinessObject "/> <property name =" targetMethod "value =" doIt "/> <property name =" concurrent "value = "false"/> </bean> <bean id = "simpleTrigger" class = "org. springframework. scheduling. quart Z. SimpleTriggerFactoryBean "> <! -- See the example of method invoking job above --> <property name = "jobDetail" ref = "jobDetail"/> <! -- 10 seconds --> <property name = "startDelay" value = "5000"/> <! -- Repeat every 50 seconds --> <property name = "repeatInterval" value = "3000"/> </bean> <! -- The general scheduling is used to start the Spring timer --> <bean class = "org. springframework. scheduling. quartz. schedulerFactoryBean "> <property name =" triggers "> <list> <ref bean =" cronTrigger "/> <ref bean =" simpleTrigger "/> </list> </property> </bean>

The JAVA code is as follows (method 1 ):

Package com. spring. task; import org. apache. log4j. logger; import org. quartz. jobExecutionContext; import org. quartz. jobExecutionException; import org. springframework. scheduling. quartz. quartzJobBean; public class TaskOne extends QuartzJobBean {protected static final Logger log = Logger. getLogger (TaskOne. class); private int timeout;/*** Setter called after the ExampleJob is instantiated * with the value from the JobDetailBean (5) */public void setTimeout (int timeout) {this. timeout = timeout;} @ Overrideprotected void executeInternal (JobExecutionContext arg0) throws JobExecutionException {// TODO Auto-generated method stublog.info ("----- scheduled task execution -----");}}


The JAVA code is as follows ( 2 ):


Package com. spring. task; import org. apache. log4j. logger; public class TaskTwo {protected static final Logger log = Logger. getLogger (TaskTwo. class); public void doIt () {log.info ("----- scheduled task execution -----");}}




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.