Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support async execute api. #366

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.alibaba.cola.extension.BizScenario;
import com.alibaba.cola.extension.ExtensionCoordinate;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -31,6 +35,26 @@ public <R, T> R execute(ExtensionCoordinate extensionCoordinate, Function<T, R>
return execute((Class<T>) extensionCoordinate.getExtensionPointClass(), extensionCoordinate.getBizScenario(), exeFunction);
}

public <R, T> CompletableFuture<R> executeAsync(Class<T> targetClz, BizScenario bizScenario, Function<T, R> exeFunction) {
return executeAsync(targetClz, bizScenario, exeFunction, null);
}

/**
* the async execute api. if the param {executor} is null, will use forkJoin pool
* other similar.
*/
public <R, T> CompletableFuture<R> executeAsync(Class<T> targetClz, BizScenario bizScenario, Function<T, R> exeFunction, Executor executor) {
return CompletableFuture.supplyAsync(() -> execute(targetClz, bizScenario, exeFunction), Objects.nonNull(executor) ? executor : ForkJoinPool.commonPool());
}

public <R, T> CompletableFuture<R> executeAsync(ExtensionCoordinate extensionCoordinate, Function<T, R> exeFunction) {
return executeAsync(extensionCoordinate, exeFunction, null);
}

public <R, T> CompletableFuture<R> executeAsync(ExtensionCoordinate extensionCoordinate, Function<T, R> exeFunction, Executor executor) {
return CompletableFuture.supplyAsync(() -> execute(extensionCoordinate, exeFunction), Objects.nonNull(executor) ? executor : ForkJoinPool.commonPool());
}

/**
* Execute extension without Response
*
Expand All @@ -48,5 +72,21 @@ public <T> void executeVoid(ExtensionCoordinate extensionCoordinate, Consumer<T>
executeVoid(extensionCoordinate.getExtensionPointClass(), extensionCoordinate.getBizScenario(), exeFunction);
}

public <T> void executeVoidAsync(Class<T> targetClz, BizScenario context, Consumer<T> exeFunction) {
executeVoidAsync(targetClz, context, exeFunction, null);
}

public <T> void executeVoidAsync(Class<T> targetClz, BizScenario context, Consumer<T> exeFunction, Executor executor) {
CompletableFuture.runAsync(() -> executeVoid(targetClz, context, exeFunction), Objects.nonNull(executor) ? executor : ForkJoinPool.commonPool());
}

public <T> void executeVoidAsync(ExtensionCoordinate extensionCoordinate, Consumer<T> exeFunction) {
executeVoidAsync(extensionCoordinate, exeFunction, null);
}

public <T> void executeVoidAsync(ExtensionCoordinate extensionCoordinate, Consumer<T> exeFunction, Executor executor) {
CompletableFuture.runAsync(() -> executeVoid(extensionCoordinate, exeFunction), Objects.nonNull(executor) ? executor : ForkJoinPool.commonPool());
}

protected abstract <C> C locateComponent(Class<C> targetClz, BizScenario context);
}
1 change: 1 addition & 0 deletions cola-components/cola-component-statemachine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</parent>

<artifactId>cola-component-statemachine</artifactId>
<version>4.4.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>${project.artifactId}</description>
Expand Down