当前位置: 首页 > news >正文

SpringBoot异步任务(2)|(线程池使用)

SpringBoot异步任务(2)|(线程池使用)


文章目录

  • SpringBoot异步任务(2)|(线程池使用)
    • @[TOC]
  • 前言
  • 一、使用场景
  • 二、springboot添加异步任务
    • 1.配置线程池
    • 2.线程池的使用
  • 总结

章节
第一章链接: SpringBoot异步任务(1)|(异步任务执行以及回调)

前言

线程池开启异步任务在springboot中的使用

一、使用场景

项目中有一个批量调度的任务,客户上传批量的文章,让后将这些文章去进行任务处理

二、springboot添加异步任务

1.配置线程池

在springboot容器中配置线程池,后续使用直接将bean注入使用即可

@Configuration
@EnableAsync
public class ExecutorEmbPoolConfig {private static final Logger logger = LoggerFactory.getLogger(ExecutorEmbPoolConfig.class);@Value("${embedding.pool.corePoolSize:20}")private int corePoolSize = 20;@Value("${embedding.pool.maxPoolSize:20}")private int maxPoolSize = 20;@Value("${embedding.pool.queueCapacity:100000}")private int queueCapacity = 100000;private String namePrefix = "embedding-service-";@Bean(name = "embeddingServiceExecutor")public ThreadPoolTaskExecutor asyncServiceExecutor() {logger.debug("start embedding embeddingServiceExecutor");ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();//配置核心线程数executor.setCorePoolSize(corePoolSize);//配置最大线程数executor.setMaxPoolSize(maxPoolSize);//配置队列大小executor.setQueueCapacity(queueCapacity);//配置线程池中的线程的名称前缀executor.setThreadNamePrefix(namePrefix);// 允许回收核心线程executor.setAllowCoreThreadTimeOut(true);// CALLER_RUNS: 不在新线程中执行任务,而是有调用者所在的线程来执行executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//执行初始化executor.initialize();return executor;}
}

2.线程池的使用

@Resource(name = "embeddingServiceExecutor")private ThreadPoolTaskExecutor executor;@Scheduled(cron = "0/30 * * * * ?")public void FileToMilvesJob() {//定义计数器List<DocumentMilvusRecord> documentMilvusRecords = recordService.findByStatus(RecordStatus.WAIT);if (CollectionUtils.isEmpty(documentMilvusRecords)) {return;}List<DocumentMilvusRecord> excuteList;if (documentMilvusRecords.size() > 50) {excuteList = documentMilvusRecords.subList(0, 50);} else {excuteList = documentMilvusRecords;}log.info("本次任务需要执行任务“{}条", excuteList.size());for (DocumentMilvusRecord record : excuteList) {recordService.updateRecordStatus(record);executor.execute(() -> {try {docEmbeddingCreate(record); // 执行业务逻辑} catch (Exception e) {log.error(e.getMessage());}});}}

总结

上面的方式实现了自定义一个线程池,然后执行任务的时候获取线程池并执行任务。

http://www.lryc.cn/news/117192.html

相关文章:

  • 解决Windows:Call to undefined function exif_imagetype()
  • 【Spring】Spring AOP 初识及实现原理解析
  • 【Express.js】集成Redis
  • StringBuilder创建的对象如何清空
  • mybatis-plus实现mysql自定义IKeyGenerator
  • 山西电力市场日前价格预测【2023-08-11】
  • 浏览器无法连接网络问题
  • ZyjDataLink 全量MySQL同步程序 - 开发过程 01
  • 为什么说Python股票接口是连接投资与编程的桥梁?
  • kubectl,helm安装到window
  • 【BASH】回顾与知识点梳理(目录)
  • TFRecords详解
  • 【多维定向滤波器组和表面波】表面变换:用于高效表示多维 s 的多分辨率变换(Matlab代码实现)
  • 45.113.201.X服务器远程不上是什么原因,有什么办法解决?
  • 微信小程序 地图map(电子围栏圆形和多边形)
  • Dockerfile 文件
  • ssm学院党员管理系统源码和论文PPT
  • 文件数字水印,附一种纯文本隐写术数字水印方法
  • 测试开发(一) 使用Vue开发chrome插件
  • 游戏行业实战案例 4 :在线时长分析
  • 记一次图片压缩引发的生产问题
  • mybatis-flex探索
  • 用ClickHouse 文件表引擎快速查询分析文件数据
  • esp8266httpclient_get_post使用
  • 【Spring】创建一个Spring项目与Bean对象的存储
  • Docker的入门与使用
  • Smart HTML Elements 16.1 Crack
  • [分享]STM32G070 串口 乱码 解决方法
  • [代码案例]学会python读写各类文件的操作(excel,txt,mat)
  • 【LeetCode】练习习题集【4月 - 7 月】