enhancement #I3DM92 集成asyncTool作为线程编排的核心

This commit is contained in:
bryan31
2021-11-08 19:48:18 +08:00
parent fe0aedc8e2
commit 1ccf8251a3
8 changed files with 195 additions and 4 deletions

View File

@@ -11,9 +11,19 @@
<artifactId>liteflow-async-tool</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${hutool-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springboot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,91 @@
package com.yomahub.liteflow.asynctool.test.seqwork;
import com.yomahub.liteflow.asynctool.executor.Async;
import com.yomahub.liteflow.asynctool.test.seqwork.work.*;
import com.yomahub.liteflow.asynctool.wrapper.WorkerWrapper;
import org.junit.Test;
public class SeqWorkTest {
@Test
public void test1(){
SeqWork1 seqWork1 = new SeqWork1();
SeqWork2 seqWork2 = new SeqWork2();
SeqWork3 seqWork3 = new SeqWork3();
Callback1 callback1 = new Callback1();
Callback2 callback2 = new Callback2();
Callback3 callback3 = new Callback3();
WorkerWrapper<String, String> workerWrapper1 = new WorkerWrapper.Builder<String, String>()
.worker(seqWork1)
.callback(callback1)
.param("param1")
.build();
WorkerWrapper<String, String> workerWrapper2 = new WorkerWrapper.Builder<String, String>()
.worker(seqWork2)
.callback(callback2)
.param("param2")
.depend(workerWrapper1)
.build();
WorkerWrapper<String, String> workerWrapper3 = new WorkerWrapper.Builder<String, String>()
.worker(seqWork3)
.callback(callback3)
.param("param3")
.depend(workerWrapper2)
.build();
try{
boolean flag = Async.beginWork(3500,workerWrapper1);
System.out.println(workerWrapper3.getWorkResult().getResultState());
System.out.println(flag);
}catch (Exception e){
e.printStackTrace();
}finally {
Async.shutDown();
}
}
@Test
public void test2(){
SeqWork1 seqWork1 = new SeqWork1();
SeqWork2 seqWork2 = new SeqWork2();
SeqWork3 seqWork3 = new SeqWork3();
Callback1 callback1 = new Callback1();
Callback2 callback2 = new Callback2();
Callback3 callback3 = new Callback3();
WorkerWrapper<String, String> workerWrapper1 = new WorkerWrapper.Builder<String, String>()
.worker(seqWork1)
.callback(callback1)
.param("param1")
.build();
WorkerWrapper<String, String> workerWrapper2 = new WorkerWrapper.Builder<String, String>()
.worker(seqWork2)
.callback(callback2)
.param("param2")
.depend(workerWrapper1)
.build();
WorkerWrapper<String, String> workerWrapper3 = new WorkerWrapper.Builder<String, String>()
.worker(seqWork3)
.callback(callback3)
.param("param3")
.depend(workerWrapper2)
.build();
try{
boolean flag = Async.beginWork(2500,workerWrapper1);
System.out.println(workerWrapper3.getWorkResult().getResultState());
System.out.println(flag);
}catch (Exception e){
e.printStackTrace();
}finally {
Async.shutDown();
}
}
}

View File

@@ -0,0 +1,12 @@
package com.yomahub.liteflow.asynctool.test.seqwork.work;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.asynctool.callback.ICallback;
import com.yomahub.liteflow.asynctool.worker.WorkResult;
public class Callback1 implements ICallback<String, String> {
@Override
public void result(boolean success, String param, WorkResult<String> workResult) {
System.out.println(StrUtil.format("开始执行{},结果为{},参数为{},workResult为{}", this.getClass().getSimpleName(), success, param, workResult.getResult()));
}
}

View File

@@ -0,0 +1,12 @@
package com.yomahub.liteflow.asynctool.test.seqwork.work;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.asynctool.callback.ICallback;
import com.yomahub.liteflow.asynctool.worker.WorkResult;
public class Callback2 implements ICallback<String, String> {
@Override
public void result(boolean success, String param, WorkResult<String> workResult) {
System.out.println(StrUtil.format("开始执行{},结果为{},参数为{},workResult为{}", this.getClass().getSimpleName(), success, param, workResult.getResult()));
}
}

View File

@@ -0,0 +1,12 @@
package com.yomahub.liteflow.asynctool.test.seqwork.work;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.asynctool.callback.ICallback;
import com.yomahub.liteflow.asynctool.worker.WorkResult;
public class Callback3 implements ICallback<String, String> {
@Override
public void result(boolean success, String param, WorkResult<String> workResult) {
System.out.println(StrUtil.format("开始执行{},结果为{},参数为{},workResult为{}", this.getClass().getSimpleName(), success, param, workResult.getResult()));
}
}

View File

@@ -0,0 +1,18 @@
package com.yomahub.liteflow.asynctool.test.seqwork.work;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.asynctool.callback.IWorker;
import com.yomahub.liteflow.asynctool.wrapper.WorkerWrapper;
import java.util.Map;
public class SeqWork1 implements IWorker<String, String> {
@Override
public String action(String object, Map<String, WorkerWrapper> allWrappers) {
System.out.println(StrUtil.format("开始执行{},参数为{},当前线程ID为{}", this.getClass().getSimpleName(), object, Thread.currentThread().getId()));
try{
Thread.sleep(1000);
}catch (Exception ignored){}
return "result1";
}
}

View File

@@ -0,0 +1,18 @@
package com.yomahub.liteflow.asynctool.test.seqwork.work;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.asynctool.callback.IWorker;
import com.yomahub.liteflow.asynctool.wrapper.WorkerWrapper;
import java.util.Map;
public class SeqWork2 implements IWorker<String, String> {
@Override
public String action(String object, Map<String, WorkerWrapper> allWrappers) {
System.out.println(StrUtil.format("开始执行{},参数为{},当前线程ID为{}", this.getClass().getSimpleName(), object, Thread.currentThread().getId()));
try{
Thread.sleep(1000);
}catch (Exception ignored){}
return "result2";
}
}

View File

@@ -0,0 +1,18 @@
package com.yomahub.liteflow.asynctool.test.seqwork.work;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.asynctool.callback.IWorker;
import com.yomahub.liteflow.asynctool.wrapper.WorkerWrapper;
import java.util.Map;
public class SeqWork3 implements IWorker<String, String> {
@Override
public String action(String object, Map<String, WorkerWrapper> allWrappers) {
System.out.println(StrUtil.format("开始执行{},参数为{},当前线程ID为{}", this.getClass().getSimpleName(), object, Thread.currentThread().getId()));
try{
Thread.sleep(1000);
}catch (Exception ignored){}
return "result3";
}
}