Package com.rabbitmq.tools.jsonrpc
Class JsonRpcClient
java.lang.Object
com.rabbitmq.client.RpcClient
com.rabbitmq.tools.jsonrpc.JsonRpcClient
- All Implemented Interfaces:
InvocationHandler
JSON-RPC is a lightweight
RPC mechanism using JSON
as a data language for request and reply messages. It is
rapidly becoming a standard in web development, where it is
used to make RPC requests over HTTP. RabbitMQ provides an
AMQP transport binding for JSON-RPC in the form of the
JsonRpcClient class.
JSON-RPC services are self-describing - each service is able
to list its supported procedures, and each procedure
describes its parameters and types. An instance of
JsonRpcClient retrieves its service description using the
standard system.describe procedure when it is
constructed, and uses the information to coerce parameter
types appropriately. A JSON service description is parsed
into instances of ServiceDescription. Client
code can access the service description by reading the
serviceDescription field of
JsonRpcClient instances.- See Also:
-
Field Summary
Fields inherited from class com.rabbitmq.client.RpcClient
NO_TIMEOUT -
Constructor Summary
ConstructorsConstructorDescriptionJsonRpcClient(Channel channel, String exchange, String routingKey) JsonRpcClient(Channel channel, String exchange, String routingKey, int timeout) Construct a new JsonRpcClient, passing the parameters through to RpcClient's constructor. -
Method Summary
Modifier and TypeMethodDescriptionPublic API - ascall(String,Object[]), but takes the method name from the first entry inargs, and the parameters from subsequent entries.Public API - builds, encodes and sends a JSON-RPC request, and waits for the response.static ObjectcheckReply(Map<String, Object> reply) Private API - parses a JSON-RPC reply object, checking it for exceptions.static ObjectPrivate API - used bycall(String[])to ad-hoc convert strings into the required data types for a call.createProxy(Class<?> klass) Public API - gets a dynamic proxy for a particular interface class.Public API - gets the service description record that this service loaded from the server itself at construction time.Public API - implements InvocationHandler.invoke.Methods inherited from class com.rabbitmq.client.RpcClient
checkConsumer, close, getChannel, getConsumer, getContinuationMap, getCorrelationId, getExchange, getRoutingKey, mapCall, mapCall, primitiveCall, primitiveCall, publish, setupConsumer, stringCall
-
Constructor Details
-
JsonRpcClient
public JsonRpcClient(Channel channel, String exchange, String routingKey, int timeout) throws IOException, JsonRpcException, TimeoutException Construct a new JsonRpcClient, passing the parameters through to RpcClient's constructor. The service description record is retrieved from the server during construction.- Throws:
TimeoutException- if a response is not received within the timeout specified, if anyIOExceptionJsonRpcException
-
JsonRpcClient
public JsonRpcClient(Channel channel, String exchange, String routingKey) throws IOException, JsonRpcException, TimeoutException
-
-
Method Details
-
checkReply
Private API - parses a JSON-RPC reply object, checking it for exceptions.- Returns:
- the result contained within the reply, if no exception is found Throws JsonRpcException if the reply object contained an exception
- Throws:
JsonRpcException
-
call
public Object call(String method, Object[] params) throws IOException, JsonRpcException, TimeoutException Public API - builds, encodes and sends a JSON-RPC request, and waits for the response.- Returns:
- the result contained within the reply, if no exception is found
- Throws:
JsonRpcException- if the reply object contained an exceptionTimeoutException- if a response is not received within the timeout specified, if anyIOException
-
invoke
Public API - implements InvocationHandler.invoke. This is useful for constructing dynamic proxies for JSON-RPC interfaces.- Specified by:
invokein interfaceInvocationHandler- Throws:
Throwable
-
createProxy
Public API - gets a dynamic proxy for a particular interface class.- Throws:
IllegalArgumentException
-
coerce
Private API - used bycall(String[])to ad-hoc convert strings into the required data types for a call.- Throws:
NumberFormatException
-
call
public Object call(String[] args) throws NumberFormatException, IOException, JsonRpcException, TimeoutException Public API - ascall(String,Object[]), but takes the method name from the first entry inargs, and the parameters from subsequent entries. All parameter values are passed through coerce() to attempt to make them the types the server is expecting.- Returns:
- the result contained within the reply, if no exception is found
- Throws:
JsonRpcException- if the reply object contained an exceptionNumberFormatException- if a coercion failedTimeoutException- if a response is not received within the timeout specified, if anyIOException- See Also:
-
getServiceDescription
Public API - gets the service description record that this service loaded from the server itself at construction time.
-