Interface FutureDependencyBuilder<F>

Type Parameters:
F - the type of the CompletableFuture result.
All Superinterfaces:
DependencyBuilder<org.apache.felix.dm.Dependency>

public interface FutureDependencyBuilder<F> extends DependencyBuilder<org.apache.felix.dm.Dependency>
Defines a builder for a CompletableFuture 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.

Usage Example

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.
   }
 }
 
 
  • Method Details

    • complete

      FutureDependencyBuilder<F> complete(String callback)
      Sets the callback method name to invoke on the component implementation, once the CompletableFuture has completed.
      Parameters:
      callback - the callback method name to invoke on the component implementation, once the CompletableFuture on which we depend has completed.
      Returns:
      this dependency.
    • complete

      FutureDependencyBuilder<F> complete(Object callbackInstance, String callback)
      Sets the callback instance method name to invoke on a given Object instance, once the CompletableFuture has completed.
      Parameters:
      callbackInstance - the object instance on which the callback must be invoked
      callback - the callback method name to invoke on Object instance, once the CompletableFuture has completed.
      Returns:
      this dependency.
    • complete

      <T> FutureDependencyBuilder<F> complete(CbFuture<T,? super F> callback)
      Sets the function to invoke when the future task has completed. The function is from one of the Component implementation classes, and it accepts the result of the completed future.
      Type Parameters:
      T - the type of the CompletableFuture result.
      Parameters:
      callback - the function to perform when the future task as completed.
      Returns:
      this dependency
    • complete

      <T> FutureDependencyBuilder<F> complete(CbFuture<T,? super F> callback, boolean async)
      Sets the function to invoke asynchronously when the future task has completed. The function is from one of the Component implementation classes, and it accepts the result of the completed future.
      Type Parameters:
      T - the type of the CompletableFuture result.
      Parameters:
      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.
      Returns:
      this dependency
    • complete

      <T> FutureDependencyBuilder<F> complete(CbFuture<T,? super F> callback, Executor executor)
      Sets the function to invoke asynchronously when the future task has completed. The function is from one of the Component implementation classes, and it accepts the result of the completed future.
      Type Parameters:
      T - the type of the CompletableFuture result.
      Parameters:
      callback - the function to perform when the future task as completed.
      executor - the executor used to schedule the callback.
      Returns:
      this dependency
    • complete

      FutureDependencyBuilder<F> complete(InstanceCbFuture<? super F> callback)
      Sets the callback instance to invoke when the future task has completed. The callback is a Consumer instance which accepts the result of the completed future.
      Parameters:
      callback - a Consumer instance which accepts the result of the completed future.
      Returns:
      this dependency
    • complete

      FutureDependencyBuilder<F> complete(InstanceCbFuture<? super F> callback, boolean async)
      Sets the callback instance to invoke when the future task has completed. The callback is a Consumer instance which accepts the result of the completed future.
      Parameters:
      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.
      Returns:
      this dependency
    • complete

      FutureDependencyBuilder<F> complete(InstanceCbFuture<? super F> callback, Executor executor)
      Sets the callback instance to invoke when the future task has completed. The callback is a Consumer instance which accepts the result of the completed future.
      Parameters:
      callback - the action to perform when the future task as completed.
      executor - the executor to use for asynchronous execution of the callback.
      Returns:
      this dependency