Skip to main content
Jump to: navigation, search

Difference between revisions of "ECF/Asynchronous Remote Services"

< ECF
Line 73: Line 73:
 
===Synchronous Remote Services===
 
===Synchronous Remote Services===
  
When an OSGi service is actually invoked today, this is done by the caller calling a method on the service.  For example, consider a simple 'hello' service:
+
Whether a remote or local OSGi service, a service is invoked by calling a method on the service interface.  For example, consider a simple IHello service interface:
  
 
<source lang="java">package org.eclipse.ecf.examples.remoteservices.hello;
 
<source lang="java">package org.eclipse.ecf.examples.remoteservices.hello;
Line 84: Line 84:
 
</source>
 
</source>
  
Once a consumer of this service gets a valid service reference (i.e. through ServiceTracker, injection via declarative services (DS) or other injection framework, BundleContext.getServiceReference or others), it can actually invoke/use the service:
+
Once the consumer gets a valid service instance (i.e. through ServiceTracker, injection via declarative services (DS) or other injection framework, BundleContext.getServiceReference or other methods), it can actually invoke by calling the service interface method:
  
 
<source lang="java">
 
<source lang="java">
Line 90: Line 90:
 
</source>
 
</source>
  
With OSGi remote services, this 'helloService' will typically be a proxy.  When invoked, the proxy will marshall the arguments (i.e. 'slewis' String in this case), communicate with the remote service host via some protocol.  If there is a result, then it will then be unmarshalled and returned to the caller thread on the consumerThe thread that calls 'hello' will block until this entire process is complete and the return value is available.
+
With remote services, the helloService instance will typically be a proxy.  When invoked/called, the proxy will marshall any arguments (i.e. 'slewis' String in this case), communicate with the remote service host via some protocol, the host will execute the associated code, and if there is a result, it will be unmarshalled and the result value returned to the caller thread.  As per typical java call-return semantics, the thread that calls 'hello' will block until this entire process is complete.
  
 
===Asynchronous Remote Services===
 
===Asynchronous Remote Services===
  
ECF has added the ability to declare asynchronous access to a remote method, so that the consumer can ge guaranteed that calling the service will not block.  For example, it's now possible to make a non-blocking call to the hello service like this
+
ECF has added the ability for the service host to declare asynchronous access to a remote method, so that the consumer can be guaranteed that invoking/calling the service will not block.  For example, it's possible for a consumer to make a non-blocking call to the IHello service like this
  
 
<source lang="java">
 
<source lang="java">
Line 100: Line 100:
 
</source>
 
</source>
  
With an ECF created asynchronous proxy, the consumer thread that calls helloAsyncService.helloAsync is guaranteed '''not''' to block, and the returned Future&gt;String&lt;
+
With ECF asynchronous proxies (AP), the consumer thread that calls helloAsyncService.helloAsync is guaranteed '''not''' to block, and the returned Future&gt;String&lt;
can be used to subsequently access the String result.
+
can be used to subsequently access the String result (or exception/failure).
  
With ECF's asynchronous proxy support, neither the consumer nor the host have to actually implement the asynchronous proxy.  The proxy is automatically constructed by the ECF remote services implementation.
+
With ECF's AP support, neither the consumer nor the host have to implement this asynchronous behavior.  The proxy is automatically and dynamically constructed by the ECF remote services implementation on the consumer.
  
At this point, you might ask:  How is the asynchronous proxy defined?  i.e. where does the '''helloAsyncService''' come from?
+
You might ask:  How is the asynchronous proxy defined?  i.e. where does the '''helloAsyncService''' come from?
  
The answer to this is that it is defined in a new/second service interface...called the asynchronous service interface that is related to the '''IHello''' service interface:
+
The answer is that it is defined in a new/second service interface...called the '''asynchronous service interface'''.  For example, here is how to declare an asynchronous service interface for the '''IHello''' service:
  
 
<source lang="java">
 
<source lang="java">
Line 118: Line 118:
 
</source>
 
</source>
  
Notice that this asynchronous service interface declaration resembles the IHello service interface declaration, but differs from it in several specific ways:
+
Notice that this declaration resembles the IHello service interface declaration, but differs from it in several important ways:
  
 
<ol><li>The name is <b>IHelloAsync</b> rather than <b>IHello</b>
 
<ol><li>The name is <b>IHelloAsync</b> rather than <b>IHello</b>
Line 125: Line 125:
 
</ol>
 
</ol>
  
With ECF remote services, when a IHello proxy is created, the proxy will also implement the IHelloAsync interface.  So, for example
+
With ECF remote services, on the consumer when a IHello proxy is created, the proxy will also automatically implement the IHelloAsync interface, and the consumer is able to use either the synchronous IHello service interface or the IHelloAsync asynchronous service interface.  So, for example
  
 
<source lang="java">
 
<source lang="java">
Line 132: Line 132:
 
if (helloService instanceof IHelloAsync) {
 
if (helloService instanceof IHelloAsync) {
 
   IHelloAsync helloServiceAsync = (IHelloAsync) helloService;
 
   IHelloAsync helloServiceAsync = (IHelloAsync) helloService;
   // call it asynchronously
+
   // call it asynchronously...no blocking
 
   Future<String> future = helloAsync("slewis");
 
   Future<String> future = helloAsync("slewis");
 
   // do other things
 
   // do other things
Line 154: Line 154:
 
</source>
 
</source>
  
Having both the synchronous (IHello) and asynchronous (IHelloAsync) service types gives the consumer flexibility in determining how a given remote invocation will occur (i.e. synchronously or asynchronously).  Synchronous, asynchronous, or both invocation methods may be used.
+
Having both the synchronous (IHello) and asynchronous (IHelloAsync) service types gives the consumer flexibility in determining how a given remote invocation will occur (i.e. synchronously/blocking or asynchronously/non-blocking).  Synchronous, asynchronous, or both invocation methods may be used as appropriate for the consumer's use case(s) for the service.
  
 
===Java 8 CompletableFuture===
 
===Java 8 CompletableFuture===
  
With Java8's introduction of the class '''[http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html java.util.concurrent.CompletableFuture]''' and ECF 3.8.1/Juno remote services, it's now possible to return a CompletableFuture rather than a Future in the asynchronous service interface:
+
Java 8 introduces a new type of Future called '''[http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html java.util.concurrent.CompletableFuture]'''.  With ECF 3.8.1/Juno remote services, it's now possible for asynchronous service interfaces to return a CompletableFuture:
  
 
<source lang="java">package my.package;
 
<source lang="java">package my.package;
Line 181: Line 181:
 
</source>
 
</source>
  
The use of CompletableFuture has some very nice properties for remote service design, as well as some nice guarantees for the remote service consumer.  For remote service designers, they may declare and implement normal OSGi remote services (IHello) without having to concern themselves with the consumer's invocation style (synchronous or asynchronous).
+
The use of CompletableFuture has some very nice properties for service design, as well as some nice guarantees for the service consumer.  For remote service designers, they may declare and implement normal OSGi remote services (IHello) without having to concern themselves with the consumer's invocation style (synchronous or asynchronous).
  
For remote service consumers, with CompletableFuture, remote service callers can be assured that there will be no blocking in their code (via the guarantees of CompletableFuture), use lambda to simply and succinctly express arbitrary code execution...guaranteed to be done asynchronously when the remote response becomes available.   
+
For remote service consumers remote service consumers can be assured that there will be no blocking in their code (via the guarantees of CompletableFuture).  They may also use lambda to succinctly express arbitrary eventual execution...guaranteed to be done asynchronously when the response becomes available.   
  
Further, for both the service designer and consumer, there are no extra class dependencies in the service types (IHello and IHelloAsync).  That is, no dependencies on OSGi, OSGi Remote Services, or ECF classes...meaning that if they wish to use/reuse these services in other runtime contexts (e.g. outside of OSGi, OSGi Remote Services, and/or ECF), the only dependencies necessary are to their own types and java.util.concurrent.CompletableFuture.
+
Note that with ECF remote services there is no need for either the host implementation or the consumer to implement the asynchronous service interface.  The dynamically constructed proxy will automatically have/expose the asynchronous service interface to the consumer, and this proxy will provide the underlying implementation.
 +
 
 +
Note another advantage...for both the service designer and consumer...is that there are no extra class dependencies in the synchronous or asynchronous service interfaces (e.g. IHello and IHelloAsync).  That is, there are no dependencies on OSGi, OSGi Remote Services, or ECF classes...meaning that if they wish to use/reuse these services in other runtime contexts (e.g. outside of OSGi, OSGi Remote Services, and/or ECF) in the service interfaces or in any client code.  The only dependencies are to other service types and java.util.concurrent.CompletableFuture or java.util.concurrent.Future.
  
 
<references/>
 
<references/>

Revision as of 11:39, 9 April 2014


Synchronous Remote Services

Whether a remote or local OSGi service, a service is invoked by calling a method on the service interface. For example, consider a simple IHello service interface:

package org.eclipse.ecf.examples.remoteservices.hello;
 
public interface IHello {
 
	public String hello(String from);
 
}

Once the consumer gets a valid service instance (i.e. through ServiceTracker, injection via declarative services (DS) or other injection framework, BundleContext.getServiceReference or other methods), it can actually invoke by calling the service interface method:

String response = helloService.hello("slewis");

With remote services, the helloService instance will typically be a proxy. When invoked/called, the proxy will marshall any arguments (i.e. 'slewis' String in this case), communicate with the remote service host via some protocol, the host will execute the associated code, and if there is a result, it will be unmarshalled and the result value returned to the caller thread. As per typical java call-return semantics, the thread that calls 'hello' will block until this entire process is complete.

Asynchronous Remote Services

ECF has added the ability for the service host to declare asynchronous access to a remote method, so that the consumer can be guaranteed that invoking/calling the service will not block. For example, it's possible for a consumer to make a non-blocking call to the IHello service like this

Future<String> future = helloAsyncService.helloAsync("slewis");

With ECF asynchronous proxies (AP), the consumer thread that calls helloAsyncService.helloAsync is guaranteed not to block, and the returned Future>String< can be used to subsequently access the String result (or exception/failure).

With ECF's AP support, neither the consumer nor the host have to implement this asynchronous behavior. The proxy is automatically and dynamically constructed by the ECF remote services implementation on the consumer.

You might ask: How is the asynchronous proxy defined? i.e. where does the helloAsyncService come from?

The answer is that it is defined in a new/second service interface...called the asynchronous service interface. For example, here is how to declare an asynchronous service interface for the IHello service:

public interface IHelloAsync {
 
	public Future<String> helloAsync(String from);
}

Notice that this declaration resembles the IHello service interface declaration, but differs from it in several important ways:

  1. The name is IHelloAsync rather than IHello
  2. The method name is helloAsync rather than hello
  3. The return value is Future<String> (or with Java8, CompletableFuture, see below)

With ECF remote services, on the consumer when a IHello proxy is created, the proxy will also automatically implement the IHelloAsync interface, and the consumer is able to use either the synchronous IHello service interface or the IHelloAsync asynchronous service interface. So, for example

IHello helloService = ...get service via DS/injections, ServiceTracker or otherServiceTracker, or other...
if (helloService instanceof IHelloAsync) {
   IHelloAsync helloServiceAsync = (IHelloAsync) helloService;
   // call it asynchronously...no blocking
   Future<String> future = helloAsync("slewis");
   // do other things
   String result = future.get();
   // do something with result...
}

or

IHelloAsync helloAsyncService = ...get service via DS/injections, ServiceTracker or other
// call it asynchronously...no blocking
Future<String> future = helloAsync("slewis");
// do other things
String result = future.get();
// do something with result...

Having both the synchronous (IHello) and asynchronous (IHelloAsync) service types gives the consumer flexibility in determining how a given remote invocation will occur (i.e. synchronously/blocking or asynchronously/non-blocking). Synchronous, asynchronous, or both invocation methods may be used as appropriate for the consumer's use case(s) for the service.

Java 8 CompletableFuture

Java 8 introduces a new type of Future called java.util.concurrent.CompletableFuture. With ECF 3.8.1/Juno remote services, it's now possible for asynchronous service interfaces to return a CompletableFuture:

package my.package;
import java.util.concurrent.CompletableFuture;
 
public interface IHelloAsync {
 
    public CompletableFuture<String> hello(String from);
}

with CompletableFuture it's not necessary for the consumer to call Future.get (and possibly block) directly...but rather you can write succinct, simple, and guaranteed to be non-blocking calls such as:

IHelloAsync helloAsync = ...get remote service via DS/injections, ServiceTracker or other
 
CompletableFuture<String> cf = helloAsync("slewis");
 
cf.thenAccept((response) -&gt; System.out.println("response to hello: " + response));

The use of CompletableFuture has some very nice properties for service design, as well as some nice guarantees for the service consumer. For remote service designers, they may declare and implement normal OSGi remote services (IHello) without having to concern themselves with the consumer's invocation style (synchronous or asynchronous).

For remote service consumers remote service consumers can be assured that there will be no blocking in their code (via the guarantees of CompletableFuture). They may also use lambda to succinctly express arbitrary eventual execution...guaranteed to be done asynchronously when the response becomes available.

Note that with ECF remote services there is no need for either the host implementation or the consumer to implement the asynchronous service interface. The dynamically constructed proxy will automatically have/expose the asynchronous service interface to the consumer, and this proxy will provide the underlying implementation.

Note another advantage...for both the service designer and consumer...is that there are no extra class dependencies in the synchronous or asynchronous service interfaces (e.g. IHello and IHelloAsync). That is, there are no dependencies on OSGi, OSGi Remote Services, or ECF classes...meaning that if they wish to use/reuse these services in other runtime contexts (e.g. outside of OSGi, OSGi Remote Services, and/or ECF) in the service interfaces or in any client code. The only dependencies are to other service types and java.util.concurrent.CompletableFuture or java.util.concurrent.Future.


Eclipse Communication Framework
API
API DocumentationJavadocProviders
Development
Development GuidelinesIntegrators Guide

Back to the top