# async-springboot-starter **Repository Path**: Croce/async-spring-boot-starter ## Basic Information - **Project Name**: async-springboot-starter - **Description**: 函数式多线程编程 多线程任务编排(京东代码二次开发) - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 5 - **Created**: 2023-07-26 - **Last Updated**: 2023-07-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 项目名称 > web-cloud-async > > 主要实现在web容器中进行异步调用 ## 立项说明 > 基于函数编程实现多线程异步调用,形参为函数,使用scala懒加载特性进行在指定位置执行。 > > 多线程任务编排主要来自京东源代码的二次优化。 ## 运行条件 > 项目运行条件(需要打包在maven仓库中并且在SpringBoot项目中进行引入) * Scala code runner version 2.12.14 以上版本 * Apache Maven 3.6.0 以上版本 * SpringBoot具体以项目编译的版本为主 * JDK8 以上 * 具体需要以实际测试为主(以上是测试环境,具体需要进行相应的测试) ## 运行说明 > 说明如何运行和使用你的项目,建议给出具体的步骤说明 * 构建SpringBoot项目 * 编译本地Maven库/maven私服 * 具体操作见测试案例 ## 测试说明 > 见测试案例 ## 项目核心 - web-cloud-async-spring-boot-starter - 主要提供springboot引入作为web容器的基础模块 - web-cloud-async-spring-boot-starter-autoconfigure - 核心异步编程执行的核心 - web-cloud-async-spring-boot-starter-flow - 多线程任务编排 - 源代码来自:https://gitee.com/jd-platform-opensource/asyncTool ## 核心代码 - web-cloud-async-spring-boot-starter - 引用包,无代码传递 - web-cloud-async-spring-boot-starter-autoconfigure - AsyncBusiness:顶级实现类(特质) - DataSourceAsyncBusiness:主要作为数据容器存放异步函数提供基础方法 - ApplicationContext:业务执行的核心方法,如实现异步任务、保存数据到数据容器、释放数据容器中的数据、获取结果等 - AbstractApplicationContext:业务核心实现类,基础代码实现,如需扩展实现该类即可 - StandardAsyncContext:具体业务落地,后期可以根据需求需要自行模仿该类实现完成在容器中的配置类 - 以上是代码自上而下主要层级 - web-cloud-async-spring-boot-starter-flow - 主要参考京东零售开源代码,代码地址:https://gitee.com/jd-platform-opensource/asyncTool - 代码二次开发 - 具体参考https://gitee.com/jd-platform-opensource/asyncTool说明文档(少许方法二次修改) ## 快速开始 ### 函数多线程(async) > 进行下载代码进行打包至相应环境,下载scala环境 > > 新建springboot项目 > > 进行配置容器 > > 方法1: > > > 启动类添加 @EnableWebAsyncConfiguration > > > > 进行yml文件配置 > > > > ```yml > > web: > > cloud: > > async: > > #全局等待时间 默认1s (1000 = 1s) > > milli-second: 1000 > > # 本地数据容器Map的长度 > > date-source-size: 20 > > ``` > > > > 创建线程池并且加入容器 > > > > ```java > > @Bean > > public Executor executor() { > > ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); > > threadPoolTaskExecutor.setCorePoolSize(corePoolSize); > > threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize); > > threadPoolTaskExecutor.setQueueCapacity(workQueue); > > threadPoolTaskExecutor.setThreadNamePrefix("projectTaskExecutor-"); > > threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveTime); > > threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); > > threadPoolTaskExecutor.initialize(); > > return threadPoolTaskExecutor; > > } > > ``` > > > > 使用...... > > 方法2: > > > 配容器 > > > > ``` > > @Bean > > public Executor executor() { > > ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); > > threadPoolTaskExecutor.setCorePoolSize(corePoolSize); > > threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize); > > threadPoolTaskExecutor.setQueueCapacity(workQueue); > > threadPoolTaskExecutor.setThreadNamePrefix("projectTaskExecutor-"); > > threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveTime); > > threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); > > threadPoolTaskExecutor.initialize(); > > return threadPoolTaskExecutor; > > } > > ``` > > > > 配置对象接入容器 > > > > ```java > > @Bean("standardAsyncContext") > > public StandardAsyncContext standardAsyncContext() { > > /** > > * 参数1:线程池 > > * 参数2:结果处理类 > > * 参数3:全局等待时间(默认1s) > > * 参数4:当前类的Map空间大小 > > */ > > StandardAsyncContext standardAsyncContext = new StandardAsyncContext( > > executor, new StandardResultFactory(), > > 1000L, 30 > > ); > > return standardAsyncContext; > > } > > ``` > > > > 业务中使用... > > > 具体见web-cloud-springboot-example测试案例 > > > > com.cloud.example.business.async.controller.AsyncController ### 多线程任务编排(flow) > 具体相关信息见: > > https://gitee.com/jd-platform-opensource/asyncTool/blob/master/QuickStart.md 注入容器: ``` //多次安城任务编排(事务) //com.cloud.async.flow.web.tx.TransactionSource需要实现接口 //com.cloud.example.config.sql.TransactionSourceFlowConfig 实现类 @Bean AsyncFlowTemplate flowTemplate() { return new FlowTemplate(Executors.newCachedThreadPool(), transactionSourceFlowConfig); } // 多线程任务编排(不带事务) // @Bean // AsyncFlowTemplate flowTemplate() { // return new FlowTemplate(Executors.newCachedThreadPool()); // } ``` > 具体测试见包: > > > 具体编排组合代码见文档 > > > > https://gitee.com/jd-platform-opensource/asyncTool > > com.cloud.example.business.flow (无事务) > > com.cloud.example.business.transaction (事务 目前业务正常,未进行大规模测试) ## 版本升级 > > 版本:1.0.0 > > > > 异步返回值以String类型为主,对定制的返回需要使用定制结果进行处理 > > > > > 版本:2.0.0 > > > > 功能开发中 > >