Package com.rabbitmq.tools.jsonrpc
Class JsonRpcClient
- java.lang.Object
-
- com.rabbitmq.client.RpcClient
-
- com.rabbitmq.tools.jsonrpc.JsonRpcClient
-
- All Implemented Interfaces:
InvocationHandler
public class JsonRpcClient extends RpcClient implements 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.CorrelationIdSupplier, RpcClient.Response, RpcClient.RpcClientReplyHandler
-
-
Field Summary
-
Fields inherited from class com.rabbitmq.client.RpcClient
DEFAULT_REPLY_HANDLER, NO_TIMEOUT
-
-
Constructor Summary
Constructors Constructor Description JsonRpcClient(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.JsonRpcClient(Channel channel, String exchange, String routingKey, int timeout, JsonRpcMapper mapper)
Construct a new JsonRpcClient, passing the parameters through to RpcClient's constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Object
call(String[] args)
Deprecated.This method will be removed in the next major versionObject
call(String method, Object[] params)
Public API - builds, encodes and sends a JSON-RPC request, and waits for the response.static Object
coerce(String val, String type)
Deprecated.This method will be removed in the next major version<T> T
createProxy(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.Object
invoke(Object proxy, Method method, Object[] args)
Public API - implements InvocationHandler.invoke.-
Methods inherited from class com.rabbitmq.client.RpcClient
checkConsumer, close, doCall, doCall, getChannel, getConsumer, getContinuationMap, getCorrelationId, getExchange, getRoutingKey, incrementingCorrelationIdSupplier, incrementingCorrelationIdSupplier, mapCall, mapCall, primitiveCall, primitiveCall, primitiveCall, publish, responseCall, responseCall, setupConsumer, stringCall
-
-
-
-
Constructor Detail
-
JsonRpcClient
public JsonRpcClient(Channel channel, String exchange, String routingKey, int timeout, JsonRpcMapper mapper) 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 anyIOException
JsonRpcException
-
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 anyIOException
JsonRpcException
-
JsonRpcClient
public JsonRpcClient(Channel channel, String exchange, String routingKey) throws IOException, JsonRpcException, TimeoutException
-
-
Method Detail
-
coerce
@Deprecated public static Object coerce(String val, String type) throws 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:
NumberFormatException
-
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 Object invoke(Object proxy, Method method, Object[] args) throws Throwable
Public API - implements InvocationHandler.invoke. This is useful for constructing dynamic proxies for JSON-RPC interfaces.- Specified by:
invoke
in interfaceInvocationHandler
- Throws:
Throwable
-
createProxy
public <T> T createProxy(Class<T> klass) throws IllegalArgumentException
Public API - gets a dynamic proxy for a particular interface class.- Throws:
IllegalArgumentException
-
call
@Deprecated public Object call(String[] args) throws NumberFormatException, IOException, JsonRpcException, 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 exceptionNumberFormatException
- if a coercion failedTimeoutException
- if a response is not received within the timeout specified, if anyIOException
- 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.
-
-