mirror of
https://gitee.com/jd-platform-opensource/asyncTool.git
synced 2024-12-31 15:35:34 +08:00
增加“后面请求依赖于前面请求的执行结果”的测试案例
This commit is contained in:
parent
b706edc231
commit
f0ea3f1e1c
@ -1,6 +1,5 @@
|
|||||||
package com.tianyalei.async.group;
|
package com.tianyalei.async.group;
|
||||||
|
|
||||||
|
|
||||||
import com.tianyalei.async.callback.DefaultCallback;
|
import com.tianyalei.async.callback.DefaultCallback;
|
||||||
import com.tianyalei.async.callback.ICallback;
|
import com.tianyalei.async.callback.ICallback;
|
||||||
import com.tianyalei.async.callback.IWorker;
|
import com.tianyalei.async.callback.IWorker;
|
||||||
@ -56,7 +55,7 @@ public class WorkerWrapper<T, V> {
|
|||||||
/**
|
/**
|
||||||
* 也是个钩子变量,用来存临时的结果
|
* 也是个钩子变量,用来存临时的结果
|
||||||
*/
|
*/
|
||||||
private volatile WorkResult<V> workResult;
|
private volatile WorkResult<V> workResult = WorkResult.defaultResult();
|
||||||
|
|
||||||
private static final int FINISH = 1;
|
private static final int FINISH = 1;
|
||||||
private static final int ERROR = 2;
|
private static final int ERROR = 2;
|
||||||
@ -205,7 +204,7 @@ public class WorkerWrapper<T, V> {
|
|||||||
WorkerWrapper workerWrapper = dependWrapper.getDependWrapper();
|
WorkerWrapper workerWrapper = dependWrapper.getDependWrapper();
|
||||||
WorkResult tempWorkResult = workerWrapper.getWorkResult();
|
WorkResult tempWorkResult = workerWrapper.getWorkResult();
|
||||||
//为null或者isWorking,说明它依赖的某个任务还没执行到或没执行完
|
//为null或者isWorking,说明它依赖的某个任务还没执行到或没执行完
|
||||||
if (tempWorkResult == null || workerWrapper.getState() == WORKING) {
|
if (workerWrapper.getState() == INIT || workerWrapper.getState() == WORKING) {
|
||||||
existNoFinish = true;
|
existNoFinish = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -276,7 +275,8 @@ public class WorkerWrapper<T, V> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (workResult == null) {
|
//尚未处理过结果
|
||||||
|
if (checkIsNullResult()) {
|
||||||
if (e == null) {
|
if (e == null) {
|
||||||
workResult = defaultResult();
|
workResult = defaultResult();
|
||||||
} else {
|
} else {
|
||||||
@ -293,7 +293,7 @@ public class WorkerWrapper<T, V> {
|
|||||||
*/
|
*/
|
||||||
private WorkResult<V> workerDoJob() {
|
private WorkResult<V> workerDoJob() {
|
||||||
//避免重复执行
|
//避免重复执行
|
||||||
if (workResult != null) {
|
if (!checkIsNullResult()) {
|
||||||
return workResult;
|
return workResult;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@ -307,20 +307,20 @@ public class WorkerWrapper<T, V> {
|
|||||||
//执行耗时操作
|
//执行耗时操作
|
||||||
V resultValue = worker.action(getParam());
|
V resultValue = worker.action(getParam());
|
||||||
|
|
||||||
WorkResult<V> tempResult = new WorkResult<>(resultValue, ResultState.SUCCESS);
|
|
||||||
|
|
||||||
//如果状态不是在working,说明别的地方已经修改了
|
//如果状态不是在working,说明别的地方已经修改了
|
||||||
if (!compareAndSetState(WORKING, FINISH)) {
|
if (!compareAndSetState(WORKING, FINISH)) {
|
||||||
return workResult;
|
return workResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
workResult.setResultState(ResultState.SUCCESS);
|
||||||
|
workResult.setResult(resultValue);
|
||||||
//回调成功
|
//回调成功
|
||||||
callback.result(true, getParam(), tempResult);
|
callback.result(true, getParam(), workResult);
|
||||||
workResult = tempResult;
|
|
||||||
|
|
||||||
return workResult;
|
return workResult;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//避免重复回调
|
//避免重复回调
|
||||||
if (workResult != null) {
|
if (!checkIsNullResult()) {
|
||||||
return workResult;
|
return workResult;
|
||||||
}
|
}
|
||||||
fastFail(WORKING, e);
|
fastFail(WORKING, e);
|
||||||
@ -328,6 +328,10 @@ public class WorkerWrapper<T, V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean checkIsNullResult() {
|
||||||
|
return ResultState.DEFAULT == workResult.getResultState();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public WorkerWrapper addNext(WorkerWrapper<?, ?>... nextWrappers) {
|
public WorkerWrapper addNext(WorkerWrapper<?, ?>... nextWrappers) {
|
||||||
if (nextWrappers == null) {
|
if (nextWrappers == null) {
|
||||||
@ -385,11 +389,16 @@ public class WorkerWrapper<T, V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private WorkResult<V> defaultResult() {
|
private WorkResult<V> defaultResult() {
|
||||||
return new WorkResult<>(getWorker().defaultValue(), ResultState.TIMEOUT);
|
workResult.setResultState(ResultState.TIMEOUT);
|
||||||
|
workResult.setResult(getWorker().defaultValue());
|
||||||
|
return workResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
private WorkResult<V> defaultExResult(Exception ex) {
|
private WorkResult<V> defaultExResult(Exception ex) {
|
||||||
return new WorkResult<>(getWorker().defaultValue(), ResultState.EXCEPTION, ex);
|
workResult.setResultState(ResultState.EXCEPTION);
|
||||||
|
workResult.setResult(getWorker().defaultValue());
|
||||||
|
workResult.setEx(ex);
|
||||||
|
return workResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
private WorkResult<V> getNoneNullWorkResult() {
|
private WorkResult<V> getNoneNullWorkResult() {
|
||||||
|
@ -7,5 +7,6 @@ package com.tianyalei.async.worker;
|
|||||||
public enum ResultState {
|
public enum ResultState {
|
||||||
SUCCESS,
|
SUCCESS,
|
||||||
TIMEOUT,
|
TIMEOUT,
|
||||||
EXCEPTION
|
EXCEPTION,
|
||||||
|
DEFAULT //默认状态
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ public class WorkResult<V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static WorkResult defaultResult() {
|
public static WorkResult defaultResult() {
|
||||||
return new WorkResult<>(null, ResultState.TIMEOUT);
|
return new WorkResult<>(null, ResultState.DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
40
src/main/java/com/tianyalei/test/depend/ParWorker.java
Executable file
40
src/main/java/com/tianyalei/test/depend/ParWorker.java
Executable file
@ -0,0 +1,40 @@
|
|||||||
|
package com.tianyalei.test.depend;
|
||||||
|
|
||||||
|
|
||||||
|
import com.tianyalei.async.callback.ICallback;
|
||||||
|
import com.tianyalei.async.callback.IWorker;
|
||||||
|
import com.tianyalei.async.executor.timer.SystemClock;
|
||||||
|
import com.tianyalei.async.worker.WorkResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wuweifeng wrote on 2019-11-20.
|
||||||
|
*/
|
||||||
|
public class ParWorker implements IWorker<String, User>, ICallback<String, User> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User action(String object) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return new User("user0");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User defaultValue() {
|
||||||
|
return new User("default User");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void begin() {
|
||||||
|
//System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void result(boolean success, String param, WorkResult<User> workResult) {
|
||||||
|
System.out.println("worker0 的结果是:" + workResult.getResult());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
src/main/java/com/tianyalei/test/depend/ParWorker1.java
Executable file
40
src/main/java/com/tianyalei/test/depend/ParWorker1.java
Executable file
@ -0,0 +1,40 @@
|
|||||||
|
package com.tianyalei.test.depend;
|
||||||
|
|
||||||
|
|
||||||
|
import com.tianyalei.async.callback.ICallback;
|
||||||
|
import com.tianyalei.async.callback.IWorker;
|
||||||
|
import com.tianyalei.async.worker.WorkResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wuweifeng wrote on 2019-11-20.
|
||||||
|
*/
|
||||||
|
public class ParWorker1 implements IWorker<WorkResult<User>, User>, ICallback<WorkResult<User>, User> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User action(WorkResult<User> result) {
|
||||||
|
System.out.println("par1的入参来自于par0: " + result.getResult());
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return new User("user1");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User defaultValue() {
|
||||||
|
return new User("default User");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void begin() {
|
||||||
|
//System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void result(boolean success, WorkResult<User> param, WorkResult<User> workResult) {
|
||||||
|
System.out.println("worker1 的结果是:" + workResult.getResult());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
src/main/java/com/tianyalei/test/depend/ParWorker2.java
Executable file
40
src/main/java/com/tianyalei/test/depend/ParWorker2.java
Executable file
@ -0,0 +1,40 @@
|
|||||||
|
package com.tianyalei.test.depend;
|
||||||
|
|
||||||
|
|
||||||
|
import com.tianyalei.async.callback.ICallback;
|
||||||
|
import com.tianyalei.async.callback.IWorker;
|
||||||
|
import com.tianyalei.async.worker.WorkResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wuweifeng wrote on 2019-11-20.
|
||||||
|
*/
|
||||||
|
public class ParWorker2 implements IWorker<WorkResult<User>, String>, ICallback<WorkResult<User>, String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String action(WorkResult<User> result) {
|
||||||
|
System.out.println("par2的入参来自于par1: " + result.getResult());
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return result.getResult().getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String defaultValue() {
|
||||||
|
return "default";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void begin() {
|
||||||
|
//System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void result(boolean success, WorkResult<User> param, WorkResult<String> workResult) {
|
||||||
|
System.out.println("worker2 的结果是:" + workResult.getResult());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
39
src/main/java/com/tianyalei/test/depend/Test.java
Normal file
39
src/main/java/com/tianyalei/test/depend/Test.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package com.tianyalei.test.depend;
|
||||||
|
|
||||||
|
import com.tianyalei.async.executor.Async;
|
||||||
|
import com.tianyalei.async.group.WorkerWrapper;
|
||||||
|
import com.tianyalei.async.worker.WorkResult;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后面请求依赖于前面请求的执行结果
|
||||||
|
* @author wuweifeng wrote on 2019-12-26
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||||
|
ParWorker w = new ParWorker();
|
||||||
|
ParWorker1 w1 = new ParWorker1();
|
||||||
|
ParWorker2 w2 = new ParWorker2();
|
||||||
|
|
||||||
|
WorkerWrapper<String, User> workerWrapper = new WorkerWrapper<>(w, "0", w);
|
||||||
|
//虽然尚未执行,但是也可以先取得结果的引用,作为下一个任务的入参
|
||||||
|
WorkResult<User> result = workerWrapper.getWorkResult();
|
||||||
|
|
||||||
|
WorkerWrapper<WorkResult<User>, User> workerWrapper1 = new WorkerWrapper<>(w1, result, w1);
|
||||||
|
WorkResult<User> result1 = workerWrapper1.getWorkResult();
|
||||||
|
|
||||||
|
WorkerWrapper<WorkResult<User>, String> workerWrapper2 = new WorkerWrapper<>(w2, result1, w2);
|
||||||
|
|
||||||
|
workerWrapper.addNext(workerWrapper1);
|
||||||
|
workerWrapper1.addNext(workerWrapper2);
|
||||||
|
|
||||||
|
Async.beginWork(3500, workerWrapper);
|
||||||
|
|
||||||
|
System.out.println(workerWrapper2.getWorkResult());
|
||||||
|
Async.shutDown();
|
||||||
|
}
|
||||||
|
}
|
29
src/main/java/com/tianyalei/test/depend/User.java
Normal file
29
src/main/java/com/tianyalei/test/depend/User.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package com.tianyalei.test.depend;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一个包装类
|
||||||
|
* @author wuweifeng wrote on 2019-12-26
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public class User {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public User(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -10,7 +10,7 @@ import java.util.concurrent.ExecutionException;
|
|||||||
import static com.tianyalei.async.executor.Async.getThreadCount;
|
import static com.tianyalei.async.executor.Async.getThreadCount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 串行测试
|
* 并行测试
|
||||||
*
|
*
|
||||||
* @author wuweifeng wrote on 2019-11-20.
|
* @author wuweifeng wrote on 2019-11-20.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user