From e708f0713cd86060b79e9ee37bde646c89675e20 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Fri, 19 Apr 2019 13:50:15 +0300 Subject: [PATCH] Returns CompletionStage instead of CompletableFuture in Async API --- demo/app/src/main/java/org/pf4j/demo/Boot.java | 12 ++++++------ pf4j/src/main/java/org/pf4j/AsyncPluginManager.java | 6 +++--- .../java/org/pf4j/DefaultAsyncPluginManager.java | 9 +++++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/demo/app/src/main/java/org/pf4j/demo/Boot.java b/demo/app/src/main/java/org/pf4j/demo/Boot.java index c4ddc3f..904b32f 100644 --- a/demo/app/src/main/java/org/pf4j/demo/Boot.java +++ b/demo/app/src/main/java/org/pf4j/demo/Boot.java @@ -27,7 +27,7 @@ import org.pf4j.demo.api.Greeting; import java.util.List; import java.util.Set; -import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; /** * A boot class that start the demo. @@ -55,19 +55,19 @@ public class Boot { // load the plugins // pluginManager.loadPlugins(); - CompletableFuture feature = pluginManager.loadPluginsAsync(); - feature.thenRun(() -> System.out.println("Plugins loaded")); + CompletionStage stage = pluginManager.loadPluginsAsync(); + stage.thenRun(() -> System.out.println("Plugins loaded")); // enable a disabled plugin // pluginManager.enablePlugin("welcome-plugin"); // start (active/resolved) the plugins // pluginManager.startPlugins(); - feature.thenCompose(v -> pluginManager.startPluginsAsync()); - feature.thenRun(() -> System.out.println("Plugins started")); + stage.thenCompose(v -> pluginManager.startPluginsAsync()); + stage.thenRun(() -> System.out.println("Plugins started")); // block and wait for the future to complete (not the best approach in real applications) - feature.get(); + stage.toCompletableFuture().get(); // retrieves the extensions for Greeting extension point List greetings = pluginManager.getExtensions(Greeting.class); diff --git a/pf4j/src/main/java/org/pf4j/AsyncPluginManager.java b/pf4j/src/main/java/org/pf4j/AsyncPluginManager.java index 5f3c12b..d1195d6 100644 --- a/pf4j/src/main/java/org/pf4j/AsyncPluginManager.java +++ b/pf4j/src/main/java/org/pf4j/AsyncPluginManager.java @@ -15,7 +15,7 @@ */ package org.pf4j; -import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; /** * A {@link PluginManager} that supports asynchronous operation with the methods ending in {@code Async}. @@ -24,8 +24,8 @@ import java.util.concurrent.CompletableFuture; */ public interface AsyncPluginManager extends PluginManager { - CompletableFuture loadPluginsAsync(); + CompletionStage loadPluginsAsync(); - CompletableFuture startPluginsAsync(); + CompletionStage startPluginsAsync(); } diff --git a/pf4j/src/main/java/org/pf4j/DefaultAsyncPluginManager.java b/pf4j/src/main/java/org/pf4j/DefaultAsyncPluginManager.java index 35d3d9a..ecbc3ff 100644 --- a/pf4j/src/main/java/org/pf4j/DefaultAsyncPluginManager.java +++ b/pf4j/src/main/java/org/pf4j/DefaultAsyncPluginManager.java @@ -23,6 +23,7 @@ import java.nio.file.Path; import java.util.List; import java.util.Objects; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; /** * An extension of {@link DefaultPluginManager} that supports asynchronous methods (@{AsyncPluginManager}). @@ -34,7 +35,7 @@ public class DefaultAsyncPluginManager extends DefaultPluginManager implements A private static final Logger log = LoggerFactory.getLogger(DefaultAsyncPluginManager.class); @Override - public CompletableFuture loadPluginsAsync() { + public CompletionStage loadPluginsAsync() { Path pluginsRoot = getPluginsRoot(); PluginRepository pluginRepository = getPluginRepository(); @@ -75,7 +76,7 @@ public class DefaultAsyncPluginManager extends DefaultPluginManager implements A } @Override - public CompletableFuture startPluginsAsync() { + public CompletionStage startPluginsAsync() { /* // chain start plugins one after another CompletableFuture feature = CompletableFuture.completedFuture(null); @@ -91,7 +92,7 @@ public class DefaultAsyncPluginManager extends DefaultPluginManager implements A .toArray(CompletableFuture[]::new)); } - protected CompletableFuture loadPluginFromPathAsync(Path pluginPath) { + protected CompletionStage loadPluginFromPathAsync(Path pluginPath) { return CompletableFuture.supplyAsync(() -> { try { return loadPluginFromPath(pluginPath); @@ -102,7 +103,7 @@ public class DefaultAsyncPluginManager extends DefaultPluginManager implements A }); } - protected CompletableFuture startPluginAsync(PluginWrapper pluginWrapper) { + protected CompletionStage startPluginAsync(PluginWrapper pluginWrapper) { return CompletableFuture.runAsync(() -> { PluginState pluginState = pluginWrapper.getPluginState(); if ((PluginState.DISABLED != pluginState) && (PluginState.STARTED != pluginState)) { -- 2.39.5