标签:get 1.0 rom sim orm not schema cep enc
1)在Spring.xml中开启定时器的功能
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "> <!-- 让Spring找到被@Component注解的类 --> <context:component-scan base-package="cn.ltian.base" /> <!--定时器开关 --> <task:annotation-driven />
2)编写定时器类
package cn.ltian.base; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @Component public class MyScheduler { //每10秒执行一次 @Scheduled(cron = "0/2 * * * * ?") public void aTask() { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date())+" :"+Thread.currentThread().getName()+"a任务执行一次,2m 开始"); try{ Thread.sleep(5000); }catch(Exception e){ } System.out.println(sdf.format(new Date())+" :"+Thread.currentThread().getName()+"a任务执行一次,2m 结束"); } //每5秒执行一次 @Scheduled(cron = "0/5 * * * * ?") public void bTask() { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date())+" :"+Thread.currentThread().getName()+"b任务执行一次,5m"); } }
3)运行结果
2018-11-13 20:27:20 :pool-1-thread-1a任务执行一次,2m 开始
2018-11-13 20:27:25 :pool-1-thread-1a任务执行一次,2m 结束
2018-11-13 20:27:25 :pool-1-thread-1b任务执行一次,5m
2018-11-13 20:27:26 :pool-1-thread-1a任务执行一次,2m 开始
2018-11-13 20:27:31 :pool-1-thread-1a任务执行一次,2m 结束
2018-11-13 20:27:31 :pool-1-thread-1b任务执行一次,5m
2018-11-13 20:27:32 :pool-1-thread-1a任务执行一次,2m 开始
2018-11-13 20:27:37 :pool-1-thread-1a任务执行一次,2m 结束
2018-11-13 20:27:37 :pool-1-thread-1b任务执行一次,5m
2018-11-13 20:27:38 :pool-1-thread-1a任务执行一次,2m 开始
2018-11-13 20:27:43 :pool-1-thread-1a任务执行一次,2m 结束
2018-11-13 20:27:43 :pool-1-thread-1b任务执行一次,5m
2018-11-13 20:27:44 :pool-1-thread-1a任务执行一次,2m 开始
1)在Spring.xml中开启定时器的功能
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "> <!-- 让Spring找到被@Component注解的类 --> <context:component-scan base-package="cn.ltian.base" /> <!--定时器开关 --> <task:annotation-driven scheduler="MyScheduler"/> <!-- 配置任务线程池 --> <task:scheduler id="MyScheduler" pool-size="4" />
2)编写定时器类
package cn.ltian.base; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @Component public class MyScheduler { //每10秒执行一次 @Scheduled(cron = "0/2 * * * * ?") public void aTask() { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date())+" :"+Thread.currentThread().getName()+"a任务执行一次,2m 开始"); try{ Thread.sleep(5000); }catch(Exception e){ } System.out.println(sdf.format(new Date())+" :"+Thread.currentThread().getName()+"a任务执行一次,2m 结束"); } //每5秒执行一次 @Scheduled(cron = "0/5 * * * * ?") public void bTask() { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date())+" :"+Thread.currentThread().getName()+"b任务执行一次,5m"); } }
3)运行结果
2018-11-13 20:29:18 :MyScheduler-1a任务执行一次,2m 开始
2018-11-13 20:29:20 :MyScheduler-2b任务执行一次,5m
2018-11-13 20:29:23 :MyScheduler-1a任务执行一次,2m 结束
2018-11-13 20:29:24 :MyScheduler-2a任务执行一次,2m 开始
2018-11-13 20:29:25 :MyScheduler-3b任务执行一次,5m
2018-11-13 20:29:29 :MyScheduler-2a任务执行一次,2m 结束
2018-11-13 20:29:30 :MyScheduler-3b任务执行一次,5m
2018-11-13 20:29:30 :MyScheduler-2a任务执行一次,2m 开始
2018-11-13 20:29:35 :MyScheduler-2a任务执行一次,2m 结束
2018-11-13 20:29:35 :MyScheduler-3b任务执行一次,5m
2018-11-13 20:29:36 :MyScheduler-2a任务执行一次,2m 开始
2018-11-13 20:29:40 :MyScheduler-4b任务执行一次,5m
2018-11-13 20:29:41 :MyScheduler-2a任务执行一次,2m 结束
2018-11-13 20:29:42 :MyScheduler-2a任务执行一次,2m 开始
2018-11-13 20:29:45 :MyScheduler-4b任务执行一次,5m
2018-11-13 20:29:47 :MyScheduler-2a任务执行一次,2m 结束
2018-11-13 20:29:48 :MyScheduler-2a任务执行一次,2m 开始
2018-11-13 20:29:50 :MyScheduler-4b任务执行一次,5m
2018-11-13 20:29:53 :MyScheduler-2a任务执行一次,2m 结束
2018-11-13 20:29:54 :MyScheduler-2a任务执行一次,2m 开始
2018-11-13 20:29:55 :MyScheduler-4b任务执行一次,5m
2018-11-13 20:29:59 :MyScheduler-2a任务执行一次,2m 结束
2018-11-13 20:30:00 :MyScheduler-2a任务执行一次,2m 开始
2018-11-13 20:30:00 :MyScheduler-4b任务执行一次,5m
1)在Spring.xml中开启定时器的功能
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "> <!-- 让Spring找到被@Component注解的类 --> <context:component-scan base-package="cn.ltian.base" /> <!--定时器开关 --> <task:annotation-driven scheduler=" myScheduler"/> <!-- 配置任务线程池 --> <task:scheduler id="myScheduler" pool-size="4" />
2)编写定时器类
package cn.ltian.base; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @Component public class MyScheduler { //每10秒执行一次 @Scheduled(cron = "0/2 * * * * ?") @Async(value="MyScheduler") public void aTask() { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date())+" :"+Thread.currentThread().getName()+"a任务执行一次,2m 开始"); try{ Thread.sleep(5000); }catch(Exception e){ } System.out.println(sdf.format(new Date())+" :"+Thread.currentThread().getName()+"a任务执行一次,2m 结束"); } //每5秒执行一次 @Scheduled(cron = "0/5 * * * * ?") public void bTask() { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date())+" :"+Thread.currentThread().getName()+"b任务执行一次,5m"); } }
3)运行结果
2018-11-13 20:32:24 :MyScheduler-2a任务执行一次,2m 开始
2018-11-13 20:32:25 :MyScheduler-3b任务执行一次,5m
2018-11-13 20:32:26 :MyScheduler-3a任务执行一次,2m 开始
2018-11-13 20:32:28 :MyScheduler-4a任务执行一次,2m 开始
2018-11-13 20:32:29 :MyScheduler-2a任务执行一次,2m 结束
2018-11-13 20:32:30 :MyScheduler-2b任务执行一次,5m
2018-11-13 20:32:30 :MyScheduler-1a任务执行一次,2m 开始
2018-11-13 20:32:31 :MyScheduler-3a任务执行一次,2m 结束
2018-11-13 20:32:32 :MyScheduler-3a任务执行一次,2m 开始
2018-11-13 20:32:33 :MyScheduler-4a任务执行一次,2m 结束
2018-11-13 20:32:34 :MyScheduler-4a任务执行一次,2m 开始
2018-11-13 20:32:35 :MyScheduler-1a任务执行一次,2m 结束
2018-11-13 20:32:35 :MyScheduler-2b任务执行一次,5m
2018-11-13 20:32:36 :MyScheduler-2a任务执行一次,2m 开始
2018-11-13 20:32:37 :MyScheduler-3a任务执行一次,2m 结束
2018-11-13 20:32:38 :MyScheduler-3a任务执行一次,2m 开始
标签:get 1.0 rom sim orm not schema cep enc
原文地址:https://www.cnblogs.com/ltian123/p/10027798.html