@ProviderType public interface Async
Typical usage:
Async async = ctx.getService(asyncRef); ServiceReference<MyService> ref = ctx.getServiceReference(MyService.class); MyService mediator = async.mediate(ref, MyService.class); Promise<BigInteger> result = async.call(mediator.getSumOverAllValues());
The Promise
API allows callbacks to be made when asynchronous tasks
complete, and can be used to chain Promises.
Multiple asynchronous tasks can be started concurrently, and will run in parallel if the Async Service has threads available.
Modifier and Type | Method and Description |
---|---|
Promise<?> |
call()
Invoke the last method call registered by a mediated object as an
asynchronous task.
|
<R> Promise<R> |
call(R r)
Invoke the last method call registered by a mediated object as an
asynchronous task.
|
Promise<Void> |
execute()
Invoke the last method call registered by a mediated object as a
"fire-and-forget" asynchronous task.
|
<T> T |
mediate(ServiceReference<? extends T> target,
Class<T> iface)
Create a mediator for the specified service.
|
<T> T |
mediate(T target,
Class<T> iface)
Create a mediator for the specified object.
|
<T> T mediate(T target, Class<T> iface)
call(Object)
, call()
, or execute()
method.
The values returned by method calls made on a mediated object are opaque and should not be interpreted.
Normal usage:
I s = ...; I i = async.mediate(s, I.class); Promise<String> p = async.call(i.foo());
target
- The service object to mediate.iface
- The type that the mediated object should provide.IllegalArgumentException
- If the type represented by iface cannot
be mediated.<T> T mediate(ServiceReference<? extends T> target, Class<T> iface)
call(Object)
, call()
, or execute()
method.
The values returned by method calls made on a mediated object are opaque and should not be interpreted.
This method differs from mediate(Object, Class)
in that it can
track the availability of the specified service. This is recommended as
the preferred option for mediating OSGi services as asynchronous tasks
may not start executing until some time after they are requested.
Tracking the validity of the ServiceReference
for the service
ensures that these tasks do not proceed with an invalid object.
Normal usage:
ServiceReference<I> s = ...; I i = async.mediate(s, I.class); Promise<String> p = async.call(i.foo());
target
- The service reference to mediate.iface
- The type that the mediated object should provide.IllegalArgumentException
- If the type represented by iface cannot
be mediated.<R> Promise<R> call(R r)
Typically the parameter for this method will be supplied inline like this:
ServiceReference<I> s = ...; I i = async.mediate(s, I.class); Promise<String> p = async.call(i.foo());
r
- The return value of the mediated call, used for type
information.Promise<?> call()
Generally it is preferable to use call(Object)
like this:
ServiceReference<I> s = ...; I i = async.mediate(s, I.class); Promise<String> p = async.call(i.foo());
However this pattern does not work for void methods. Void methods can therefore be handled like this:
ServiceReference<I> s = ...; I i = async.mediate(s, I.class); i.voidMethod() Promise<?> p = async.call();
Promise<Void> execute()
call()
and call(Object)
when
no callbacks, or other features of Promise
, are needed.
The advantage of this method is that it allows for greater optimization
of the underlying asynchronous task. Clients are therefore likely to see
better performance when using this method compared to using
call(Object)
or call()
and ignoring the returned
Promise. The Promise
returned by this method is different from
the Promise returned by call(Object)
or call()
, in that
the returned Promise will resolve when the fire-and-forget task is
successfully started, or fail if the task cannot be started. Note that
there is no happens-before relationship and the returned Promise
may resolve before or after the fire-and-forget task starts, or
completes.
Typically this method is used like call()
:
ServiceReference<I> s = ...; I i = async.mediate(s, I.class); i.someMethod() Promise<Void> p = async.execute();
Copyright © OSGi Alliance (2000, 2015). All Rights Reserved. Licensed under the OSGi Specification License, Version 2.0