Tuesday, May 01, 2018

ECF Photon supports OSGi Async Intent

In a previous post, I indicated that ECF Photon will support OSGi R7.   What does this mean for  remote service developers?

Support osgi.async Remote Service Intent

The OSGi R7 Remote Services specification has been enhanced with new remote service intents.  Remote Service Intents allow remote service authors to specify requirements on the underlying distribution system.   The osgi.async intent allows the service interface to use return types like Java8's CompletableFuture or OSGi's Promise.   When a distribution provider supports this intent, the remote service proxy will automatically implement the asynchronous/non-blocking behavior for the service consumer.

For example, consider a service interface:
public interface Hello {
    CompletableFuture<String> hello(String greetingMessage);
When an implementation of this service is registered and exported as a remote service:
@Component(property = { "service.exported.interfaces=*", "service.intents=osgi.async" })
public class HelloImpl implements Hello {
    public CompletableFuture hello(String greetingMessage) {
          CompletableFuture<String> future = new CompletableFuture<String>();
          future.complete("Hi.  This a response to the greeting: "+greetingMessage);
          return future;
Then when a Hello servicee consumer discovers, imports and the remote service is injected by DS:
public class HelloConsumer {

    private Hello helloService;

    void activate() throws Exception {
        // Call helloService.hello remote service without blocking
        helloService.hello("hi there").whenComplete((result,exception) -> {
            if (exception != null)
                System.out.println("hello service responds: " + result);
The injected helloService instance will automatically implement the asynchronous/non-blocking remote call via the distribution provider.   No need for the consumer to implement anything other than calling the 'hello' method and handling the response via implementing whenComplete.   The Java8 CompletionStage, Future, and OSGi's Promise are also supported return types.

This makes it very easy to define, implement, and efficiently use loosely-coupled asynchronous remote services.   It also makes remote service contracts transport independent, allowing the swapping of distribution providers or custom providers without changes to the service interface contract.

No comments: