public abstract class ServiceMethod extends Object
This class is immutable and the method execution is thread-safe. Each method can either be executed synchronously or asynchronously. The method itself, its arguments and its results are self-describing: they provide a name, description and a type.
Implementations of this class must guarantee immutability and thread-safety.
They can choose whether to provide a synchronous implementation, an asynchronous implementation
or both. The framework will route or wrap the call appropriately (i.e if a
synchronous call is requested but only an asynchronous implementation is provided,
the call will wait until the asynchronous implementation returns).
Implementations are provided by overriding
asyncExecImpl(java.util.Map, java.util.function.Consumer, java.util.function.Consumer)
and/or syncExecImpl(java.util.Map)
.
Service methods can have parameters that are set by the corresponding service method description. Refer to the examples and wiki documentation for how to do it.
Modifier and Type | Class and Description |
---|---|
static class |
ServiceMethod.DataDescription
The full description of an argument of a result of the service.
|
Constructor and Description |
---|
ServiceMethod(ServiceMethodDescription serviceMethodDescription,
ServiceDescription serviceDescription)
Creates a new service method with the given description.
|
Modifier and Type | Method and Description |
---|---|
protected void |
asyncExecImpl(Map<String,Object> parameters,
java.util.function.Consumer<Map<String,Object>> callback,
java.util.function.Consumer<Exception> errorCallback)
Asynchronous implementation of the service method.
|
void |
executeAsync(Map<String,Object> arguments,
java.util.function.Consumer<Map<String,Object>> callback,
java.util.function.Consumer<Exception> errorCallback)
Executes the service method with the given parameters, the result of
which will be communicated through the callbacks (asynchronous execution
of this service method).
|
Map<String,Object> |
executeSync(Map<String,Object> arguments)
Executes the service method with the given parameters, and waits for the
response (synchronous execution of this service method).
|
Map<String,ServiceMethod.DataDescription> |
getArgumentMap()
The arguments, indexed by name.
|
List<ServiceMethod.DataDescription> |
getArguments()
The list of arguments, with their name, description and type.
|
String |
getDescription()
A description for the service method.
|
String |
getName()
A brief name for the service method.
|
Map<String,ServiceMethod.DataDescription> |
getResultMap()
The results, indexed by name.
|
List<ServiceMethod.DataDescription> |
getResults()
The list of results, with their name, description and type.
|
protected Map<String,Object> |
syncExecImpl(Map<String,Object> parameters)
Synchronous implementation of the service method.
|
String |
toString()
Provides a text representation for the method (i.e.
|
public ServiceMethod(ServiceMethodDescription serviceMethodDescription, ServiceDescription serviceDescription)
serviceMethodDescription
- the description of the service method; can't be nullserviceDescription
- the description of the service; can't be nullpublic final String getName()
public final String getDescription()
public List<ServiceMethod.DataDescription> getArguments()
public Map<String,ServiceMethod.DataDescription> getArgumentMap()
public List<ServiceMethod.DataDescription> getResults()
public Map<String,ServiceMethod.DataDescription> getResultMap()
public String toString()
name(Type arg1, Type arg2): Type res1
).protected Map<String,Object> syncExecImpl(Map<String,Object> parameters) throws Exception
The implementation should return the value in case of success, or throw an exception in case of failure.
parameters
- the parameters for the method; not nullException
- the result callback, for failureprotected void asyncExecImpl(Map<String,Object> parameters, java.util.function.Consumer<Map<String,Object>> callback, java.util.function.Consumer<Exception> errorCallback)
The implementation should call only one callback, the callback or the errorCallback. If the method throws an exception it will be sent to the errorCallbak (i.e. you can let exceptions go through) in which case no callback must be used.
parameters
- the parameters for the method, already type checkedcallback
- the result callback, for success; not nullerrorCallback
- the error callback, for failures; can't be nullpublic Map<String,Object> executeSync(Map<String,Object> arguments)
arguments
- the parameters for the service; can't be nullRuntimeException
- if the method execution is unsuccessfulpublic void executeAsync(Map<String,Object> arguments, java.util.function.Consumer<Map<String,Object>> callback, java.util.function.Consumer<Exception> errorCallback)
arguments
- the parameters for the service; can't be nullcallback
- the result callback, for success; can't be nullerrorCallback
- the error callback, for failures; can't be nullCopyright © 2015. All rights reserved.