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

Bug 490 #491

Closed
wants to merge 4 commits into from
Closed
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.cola.extension.Extension;
import com.alibaba.cola.extension.ExtensionPointI;
import com.alibaba.cola.extension.Extensions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand All @@ -18,6 +19,7 @@
* @author Frank Zhang
* @date 2020-06-18 7:55 PM
*/
@Slf4j
@Component
public class ExtensionBootstrap implements ApplicationContextAware {

Expand All @@ -28,14 +30,16 @@ public class ExtensionBootstrap implements ApplicationContextAware {

@PostConstruct
public void init(){
Map<String, Object> extensionBeans = applicationContext.getBeansWithAnnotation(Extension.class);
extensionBeans.values().forEach(
extension -> extensionRegister.doRegistration((ExtensionPointI) extension)
);

// handle @Extensions annotation
Map<String, Object> extensionsBeans = applicationContext.getBeansWithAnnotation(Extensions.class);
extensionsBeans.values().forEach( extension -> extensionRegister.doRegistrationExtensions((ExtensionPointI) extension));
Map<String, ExtensionPointI> extMap = applicationContext.getBeansOfType(ExtensionPointI.class);
for (ExtensionPointI ext : extMap.values()) {
if (ext.getClass().isAnnotationPresent(Extension.class)) {
extensionRegister.doRegistration(ext);
}else if (ext.getClass().isAnnotationPresent(Extensions.class)){
extensionRegister.doRegistrationExtensions(ext);
}else {
log.error("There is no annotation for @Extension or @Extension on this extension class:{}" , ext.getClass());
}
}
}

@Override
Expand Down
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
Loading