Giter VIP home page Giter VIP logo

asyncremoteserviceexamples's Introduction

Async Remote Service Examples

Example OSGi services that use OSGi R7 Async Remote Services. When used with ECF's Remote Services Implementation and a supporting distribution provider, the method call will be invoked asynchronously by the service consumer. As described here, return value given in the service interface has one of the Java types: CompletableFuture, Future, CompletionStage, or Promise, then the service method will be invoked without blocking and with an (optional) timeout.

For example, here is a 'hello' service interface:

public interface IHello {

    String sayHello(String from, String message);
	
    CompletableFuture sayHelloAsync(String from, String message);
	
    Promise sayHelloPromise(String from, String message);
	
}

Since the sayHelloAsync and sayHelloPromise methods can be used over a network by remote service consumers with a guarantee of asynchronous invocation and optionally with a given timeout. Here is an implementation that uses the OSGi R7 specified intents to do so:

@Component(immediate=true,property = { "service.intents=osgi.async",  // R7 osgi.async remote service property
                                       "service.exported.interfaces=*",  // RSA-required remote service property
				       "osgi.basic.timeout:Long=50000"  // timeout of 50000ms=50 seconds
									                   }
public class HelloImpl implements IHello {

...implementation of IHello...

When the HelloImpl service instance is exported, the osgi.async property will require the distribution provider to create a proxy that returns a CompletableFuture instance immediately, and require the remote service invocation timeout after 50 seconds. Here is an example consumer for this service:

void bindHello(IHello hello) {
	
    hello.sayHelloAsync("JavaAsync","Howdy Python").whenComplete((resp,except) -> {
                                                        if (except != null)
                                                            except.printStackTrace();
                                                        else
                                                            System.out.println("sayHelloAsync received result="+resp);
                                                    });
}

When invoked, the sayHelloAsync call will immediately return a CompletableFuture instance that will timeout after 50s, or call whenComplete before that timeout with either a valid resp instance or an exception. This runtime behavior is provided via a R7-compliant distribution provider.

This repostiory contains this example, as well as other examples of both synchronous and asynchronous remote services.

asyncremoteserviceexamples's People

Contributors

scottslewis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.