Home  >  Article  >  Java  >  Spring integrates TimerTask to implement scheduled task scheduling

Spring integrates TimerTask to implement scheduled task scheduling

高洛峰
高洛峰Original
2017-02-07 15:27:351612browse

1. Preface

Recently, timed tasks have been used in the company’s projects. This blog post will summarize the TimerTask timed tasks. In fact, TimerTask is not used much in actual projects.
Because it cannot run at a specified time, it can only allow the program to run at a certain frequency.

2. TimerTask

Timer in JDK is a timer class, which can Configure the specified scheduled task.
TimerTask in JDK is a scheduled task class, which implements the Runnable interface and is an abstract class. We can inherit this class to implement scheduled tasks.

/** 
 * 继承TimerTask实现定时任务 
 */
public class MyTask extends TimerTask { 
  
  @Override
  public void run() { 
    String currentTime = new SimpleDateFormat("yyy-MM-dd hh:mm:ss").format(new Date()); 
    System.out.println(currentTime + " 定时任务正在执行..."); 
  } 
  
  public static void main(String[] args) { 
    Timer timer = new Timer(); 
      
    // 1秒钟执行一次的任务, 参数为: task, delay, peroid 
    timer.schedule(new MyTask(), 2000, 1000); 
  } 
}

三. Integrate Spring

Two core classes: ScheduledTimerTask, TimerFactoryBean
ScheduledTimerTask class is a wrapper implementation of TimerTask, through which trigger information can be defined for this task.
TimerFactoryBean class You can let Spring use configuration to create triggers and automatically create Timer instances for a specified set of ScheduledTimerTask beans.

1. Introduce Jar packages: spring.jar, commons-logging.jar
2. Schedule scheduling Business class:

/** 
 * 定时调度业务类 
 */
public class TaskService extends TimerTask { 
  private int count = 1; 
  
  public void run() { 
    System.out.println("第" + count + "次执行定时任务"); 
    count++; 
  } 
}

3. Core configuration:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
    
  <bean id="taskService" class="com.zdp.service.TaskService"></bean> 
  <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> 
    <property name="timerTask" ref="taskService" /> 
      
    <!-- 每隔一天执行一次配置: 24*60*60*1000 -->
    <!-- 每1秒钟程序执行一次 -->
    <property name="period" value="1000" /> 
      
    <!-- 程序启动4秒钟后开始执行 -->
    <property name="delay" value="4000" /> 
  </bean> 
    
  <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> 
    <property name="scheduledTimerTasks"> 
      <list> 
        <ref bean="scheduledTimerTask" /> 
      </list> 
    </property> 
  </bean> 
</beans>

4. Test class:

public class Main { 
  public static void main(String[] args) { 
    // 加载spring配置文件 
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    System.out.println("<<-------- 启动定时任务 -------- >>"); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
    while (true) { 
      try { 
        if (reader.readLine().equals("quit")) { 
          System.out.println("<<-------- 退出定时任务 -------- >>"); 
          System.exit(0); 
        } 
      } catch (IOException e) { 
        throw new RuntimeException("error happens...", e); 
      } 
    } 
  } 
}

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support the PHP Chinese website.

For more articles related to Spring integrating TimerTask to implement scheduled task scheduling, please pay attention to the PHP Chinese website!

Statement:
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