F
- the type of the CompletableFuture result.public interface FutureDependencyBuilder<F> extends DependencyBuilder<org.apache.felix.dm.Dependency>
Using such dependency allows your component to wait for the completion of a given asynchronous task
represented by a standard jdk CompletableFuture
object.
A FutureDependency is required and unblock the Component once the CompletableFuture result has completed.
Here is an Activator that downloads a page from the web and injects the string result to the component before it is started. When the web page is downloaded, the result is injected in the MyComponent::setPage method and the component is then called in its "start" method:
public class Activator extends DependencyManagerActivator {
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
// Download a web page asynchronously, using a CompletableFuture:
String url = "http://felix.apache.org/";
CompletableFuture<String> page = CompletableFuture.supplyAsync(() -> downloadSite(url));
// The component depends on a log service and on the content of the Felix site.
// The lambda passed to the "withFuture" method configures the callback that is
// invoked with the result of the CompletableFuture (the page content).
component(comp -> comp
.impl(MyComponent.class)
.withService(LogService.class)
.withFuture(page, result -> result.complete(MyComponent::setPage)));
}
}
public class MyComponent {
volatile LogService log; // injected.
void setPage(String page) {
// injected by the FutureDependency.
}
void start() {
// all required dependencies injected.
}
}
Modifier and Type | Method and Description |
---|---|
<T> FutureDependencyBuilder<F> |
complete(CbFuture<T,? super F> callback)
Sets the function to invoke when the future task has completed.
|
<T> FutureDependencyBuilder<F> |
complete(CbFuture<T,? super F> callback,
boolean async)
Sets the function to invoke asynchronously when the future task has completed.
|
<T> FutureDependencyBuilder<F> |
complete(CbFuture<T,? super F> callback,
java.util.concurrent.Executor executor)
Sets the function to invoke asynchronously when the future task has completed.
|
FutureDependencyBuilder<F> |
complete(InstanceCbFuture<? super F> callback)
Sets the callback instance to invoke when the future task has completed.
|
FutureDependencyBuilder<F> |
complete(InstanceCbFuture<? super F> callback,
boolean async)
Sets the callback instance to invoke when the future task has completed.
|
FutureDependencyBuilder<F> |
complete(InstanceCbFuture<? super F> callback,
java.util.concurrent.Executor executor)
Sets the callback instance to invoke when the future task has completed.
|
FutureDependencyBuilder<F> |
complete(java.lang.Object callbackInstance,
java.lang.String callback)
Sets the callback instance method name to invoke on a given Object instance, once the CompletableFuture has completed.
|
FutureDependencyBuilder<F> |
complete(java.lang.String callback)
Sets the callback method name to invoke on the component implementation, once the CompletableFuture has completed.
|
build
FutureDependencyBuilder<F> complete(java.lang.String callback)
callback
- the callback method name to invoke on the component implementation, once the CompletableFuture on which we depend has completed.FutureDependencyBuilder<F> complete(java.lang.Object callbackInstance, java.lang.String callback)
callbackInstance
- the object instance on which the callback must be invokedcallback
- the callback method name to invoke on Object instance, once the CompletableFuture has completed.<T> FutureDependencyBuilder<F> complete(CbFuture<T,? super F> callback)
T
- the type of the CompletableFuture result.callback
- the function to perform when the future task as completed.<T> FutureDependencyBuilder<F> complete(CbFuture<T,? super F> callback, boolean async)
T
- the type of the CompletableFuture result.callback
- the function to perform when the future task as completed.async
- true if the callback should be invoked asynchronously using the default jdk execution facility, false if not.<T> FutureDependencyBuilder<F> complete(CbFuture<T,? super F> callback, java.util.concurrent.Executor executor)
T
- the type of the CompletableFuture result.callback
- the function to perform when the future task as completed.executor
- the executor used to schedule the callback.FutureDependencyBuilder<F> complete(InstanceCbFuture<? super F> callback)
callback
- a Consumer instance which accepts the result of the completed future.FutureDependencyBuilder<F> complete(InstanceCbFuture<? super F> callback, boolean async)
callback
- a Consumer instance which accepts the result of the completed future.async
- true if the callback should be invoked asynchronously using the default jdk execution facility, false if not.FutureDependencyBuilder<F> complete(InstanceCbFuture<? super F> callback, java.util.concurrent.Executor executor)
callback
- the action to perform when the future task as completed.executor
- the executor to use for asynchronous execution of the callback.