Spring integrates TimerTask to achieve timed task scheduling

  • 2020-05-27 04:46:09
  • OfStack

1. Introduction

The timing task has been used in the company's project recently. This blog will summarize the timing task of TimerTask. In fact, TimerTask is not used much in the actual project.
Because it cannot be run at a specified time, it can only be run at a certain frequency.

2. TimerTask

Timer in JDK is a timer class that can be configured for a specified timing task.
In JDK, TimerTask is a timing task class. This class implements the Runnable interface and is an abstract class. We can inherit this class to realize the timing task.

 *  inheritance TimerTask Implement timing task  
public class MyTask extends TimerTask { 
  public void run() { 
    String currentTime = new SimpleDateFormat("yyy-MM-dd hh:mm:ss").format(new Date()); 
    System.out.println(currentTime + "  Timed tasks are being performed ..."); 
  public static void main(String[] args) { 
    Timer timer = new Timer(); 
    // 1 Seconds to perform 1 The task of time ,  Parameters for : task, delay, peroid 
    timer.schedule(new MyTask(), 2000, 1000); 

3. Integrate Spring

Two core classes: ScheduledTimerTask and TimerFactoryBean
The ScheduledTimerTask class is the wrapper implementation of TimerTask, which defines the trigger information for this task.
The TimerFactoryBean class allows Spring to create triggers using configuration and automatically create an Timer instance for the ScheduledTimerTask bean specified in 1 group.

1. Introduction of Jar packages: spring.jar, commons-logging.jar
2. Scheduled scheduling business:

 *  Timing scheduling business class  
public class TaskService extends TimerTask { 
  private int count = 1; 
  public void run() { 
    System.out.println(" The first " + count + " Execute a timed task each time "); 

3. Core configuration:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  <bean id="taskService" class="com.zdp.service.TaskService"></bean> 
  <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> 
    <property name="timerTask" ref="taskService" /> 
    <!--  every 1 Days to perform 1 Once the configuration : 24*60*60*1000 --> 
    <!--  every 1 Second program execution 1 time  --> 
    <property name="period" value="1000" /> 
    <!--  Program started 4 Start execution in seconds  --> 
    <property name="delay" value="4000" /> 
  <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> 
    <property name="scheduledTimerTasks"> 
        <ref bean="scheduledTimerTask" /> 

4. Test category:

public class Main { 
  public static void main(String[] args) { 
    //  loading spring The configuration file  
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    System.out.println("<<--------  Start timing task  -------- >>"); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
    while (true) { 
      try { 
        if (reader.readLine().equals("quit")) { 
          System.out.println("<<--------  Exit timed task  -------- >>"); 
      } catch (IOException e) { 
        throw new RuntimeException("error happens...", e); 

Related articles: