mirror of
https://gitee.com/jd-platform-opensource/asyncTool.git
synced 2024-12-19 03:30:30 +08:00
Feature:add lambda support
This commit is contained in:
parent
0b2ab3347a
commit
55118f9b8b
@ -6,15 +6,21 @@ import com.jd.platform.async.worker.WorkResult;
|
||||
/**
|
||||
* 每个执行单元执行完毕后,会回调该接口</p>
|
||||
* 需要监听执行结果的,实现该接口即可
|
||||
*
|
||||
* @author wuweifeng wrote on 2019-11-19.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ICallback<T, V> {
|
||||
|
||||
void begin();
|
||||
/**
|
||||
* 任务开始的监听
|
||||
*/
|
||||
default void begin() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 耗时操作执行完毕后,就给value注入值
|
||||
*
|
||||
*/
|
||||
void result(boolean success, T param, WorkResult<V> workResult);
|
||||
}
|
||||
|
@ -1,25 +1,30 @@
|
||||
package com.jd.platform.async.callback;
|
||||
|
||||
import com.jd.platform.async.wrapper.WorkerWrapper;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.jd.platform.async.wrapper.WorkerWrapper;
|
||||
|
||||
/**
|
||||
* 每个最小执行单元需要实现该接口
|
||||
*
|
||||
* @author wuweifeng wrote on 2019-11-19.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface IWorker<T, V> {
|
||||
/**
|
||||
* 在这里做耗时操作,如rpc请求、IO等
|
||||
*
|
||||
* @param object
|
||||
* object
|
||||
* @param object object
|
||||
* @param allWrappers 任务包装
|
||||
*/
|
||||
V action(T object, Map<String, WorkerWrapper> allWrappers);
|
||||
|
||||
/**
|
||||
* 超时、异常时,返回的默认值
|
||||
*
|
||||
* @return 默认值
|
||||
*/
|
||||
V defaultValue();
|
||||
default V defaultValue() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
74
src/test/java/depend/LambdaTest.java
Normal file
74
src/test/java/depend/LambdaTest.java
Normal file
@ -0,0 +1,74 @@
|
||||
package depend;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.jd.platform.async.executor.Async;
|
||||
import com.jd.platform.async.worker.WorkResult;
|
||||
import com.jd.platform.async.wrapper.WorkerWrapper;
|
||||
|
||||
/**
|
||||
* @author sjsdfg
|
||||
* @since 2020/6/14
|
||||
*/
|
||||
public class LambdaTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
WorkerWrapper<WorkResult<User>, String> workerWrapper2 = new WorkerWrapper.Builder<WorkResult<User>, String>()
|
||||
.worker((WorkResult<User> result, Map<String, WorkerWrapper> allWrappers) -> {
|
||||
System.out.println("par2的入参来自于par1: " + result.getResult());
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result.getResult().getName();
|
||||
})
|
||||
.callback((boolean success, WorkResult<User> param, WorkResult<String> workResult) ->
|
||||
System.out.println(String.format("thread is %s, param is %s, result is %s", Thread.currentThread().getName(), param, workResult)))
|
||||
.id("third")
|
||||
.build();
|
||||
|
||||
WorkerWrapper<WorkResult<User>, User> workerWrapper1 = new WorkerWrapper.Builder<WorkResult<User>, User>()
|
||||
.worker((WorkResult<User> result, Map<String, WorkerWrapper> allWrappers) -> {
|
||||
System.out.println("par1的入参来自于par0: " + result.getResult());
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new User("user1");
|
||||
})
|
||||
.callback((boolean success, WorkResult<User> param, WorkResult<User> workResult) ->
|
||||
System.out.println(String.format("thread is %s, param is %s, result is %s", Thread.currentThread().getName(), param, workResult)))
|
||||
.id("second")
|
||||
.next(workerWrapper2)
|
||||
.build();
|
||||
|
||||
WorkerWrapper<String, User> workerWrapper = new WorkerWrapper.Builder<String, User>()
|
||||
.worker((String object, Map<String, WorkerWrapper> allWrappers) -> {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new User("user0");
|
||||
})
|
||||
.param("0")
|
||||
.id("first")
|
||||
.next(workerWrapper1, true)
|
||||
.callback((boolean success, String param, WorkResult<User> workResult) ->
|
||||
System.out.println(String.format("thread is %s, param is %s, result is %s", Thread.currentThread().getName(), param, workResult)))
|
||||
.build();
|
||||
|
||||
//虽然尚未执行,但是也可以先取得结果的引用,作为下一个任务的入参。V1.2前写法,需要手工给
|
||||
//V1.3后,不用给wrapper setParam了,直接在worker的action里自行根据id获取即可.参考dependnew包下代码
|
||||
WorkResult<User> result = workerWrapper.getWorkResult();
|
||||
WorkResult<User> result1 = workerWrapper1.getWorkResult();
|
||||
workerWrapper1.setParam(result);
|
||||
workerWrapper2.setParam(result1);
|
||||
|
||||
Async.beginWork(3500, workerWrapper);
|
||||
|
||||
System.out.println(workerWrapper2.getWorkResult());
|
||||
Async.shutDown();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user