com.rabbitmq.tools.jsonrpc
Class JsonRpcClient

java.lang.Object
  extended by com.rabbitmq.client.RpcClient
      extended by com.rabbitmq.tools.jsonrpc.JsonRpcClient
All Implemented Interfaces:
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 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:
call(String, Object[]), call(String[])

Field Summary
 ServiceDescription serviceDescription
          Holds the JSON-RPC service description for this client.
 
Constructor Summary
JsonRpcClient(Channel channel, java.lang.String exchange, java.lang.String routingKey)
          Construct a new JsonRpcClient, passing the parameters through to RpcClient's constructor.
 
Method Summary
 java.lang.Object call(java.lang.String[] args)
          Public API - as call(String,Object[]), but takes the method name from the first entry in args, and the parameters from subsequent entries.
 java.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.
static java.lang.Object checkReply(java.util.Map<java.lang.String,java.lang.Object> 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)
          Private API - used by call(String[]) to ad-hoc convert strings into the required data types for a call.
 java.lang.Object createProxy(java.lang.Class 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.
 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
checkConsumer, close, getChannel, getConsumer, getContinuationMap, getCorrelationId, getExchange, getReplyQueue, getRoutingKey, mapCall, mapCall, primitiveCall, primitiveCall, publish, setupConsumer, setupReplyQueue, stringCall
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serviceDescription

public ServiceDescription serviceDescription
Holds the JSON-RPC service description for this client.

Constructor Detail

JsonRpcClient

public JsonRpcClient(Channel channel,
                     java.lang.String exchange,
                     java.lang.String routingKey)
              throws java.io.IOException,
                     JsonRpcException
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.io.IOException
JsonRpcException
Method Detail

checkReply

public static java.lang.Object checkReply(java.util.Map<java.lang.String,java.lang.Object> 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
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 exception
java.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 interface java.lang.reflect.InvocationHandler
Throws:
java.lang.Throwable

createProxy

public java.lang.Object createProxy(java.lang.Class klass)
                             throws java.lang.IllegalArgumentException
Public API - gets a dynamic proxy for a particular interface class.

Throws:
java.lang.IllegalArgumentException

coerce

public static java.lang.Object coerce(java.lang.String val,
                                      java.lang.String type)
                               throws java.lang.NumberFormatException
Private API - used by call(String[]) to ad-hoc convert strings into the required data types for a call.

Throws:
java.lang.NumberFormatException

call

public java.lang.Object call(java.lang.String[] args)
                      throws java.lang.NumberFormatException,
                             java.io.IOException,
                             JsonRpcException
Public API - as call(String,Object[]), but takes the method name from the first entry in args, 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 exception
java.lang.NumberFormatException - if a coercion failed
java.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

public void retrieveServiceDescription()
                                throws java.io.IOException,
                                       JsonRpcException
Private API - invokes the "system.describe" method on the server, and parses and stores the resulting service description in this object.

Throws:
java.io.IOException
JsonRpcException