asyncTool是个啥
官方仓库这样说:
解决任意的多线程并行、串行、阻塞、依赖、回调的并行框架,可以任意组合各线程的执行顺序,带全链路执行结果回调。多线程编排一站式解决方案。来自于京东主App后台
怎么用
1、引入Maven依赖
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<dependency>
<groupId>com.gitee.jd-platform-opensource</groupId>
<artifactId>asyncTool</artifactId>
<version>V1.4-SNAPSHOT</version>
</dependency>
2、自定义任务类
IWorker<Input, Output>是一个泛型接口,其中:
Input代表入参类型,Output代表返回值类型
下面的示例中,入参类型为fastjson包中的JSONObject类型,返回值类型为String字符串类型
@Slf4j
public class JobWorker implements IWorker<JSONObject, String>, ICallback<JSONObject, String> {
@Override
public void begin() {
log.info("任务开始执行");
}
@Override
public void result(boolean b, JSONObject params, WorkResult<String> workResult) {
log.info("任务执行完毕, 成功:{}, 入参:{}, 输出结果:{}", b, params, workResult);
}
@Override
public String action(JSONObject params, Map<String, WorkerWrapper> map) {
// 这里是要执行的耗时操作
return "";
}
@Override
public String defaultValue() {
return "自定义默认值";
}
}
3、任务编排执行
JobWorker w = new JobWorker();
WorkerWrapper<JSONObject, String> workerWrapper = new WorkerWrapper
.Builder<JSONObject, String>()
.worker(w)
.callback(w)
.param(param)
.build();
try {
Async.beginWork(60000, workerWrapper);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
WorkResult<String> workResult = workerWrapper.getWorkResult();
log.info("result: {}", workResult);
以后就跟博主混了
写的很详细具体,学习到了,多谢博主的分享!⌇●﹏●⌇