1、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>test.hanergy</groupId>
<artifactId>staticTast</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>staticTast</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2、启动类App.java
package test.hanergy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3、配置类ScheduleConfig
package test.hanergy.config;
import java.util.concurrent.Executors;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Bean(destroyMethod = "shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(20);
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
}
也可以写作
@Bean(destroyMethod = "shutdown")
public ThreadPoolTaskScheduler taskScheduler() {
// 创建一个线程池调度器
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
// 设置线程池容量
scheduler.setPoolSize(20);
// 线程名前缀
scheduler.setThreadNamePrefix("task-");
// 等待时常
scheduler.setAwaitTerminationSeconds(60);
// 当调度器shutdown被调用时等待当前被调度的任务完成
scheduler.setWaitForTasksToCompleteOnShutdown(true);
// 设置当任务被取消的同时从当前调度器移除的策略
scheduler.setRemoveOnCancelPolicy(true);
return scheduler;
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setTaskScheduler(taskScheduler());
}
4、任务类Task
package test.hanergy.task;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Task {
@Scheduled(cron="0/1 * * * * ? ") //每1秒执行一次
public void testCron1() {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date())+"*********每1秒执行一次");
}
@Scheduled(cron="0/2 * * * * ? ") //每2秒执行一次
public void testCron2() {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date())+"*********每2秒执行一次");
}
@Scheduled(cron="0/3 * * * * ? ") //每3秒执行一次
public void testCron3() {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date())+"*********每3秒执行一次");
}
@Scheduled(cron="0/4 * * * * ? ") //每4秒执行一次
public void testCron4() {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date())+"*********每4秒执行一次");
}
}