定时任务执行方式:
- 单线程(串行)
- 多线程(并行)
创建定时任务:
1 package redcord.task; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.scheduling.annotation.Scheduled; 6 import org.springframework.stereotype.Component; 7 8 /** 9 * Created by P.ww on 2017-12-22. 10 */ 11 @Component 12 public class KeepAlive { 13 14 private static final Logger logger = LoggerFactory.getLogger(KeepAlive.class); 15 16 @Scheduled(cron="0/5 * * * * ?") 17 public void executeFileDownLoadTask() { 18 19 // 间隔2分钟,执行工单上传任务 20 Thread current = Thread.currentThread(); 21 System.out.println("定时任务1:"+current.getId()); 22 logger.info("ScheduledTest.executeFileDownLoadTask 定时任务1:"+current.getId()+ ",name:"+current.getName()); 23 } 24 25 @Scheduled(cron="0/5 * * * * ?") 26 public void executeUploadTask() { 27 28 // 间隔1分钟,执行工单上传任务 29 Thread current = Thread.currentThread(); 30 System.out.println("定时任务2:"+current.getId()); 31 logger.info("ScheduledTest.executeUploadTask 定时任务2:"+current.getId() + ",name:"+current.getName()); 32 } 33 34 @Scheduled(cron="0/5 * * * * ?") 35 public void executeUploadBackTask() { 36 37 // 间隔3分钟,执行工单上传任务 38 Thread current = Thread.currentThread(); 39 System.out.println("定时任务3:"+current.getId()); 40 logger.info("ScheduledTest.executeUploadBackTask 定时任务3:"+current.getId()+ ",name:"+current.getName()); 41 } 42 }
启动定时任务:
1 package redcord; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.annotation.ComponentScan; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.ImportResource; 8 import org.springframework.scheduling.annotation.EnableScheduling; 9 import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 10 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 11 12 @ComponentScan 13 @SpringBootApplication 14 @EnableScheduling 15 public class ShellApplication extends WebMvcConfigurerAdapter { 16 17 public static void main(String[] args) { 18 SpringApplication.run(ShellApplication.class, args); 19 } 20 }
其中 @EnableScheduling 注解的作用是发现注解@Scheduled的任务并后台执行。
执行结果(单线程执行定时任务):
1 定时任务2:27 2 2017-12-22 11:28:30.002 INFO 7600 --- [pool-2-thread-1] redcord.task.KeepAlive : ScheduledTest.executeUploadTask 定时任务2:27,name:pool-2-thread-1 3 定时任务3:27 4 2017-12-22 11:28:30.004 INFO 7600 --- [pool-2-thread-1] redcord.task.KeepAlive : ScheduledTest.executeUploadBackTask 定时任务3:27,name:pool-2-thread-1 5 定时任务1:27 6 2017-12-22 11:28:30.006 INFO 7600 --- [pool-2-thread-1] redcord.task.KeepAlive : ScheduledTest.executeFileDownLoadTask 定时任务1:27,name:pool-2-thread-1
开启线程池执行多线程(applicationContext.xml):
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:task="http://www.springframework.org/schema/task" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 6 7 8 <!-- Enables the Spring Task @Scheduled programming model --> 9 <task:executor id="executor" pool-size="5" /> 10 <task:scheduler id="scheduler" pool-size="10" /> 11 <task:annotation-driven executor="executor" scheduler="scheduler" /> 12 13 14 </beans>
引入xml配置文件:
1 package redcord.xmlResource; 2 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.context.annotation.ImportResource; 5 6 /** 7 * Created by Administrator on 2017-12-22. 8 */ 9 @Configuration 10 @ImportResource(locations={"classpath:applicationContext.xml"}) 11 public class XMLResource { 12 13 }
执行结果:
1 定时任务2:28 2 定时任务3:36 3 定时任务1:37 4 2017-12-22 11:31:15.001 INFO 1504 --- [ scheduler-2] redcord.task.KeepAlive : ScheduledTest.executeUploadTask 定时任务2:28,name:scheduler-2 5 2017-12-22 11:31:15.001 INFO 1504 --- [ scheduler-4] redcord.task.KeepAlive : ScheduledTest.executeUploadBackTask 定时任务3:36,name:scheduler-4 6 2017-12-22 11:31:15.001 INFO 1504 --- [ scheduler-5] redcord.task.KeepAlive : ScheduledTest.executeFileDownLoadTask 定时任务1:37,name:scheduler-5