Home > Java > javaTutorial > body text

Spring uses squertz to implement scheduled tasks

高洛峰
Release: 2017-01-23 16:13:30
Original
1716 people have browsed it

Preface

Speaking of scheduled tasks, development partners are definitely familiar with them. Some things always need computers to do it, rather than just doing it on our own. However, many people are always unfamiliar with timers. Today, I will take my friends to unveil its mystery and explain how spring uses squertz to implement 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
Copy after login

2. Configuration file

Configure here when you want to execute your Scheduled tasks, what method is executed, for example, the following is the generate method of com.seewoedu.train.quartz.GenerateRewardListTask executed at 2016.11.10 23:00:00, where cron is used to specify the execution time

<?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">
 <!-- 启动触发器的配置开始 -->
 <bean name="startQuertz" lazy-init="false" autowire="no"
  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
 <property name="triggers">
  <list>
  <ref bean="myJobTrigger" />
  </list>
 </property>
 </bean>
 <!-- 启动触发器的配置结束 -->
 
 <!-- quartz-2.x的配置 -->
 <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表达式“10 */1 * * * ?”意为:从10秒开始,每1分钟执行一次。 -->
  <value>0 0 9 10 12 ? 2016</value> <!-- Cron表达式“0 0 23 10 11 ? 2016”意为:只在2016.11.10 23:00:00 执行。 -->
 </property>
 </bean>
 <!-- 调度的配置结束 -->
 
 <!-- job的配置开始 -->
 <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的配置结束 -->
 
 <!-- 工作的bean -->
 <bean id="myJob" class="com.seewoedu.train.quartz.GenerateRewardListTask" />
</beans>
Copy after login

3. Methods to be executed

public class GenerateRewardListTask {
 @Autowired
 private GiftReceiveRecordService giftReceiveRecordService;
 
 org.slf4j.Logger logger = LoggerFactory.getLogger(GenerateRewardListTask.class);
 
 public void generate() throws Exception {
 giftReceiveRecordService.generateRewardMember(); //执行的方法
 
 }
}
Copy after login

4. Issues that need attention

1. When the execution time is specified to the year, there will be a problem, that is, when you When you start the project after this time, it will always report a memory leak error, which probably means that your scheduled tasks will never be executed, causing the project to never start.

2. Scheduled tasks are implemented by calculating the time interval between when you deploy the project and when you want to execute the scheduled task, rather than by obtaining your server time in real time. Therefore, you want to test whether the scheduled task takes effect by modifying the server time. is not feasible.

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more spring-related articles using squertz to implement scheduled tasks, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template