Package com.rabbitmq.tools.jsonrpc
Class JsonRpcClient
- java.lang.Object
-
- com.rabbitmq.client.RpcClient
-
- com.rabbitmq.tools.jsonrpc.JsonRpcClient
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.lang.reflect.InvocationHandler
public class JsonRpcClient extends RpcClient implements java.lang.reflect.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 theJsonRpcClient
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 ofServiceDescription
. Client code can access the service description by reading theserviceDescription
field ofJsonRpcClient
instances.JsonRpcClient
delegates JSON parsing and generating to aJsonRpcMapper
.- See Also:
call(String, Object[])
,JsonRpcMapper
,JacksonJsonRpcMapper
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.rabbitmq.client.RpcClient
RpcClient.Response
-
-
Field Summary
Fields Modifier and Type Field Description private static org.slf4j.Logger
LOGGER
private JsonRpcMapper
mapper
private ServiceDescription
serviceDescription
Holds the JSON-RPC service description for this client.-
Fields inherited from class com.rabbitmq.client.RpcClient
DEFAULT_REPLY_HANDLER, NO_TIMEOUT
-
-
Constructor Summary
Constructors Constructor Description JsonRpcClient(Channel channel, java.lang.String exchange, java.lang.String routingKey)
JsonRpcClient(Channel channel, java.lang.String exchange, java.lang.String routingKey, int timeout)
Construct a new JsonRpcClient, passing the parameters through to RpcClient's constructor.JsonRpcClient(Channel channel, java.lang.String exchange, java.lang.String routingKey, int timeout, JsonRpcMapper mapper)
Construct a new JsonRpcClient, passing the parameters through to RpcClient's constructor.JsonRpcClient(RpcClientParams rpcClientParams, JsonRpcMapper mapper)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.Object
call(java.lang.String[] args)
Deprecated.This method will be removed in the next major versionjava.lang.Object
call(java.lang.String method, java.lang.Object[] params)
Public API - builds, encodes and sends a JSON-RPC request, and waits for the response.private java.lang.Object
checkReply(JsonRpcMapper.JsonRpcResponse reply)
Private API - parses a JSON-RPC reply object, checking it for exceptions.static java.lang.Object
coerce(java.lang.String val, java.lang.String type)
Deprecated.This method will be removed in the next major version<T> T
createProxy(java.lang.Class<T> klass)
Public API - gets a dynamic proxy for a particular interface class.ServiceDescription
getServiceDescription()
Public API - gets the service description record that this service loaded from the server itself at construction time.java.lang.Object
invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
Public API - implements InvocationHandler.invoke.private void
retrieveServiceDescription()
Private API - invokes the "system.describe" method on the server, and parses and stores the resulting service description in this object.-
Methods inherited from class com.rabbitmq.client.RpcClient
close, doCall, doCall, getChannel, getConsumer, getContinuationMap, getCorrelationId, getExchange, getRoutingKey, incrementingCorrelationIdSupplier, incrementingCorrelationIdSupplier, mapCall, mapCall, primitiveCall, primitiveCall, primitiveCall, publish, responseCall, responseCall, setupConsumer, stringCall
-
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
mapper
private final JsonRpcMapper mapper
-
serviceDescription
private ServiceDescription serviceDescription
Holds the JSON-RPC service description for this client.
-
-
Constructor Detail
-
JsonRpcClient
public JsonRpcClient(RpcClientParams rpcClientParams, JsonRpcMapper mapper) throws java.io.IOException, JsonRpcException, java.util.concurrent.TimeoutException
Construct a newJsonRpcClient
, passing theRpcClientParams
throughRpcClient
's constructor.The service description record is retrieved from the server during construction.
- Parameters:
rpcClientParams
-mapper
-- Throws:
java.io.IOException
JsonRpcException
java.util.concurrent.TimeoutException
-
JsonRpcClient
public JsonRpcClient(Channel channel, java.lang.String exchange, java.lang.String routingKey, int timeout, JsonRpcMapper mapper) throws java.io.IOException, JsonRpcException, java.util.concurrent.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:
java.util.concurrent.TimeoutException
- if a response is not received within the timeout specified, if anyjava.io.IOException
JsonRpcException
-
JsonRpcClient
public JsonRpcClient(Channel channel, java.lang.String exchange, java.lang.String routingKey, int timeout) throws java.io.IOException, JsonRpcException, java.util.concurrent.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:
java.util.concurrent.TimeoutException
- if a response is not received within the timeout specified, if anyjava.io.IOException
JsonRpcException
-
JsonRpcClient
public JsonRpcClient(Channel channel, java.lang.String exchange, java.lang.String routingKey) throws java.io.IOException, JsonRpcException, java.util.concurrent.TimeoutException
- Throws:
java.io.IOException
JsonRpcException
java.util.concurrent.TimeoutException
-
-
Method Detail
-
coerce
@Deprecated public static java.lang.Object coerce(java.lang.String val, java.lang.String type) throws java.lang.NumberFormatException
Deprecated.This method will be removed in the next major versionPrivate API - used bycall(String[])
to ad-hoc convert strings into the required data types for a call. This method is deprecated because it uses homegrown JSON utilities that don't deal correctly with complex types. TheJacksonJsonRpcMapper
has been introduced to handle primitive and complex types, as well as primitive wrappers correctly.- Throws:
java.lang.NumberFormatException
-
checkReply
private java.lang.Object checkReply(JsonRpcMapper.JsonRpcResponse reply) throws JsonRpcException
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 java.lang.Object call(java.lang.String method, java.lang.Object[] params) throws java.io.IOException, JsonRpcException, java.util.concurrent.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 exceptionjava.util.concurrent.TimeoutException
- if a response is not received within the timeout specified, if anyjava.io.IOException
-
invoke
public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.Throwable
Public API - implements InvocationHandler.invoke. This is useful for constructing dynamic proxies for JSON-RPC interfaces.- Specified by:
invoke
in interfacejava.lang.reflect.InvocationHandler
- Throws:
java.lang.Throwable
-
createProxy
public <T> T createProxy(java.lang.Class<T> klass) throws java.lang.IllegalArgumentException
Public API - gets a dynamic proxy for a particular interface class.- Throws:
java.lang.IllegalArgumentException
-
call
@Deprecated public java.lang.Object call(java.lang.String[] args) throws java.lang.NumberFormatException, java.io.IOException, JsonRpcException, java.util.concurrent.TimeoutException
Deprecated.This method will be removed in the next major versionPublic 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. This method is deprecated because it uses homegrown JSON utilities that don't deal correctly with complex types. TheJacksonJsonRpcMapper
has been introduced to handle primitive and complex types, as well as primitive wrappers correctly.- Returns:
- the result contained within the reply, if no exception is found
- Throws:
JsonRpcException
- if the reply object contained an exceptionjava.lang.NumberFormatException
- if a coercion failedjava.util.concurrent.TimeoutException
- if a response is not received within the timeout specified, if anyjava.io.IOException
- See Also:
coerce(java.lang.String, java.lang.String)
-
getServiceDescription
public ServiceDescription getServiceDescription()
Public API - gets the service description record that this service loaded from the server itself at construction time.
-
retrieveServiceDescription
private void retrieveServiceDescription() throws java.io.IOException, JsonRpcException, java.util.concurrent.TimeoutException
Private API - invokes the "system.describe" method on the server, and parses and stores the resulting service description in this object. TODO: Avoid calling this from the constructor.- Throws:
java.util.concurrent.TimeoutException
- if a response is not received within the timeout specified, if anyjava.io.IOException
JsonRpcException
-
-