Spring Boot : 定时任务(十)
来源:互联网 发布:淘宝商城女装夏装 编辑:程序博客网 时间:2024/06/04 19:48
目录
- 目录
- 单线程定时任务
- 多线程定时任务
单线程定时任务
SpringBoot提供的定时任务是单线程的。代码很简单。
package cn.milo.controllor;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Configuration@EnableScheduling//@Componentpublic class Scheduling { private final Logger logger = LoggerFactory.getLogger(getClass()); @Scheduled(cron = "0/5 * * * * ?") // 每20秒执行一次 public void scheduler() { logger.info("定时任务 1"); }} @Scheduled(cron = "0/5 * * * * ?") // 每20秒执行一次 public void scheduler2() { logger.info("定时任务 2"); }}
大家可以打印一下线程号,会发现所有定时任务都是串行完成的。但很多时候我们需要好多个定时任务一起进行,且互不干扰。下边介绍并行定时任务。
多线程定时任务
Scheduling.java
package cn.milo.controllor;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;//@Configuration //这里就不用springboot了//@EnableScheduling //这里就不用springboot了@Component //spring直接加载这个类public class Scheduling { private final Logger logger = LoggerFactory.getLogger(getClass()); @Scheduled(cron = "0/5 * * * * ?") // 每20秒执行一次 public void scheduler() { logger.info("定时任务 1"); } @Scheduled(cron = "0/5 * * * * ?") // 每20秒执行一次 public void scheduler2() { logger.info("定时任务 2"); }}
通过SpringBoot配置Spring方式来指定Spring配置文件
SpringConfig.java
package cn.milo.controllor;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;/** * Created by mac on 2017/8/28. */@Configuration@ImportResource("/spring/applicationContext.xml")public class SpringConfig {}
在src/main/resources/spring下简历spring配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- Enables the Spring Task @Scheduled programming model --> <task:executor id="executor" pool-size="5" /> <task:scheduler id="scheduler" pool-size="10" /> <task:annotation-driven executor="executor" scheduler="scheduler" /></beans>
阅读全文
0 0
- Spring Boot : 定时任务(十)
- Spring boot 十 定时任务
- spring boot 定时任务
- spring boot 定时任务
- spring boot定时任务
- Spring boot 定时任务
- Spring Boot 定时任务
- Spring Boot参考教程(十)定时任务
- Spring Boot 实现定时任务
- spring boot创建定时任务
- Spring Boot定时任务实例
- 【Spring Boot】定时任务实现
- Spring Boot 实现定时任务
- spring boot实现定时任务
- Spring Boot入门教程-定时任务
- Spring Boot 实现定时任务
- Spring Boot 配置定时任务
- spring-boot集成定时任务
- 错误:无法将文件XXXX.dll”复制到XXXX.dll。对路径xxx.dll”的访问被拒绝。dll”的访问被拒绝。
- MFC 获取MAC-IP-NetworkName
- 分布式事务,高并发下分布式事务的解决方案
- 自定义view——wrap_context
- Java8 ArrayList的实现
- Spring Boot : 定时任务(十)
- SpringMVC笔记
- 石子合并(四边形优化第一题)
- EF更新错误:一个实体对象不能由多个 IEntityChangeTracker 实例引用
- memcached 操作
- JS 性能提升
- feign 9.3.0 学习笔记
- Codeforces 844 C Sorting by Subsequences(离散+dfs+排序)
- 310