三种定时任务方式分别为
1 org.springframework.scheduling.annotation.Scheduled 2 java.util.concurrent.ScheduledExecutorService 3 java.util.Timer
示例如下:
1. 新建Maven项目 schedule
2. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java</groupId> <artifactId>schedule</artifactId> <version>1.0.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.8.RELEASE</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
3. ScheduleStarter.java
package com.java; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class ScheduleStarter { public static void main(String[] args) { SpringApplication.run(ScheduleStarter.class, args); } }
4. ScheduledDemo.java
package com.java.schedule; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledDemo { @Scheduled(cron = "0/5 * * * * *") public void demoTask() { System.out.println("[ ScheduledDemo ]\t\t" + System.currentTimeMillis()); } }
5. ScheduledExecutorDemo.java
package com.java.schedule; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.stereotype.Component; @Component public class ScheduledExecutorDemo { private ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(5); @PostConstruct public void demoTask() { executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("[ ScheduledExecutorDemo ]\t" + System.currentTimeMillis()); } }, 0, 5, TimeUnit.SECONDS); } @PreDestroy public void destory() { executor.shutdown(); } }
6. TimerDemo.java
package com.java.schedule; import java.util.Timer; import java.util.TimerTask; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.stereotype.Component; @Component public class TimerDemo { private Timer timer = new Timer(true); @PostConstruct public void demoTask() { timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { System.out.println("[ TimerDemo ]\t\t\t" + System.currentTimeMillis()); } }, 0, 5000); } @PreDestroy public void destory() { timer.cancel(); } }
7. 运行ScheduleStarter.java,启动测试
控制台每隔5秒打印如下信息
[ ScheduledDemo ] 1548156125005 [ TimerDemo ] 1548156127556 [ ScheduledExecutorDemo ] 1548156127566
三种定时任务都正常运行!
.