Async
PF4J doesn’t supply an Async API out of the box.
I added a POC (Proof Of Concept) on the async branch.
I updated demo (Boot
class) to use the new Async API. Everything is functional.
I added below two code snippets (from demo/Boot.java
) that show you the API used to load&start plugins using SYNC and ASYNC variant.
In the new async API I added two new methods (until now) that end with Async
:
- loadPluginsAsync
- startPluginsAsync
Load and start plugins sync
// create the plugin manager
final PluginManager pluginManager = new DefaultPluginManager();
// load the plugins
pluginManager.loadPlugins();
// start (active/resolved) the plugins
pluginManager.startPlugins();
// retrieves the extensions for Greeting extension point
List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
Load and start plugins async
// create the plugin manager
final AsyncPluginManager pluginManager = new DefaultAsyncPluginManager();
// load the plugins
CompletionStage<Void> stage = pluginManager.loadPluginsAsync();
stage.thenRun(() -> System.out.println("Plugins loaded")); // optional
// start (active/resolved) the plugins
stage.thenCompose(v -> pluginManager.startPluginsAsync());
stage.thenRun(() -> System.out.println("Plugins started")); // optional
// block and wait for the future to complete (not the best approach in real applications)
stage.toCompletableFuture().get();
// retrieves the extensions for Greeting extension point
List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
I am not sure yet if the variant with new AsyncPluginManager
interface and DefaultAsyncPluginManager
class is the best design option. I don’t know if the variant with add the new async methods directly in PluginManager
is not better.
For the entire discussion about the Async API request, please see this.