spring USES squertz for timing tasks

  • 2020-06-01 09:52:05
  • OfStack


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


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"
 <!--  Start the configuration of the trigger  -->
 <bean name="startQuertz" lazy-init="false" autowire="no"
 <property name="triggers">
  <ref bean="myJobTrigger" />
 <!--  The configuration of the trigger to start ends  -->

 <!-- quartz-2.x The configuration of the  -->
 <bean id="myJobTrigger"
 <property name="jobDetail">
  <ref bean="myJobDetail" />
 <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.  -->
 <!--  The configuration of the schedule ends  -->

 <!-- job Configuration start  -->
 <bean id="myJobDetail"
 <property name="targetObject">
  <ref bean="myJob" />
 <property name="targetMethod">
 <!-- job End of configuration  -->

 <!--  work bean -->
 <bean id="myJob" class="com.seewoedu.train.quartz.GenerateRewardListTask" />

3. Method of execution

public class GenerateRewardListTask {
 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.


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.

Related articles: