springboot-定时任务-多线程
1、配置异步线程池
import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
@EnableAsync//开启异步事件的支持
public class AsyncConfig { /*
* 此处成员变量应该使用@Value从配置中读取
*/
private int corePoolSize = 10;
private int maxPoolSize = 200;
private int queueCapacity = 10; @Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.initialize();
return executor;
} }
2、定时任务类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class ScheduledService { private Logger logger = LoggerFactory.getLogger(ScheduledService.class); @Scheduled(cron = "0/5 * * * * *")
@Async
public void scheduled() {
logger.info("=====>>>>>使用cron {}", System.currentTimeMillis());
} @Scheduled(fixedRate = 5000)
@Async
public void scheduled1() {
logger.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
} @Scheduled(fixedDelay = 5000)
@Async
public void scheduled2() {
logger.info("=====>>>>>fixedDelay{}", System.currentTimeMillis());
}
}
springboot-定时任务-多线程的更多相关文章
- 【使用篇二】SpringBoot定时任务Scheduled(14)
在日常项目运行中,我们总会有需求在某一时间段周期性的执行某个动作.比如每天在某个时间段导出报表,或者每隔多久统计一次现在在线的用户量.在springboot中可以有很多方案去帮我们完成定时器的工作,有 ...
- SpringBoot定时任务 - 开箱即用分布式任务框架xxl-job
除了前文介绍的ElasticJob,xxl-job在很多中小公司有着应用(虽然其代码和设计等质量并不太高,License不够开放,有着个人主义色彩,但是其具体开箱使用的便捷性和功能相对完善性,这是中小 ...
- springboot 定时任务部署至linux服务器上后会执行两次问题
springboot定时任务在本地运行时,正常执行且只执行一次,但是在maven打包成war包,部署至linux服务器上之后,定时任务奇怪的执行了两次. 由于未做负载均衡,所以可以先排除是因为多台服务 ...
- springboot下多线程开发注意事项
基于springboot的多线程程序开发过程中,由于本身也需要注入spring容器进行管理,才能发挥springboot的优势.所以这篇文字主要用来记录开发中两者结合时需要注意的一些事项. 第一步我们 ...
- Spring boot(三) springboot 定时任务
这个不多说,springboot 定时任务非常简单就可以实现了. 30s运行一次 , @Scheduled(cron="0,30 * * * * ?") 通过这个控制定时时间 cr ...
- springboot定时任务之旅
springboot定时任务 假设场景:单体应用的定时任务,假设我们已经有了一个搭建好的springboot应用,但是需要添加一个定时执行的部分(比如笔者遇到的是定时去请求一个接口数据来更新某个表), ...
- SpringBoot定时任务@Scheduled
SpringBoot定时任务主要由两个注解完成. @Scheduled加在方法上面. @EnableScheduling加在类上面.可以是Application类,也可以是@Component类,还可 ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解
笔记 1.SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数
SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...
- SpringBoot定时任务 - 集成quartz实现定时任务(单实例和分布式两种方式)
最为常用定时任务框架是Quartz,并且Spring也集成了Quartz的框架,Quartz不仅支持单实例方式还支持分布式方式.本文主要介绍Quartz,基础的Quartz的集成案例本,以及实现基于数 ...
随机推荐
- python中常见的日期处理方法
今天:today = datetime.date.today() 昨天:yesterday = today - datetime.timedelta(days=1) 上个月:last_month = ...
- 数组的新方法 forEach some filter findIndex
forEach some filter findIndex这些都属于数组的新方法,都会对数组中的每一项,进行遍历,执行相关的操作: 只不过在循环的时候有些不一样 参考资料:https://wan ...
- ES使用org.elasticsearch.client.transport.NoNodeAvailableException: No node available
1) 端口错 client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(ipAddress, ...
- Greenplum实战之查询优化
本文主要分为三部分: GP优化需要准备的一些关于优化之外的知识,包括清空缓存.性能监控.执行计划分析. 具体优化措施,从以下四个方面考虑: 表.字段 sql GP配置.服务器配置 硬件及节点资源 GP ...
- vue1 自定义过滤器
- vue文件夹上传插件选哪个好?
文件夹数据库处理逻辑 public class DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject() ...
- 查找、AVL树、散列表
插值查找是二分查找的改进,斐波那契查找是插值查找的改进. 二分查找:mid=(low+high)/ 2 插值查找:mid=(key-a[low])*(high-low)/ (a[high]-a[l ...
- Codeforces Round #553 (Div. 2) 【C. Problem for Nazar】
题目大意: 一开始第一行是 1,第二行是2 4 ,第三行是3 5 7 9 ,类似这样下去,每一行的个数是上一行的个数,然后对这些点从第一个进行编号,问你从[l,r]区间数的和. 思路:分别求出奇数和偶 ...
- Codeforces Round #442 (Div. 2) B题【一道模拟题QAQ】
B. Nikita and string One day Nikita found the string containing letters "a" and "b&qu ...
- Cogs 58. 延绵的山峰(st表)
延绵的山峰 ★★☆ 输入文件:climb.in 输出文件:climb.out 简单对比 时间限制:1 s 内存限制:512 MB 问题描述 有一座延绵不断.跌宕起伏的山,最低处海拔为0,最高处海拔不超 ...