Spring squertz scheduled task, springsquertz
Spring squertz is a powerful method for processing scheduled tasks.
1. Required Jar
Quartz-1.8.5.jar
Commons-logging.jar
Spring-core-3.0.5.RELEASE.jar
Spring-beans-3.0.5.RELEASE.jar
Spring-context-3.0.5.RELEASE.jar
Spring-context-support-3.0.5.RELEASE.jar
Spring-asm-3.0.5.RELEASE.jar
Spring-expression-3.0.5.RELEASE.jar
Spring. transaction-3.0.5.RELEASE.jar
Spring-web-3.0.5.RELEASE.jar
2. Configuration File
Configure when to execute your scheduled task and how to execute it, as shown in the following figure: At 23:00:00 January 10, execute com. seewoedu. train. quartz. generateRewardListTask's generate method, where cron is used to specify the execution time, the related syntax can view the http://www.cnblogs.com/0201zcr/p/5995779.html
<? 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: p = "http://www.springframework.org/schema/p" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <! -- Start trigger configuration start --> <bean name = "startQuertz" lazy-init = "false" autowire = "no" class = "org. springframework. scheduling. quartz. schedulerFactoryBean "> <property name =" triggers "> <list> <ref bean =" myJobTrigger "/> </list> </property> </bean> <! -- The trigger configuration ends --> <! -- Quartz-2.x configuration --> <bean id = "myJobTrigger" class = "org. springframework. scheduling. quartz. cronTriggerFactoryBean "> <property name =" jobDetail "> <ref bean =" myJobDetail "/> </property> <property name =" cronExpression "> <! -- <Value> 10 0/1 ***? </Value> --> <! -- Cron expression "10 */1 ***?" Meaning: Execution starts every 10 seconds. --> <Value> 0 0 9 10 12? 2016 </value> <! -- Cron expression "0 0 23 10 11? 2016 "means: it will only be executed at 23:00:00. --> </Property> </bean> <! -- Scheduling configuration ended --> <! -- Start job configuration --> <bean id = "myJobDetail" class = "org. springframework. scheduling. quartz. methodInvokingJobDetailFactoryBean "> <property name =" targetObject "> <ref bean =" myJob "/> </property> <property name =" targetMethod "> <value> generate </value> </property> </bean> <! -- Job configuration ends --> <! -- Working bean --> <bean id = "myJob" class = "com. seewoedu. train. quartz. GenerateRewardListTask"/> </beans>
3. Executed Method
Public class GenerateRewardListTask {@ Autowired private GiftReceiveRecordService giftReceiveRecordService; org. slf4j. logger logger = LoggerFactory. getLogger (GenerateRewardListTask. class); public void generate () throws Exception {giftReceiveRecordService. generateRewardMember (); // method of execution }}
4. Notes
1. execution time refersSet to YearThere will be a problem, that is, when you start the project after this time, he will always report a memory leak error, it probably means that your scheduled task will never be executed, and the project will never start.
2. scheduled tasks are implemented by calculating the time interval between scheduled tasks and obtaining the server time in real time, therefore, it is not feasible to test whether a scheduled task takes effect by modifying the server time.
Thank you!