1. Preface
Recently, timed tasks have been used in the company's projects. This blog post will summarize the timerTask timing tasks. In fact, TimerTask is not used much in actual projects.
Because it cannot run at a specified time, it can only run the program at a certain frequency.
2. TimerTask
In JDK, Timer is a timer class, which can be configured for specified timing tasks.
In JDK, TimerTask is a timed task class. This class implements the Runnable interface and is an abstract class. We can inherit this class and implement timed tasks.
/** * Inherit TimerTask to implement timing tasks*/ 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 + " The timing task is executing..."); } public static void main(String[] args) { Timer timer = new Timer(); // A task that is executed once in 1 second, the parameters are: task, delay, peroid timer.schedule(new MyTask(), 2000, 1000); } } 3. Integrate Spring
Two core classes: ScheduledTimerTask, TimerFactoryBean
The ScheduledTimerTask class is a wrapper implementation of TimerTask, through which trigger information can be defined for this task.
The TimerFactoryBean class allows Spring to create triggers using configurations and automatically create Timer instances for a set of specified ScheduledTimerTask beans.
1. Introduce Jar package: spring.jar, commons-logging.jar
2. Timed scheduling business category:
/** * Timed scheduling business class*/ public class TaskService extends TimerTask { private int count = 1; public void run() { System.out.println("th" + count + "Execute timed task"); 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"></bean> <bean id="scheduledTimerTask"> <property name="timerTask" ref="taskService" /> <!-- Execute configuration every other day: 24*60*60*1000 --> <!-- Execute the program every 1 second --> <property name="period" value="1000" /> <!-- Execute it after the program starts 4 seconds --> <property name="delay" value="4000" /> </bean> <bean id="timerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="scheduledTimerTask" /> </list> </property> </bean> </beans>
4. Test class:
public class Main { public static void main(String[] args) { // Load spring configuration file 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 all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.