spring USES squertz for timing tasks
- 2020-06-01 09:52:05
- OfStack
preface
When it comes to timed tasks, developers are no strangers. Some things are always done by computers, not by ourselves. But a lot of people to timer always feel very strange, today this site will take partners to unveil its mystery, explain 1 spring using squertz timing task.
1. Jar as needed
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 files
Here configure when you will execute your scheduled task and what method you will execute, such as the following at 23:00:00, November 10, 2016
com.seewoedu.train.quartz.GenerateRewardListTask
the
generate
Method, using cron specifies the execution time here, and relevant syntax to view https: / / www ofstack. com article / 103861. htm
<?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 the configuration of the trigger -->
<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 configuration of the trigger to start ends -->
<!-- quartz-2.x The configuration of the -->
<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 The expression" 10 */1 * * * ? "From 10 Seconds start, every 1 Minutes to perform 1 Times. -->
<value>0 0 9 10 12 ? 2016</value> <!-- Cron The expression" 0 0 23 10 11 ? 2016 Only in 2016.11.10 23:00:00 The execution. -->
</property>
</bean>
<!-- The configuration of the schedule ends -->
<!-- job Configuration start -->
<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 End of configuration -->
<!-- work bean -->
<bean id="myJob" class="com.seewoedu.train.quartz.GenerateRewardListTask" />
</beans>
3. Method of execution
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. Problems to be paid attention to
1. When the execution time is specified to the year, there will be a problem, that is, when you start the project after this time, it will directly report an memory leak error, which roughly means that your scheduled task will never be executed, resulting in the failure of project 1.
2. The timing task is realized by calculating the time interval from the time you deploy the project to the time you want to perform the timing task, rather than obtaining your server time in real time. Therefore, it is not feasible to test whether the timing task is effective by modifying the server time.
conclusion
The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.