Package com.rabbitmq.tools.jsonrpc
Class JsonRpcServer
- java.lang.Object
-
- com.rabbitmq.client.RpcServer
-
- com.rabbitmq.client.StringRpcServer
-
- com.rabbitmq.tools.jsonrpc.JsonRpcServer
-
public class JsonRpcServer extends StringRpcServer
JSON-RPC Server class.Given a Java
Class
, representing an interface, and an implementation of that interface, JsonRpcServer will reflect on the class to construct theServiceDescription
, and will route incoming requests for methods on the interface to the implementation object while the mainloop() is running.JsonRpcServer
delegates JSON parsing and generating to aJsonRpcMapper
.- See Also:
RpcServer
,JsonRpcClient
,JsonRpcMapper
,JacksonJsonRpcMapper
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.rabbitmq.client.RpcServer
RpcServer.RpcConsumer
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Object
interfaceInstance
The instance backing this server.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.StringRpcServer
STRING_ENCODING
-
-
Constructor Summary
Constructors Constructor Description JsonRpcServer(Channel channel, java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance)
Construct a server that talks to the outside world using the given channel, and constructs a fresh temporary queue.JsonRpcServer(Channel channel, java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance, JsonRpcMapper mapper)
JsonRpcServer(Channel channel, java.lang.String queueName, java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance)
Construct a server that talks to the outside world using the given channel and queue name.JsonRpcServer(Channel channel, java.lang.String queueName, java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance, JsonRpcMapper mapper)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
doCall(java.lang.String requestBody)
Runs a single JSON-RPC request.private java.lang.String
errorResponse(java.lang.Object id, int code, java.lang.String message, java.lang.Object errorArg)
Construct and encode a JSON-RPC error response for the request ID given, using the code, message, and possible (JSON-encodable) argument passed in.ServiceDescription
getServiceDescription()
Public API - gets the service description record that this service built from interfaceClass at construction time.java.lang.String
handleStringCall(java.lang.String requestBody, AMQP.BasicProperties replyProperties)
Override our superclass' method, dispatching to doCall.private void
init(java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance)
java.lang.reflect.Method
matchingMethod(java.lang.String methodName, java.lang.Object[] params)
Retrieves the best matching method for the given method name and parameters.private java.lang.String
response(java.lang.Object id, java.lang.String label, java.lang.Object value)
Private API - used by errorResponse and resultResponse.private java.lang.String
resultResponse(java.lang.Object id, java.lang.Object result)
Construct and encode a JSON-RPC success response for the request ID given, using the result value passed in.-
Methods inherited from class com.rabbitmq.client.StringRpcServer
handleCall, handleCast, handleStringCall, handleStringCast
-
Methods inherited from class com.rabbitmq.client.RpcServer
close, getChannel, getQueueName, handleCall, handleCall, handleCast, handleCast, mainloop, postprocessReplyProperties, preprocessReplyProperties, processRequest, setupConsumer, terminateMainloop
-
-
-
-
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.
-
interfaceInstance
private java.lang.Object interfaceInstance
The instance backing this server.
-
-
Constructor Detail
-
JsonRpcServer
public JsonRpcServer(Channel channel, java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance, JsonRpcMapper mapper) throws java.io.IOException
- Throws:
java.io.IOException
-
JsonRpcServer
public JsonRpcServer(Channel channel, java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance) throws java.io.IOException
Construct a server that talks to the outside world using the given channel, and constructs a fresh temporary queue. Use getQueueName() to discover the created queue name.- Parameters:
channel
- AMQP channel to useinterfaceClass
- Java interface that this server is exposing to the worldinterfaceInstance
- Java instance (of interfaceClass) that is being exposed- Throws:
java.io.IOException
- if something goes wrong during an AMQP operation
-
JsonRpcServer
public JsonRpcServer(Channel channel, java.lang.String queueName, java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance, JsonRpcMapper mapper) throws java.io.IOException
- Throws:
java.io.IOException
-
JsonRpcServer
public JsonRpcServer(Channel channel, java.lang.String queueName, java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance) throws java.io.IOException
Construct a server that talks to the outside world using the given channel and queue name. Our superclass, RpcServer, expects the queue to exist at the time of construction.- Parameters:
channel
- AMQP channel to usequeueName
- AMQP queue name to listen for requests oninterfaceClass
- Java interface that this server is exposing to the worldinterfaceInstance
- Java instance (of interfaceClass) that is being exposed- Throws:
java.io.IOException
- if something goes wrong during an AMQP operation
-
-
Method Detail
-
init
private void init(java.lang.Class<?> interfaceClass, java.lang.Object interfaceInstance)
-
handleStringCall
public java.lang.String handleStringCall(java.lang.String requestBody, AMQP.BasicProperties replyProperties)
Override our superclass' method, dispatching to doCall.- Overrides:
handleStringCall
in classStringRpcServer
-
doCall
public java.lang.String doCall(java.lang.String requestBody)
Runs a single JSON-RPC request.- Parameters:
requestBody
- the JSON-RPC request string (a JSON encoded value)- Returns:
- a JSON-RPC response string (a JSON encoded value)
-
matchingMethod
public java.lang.reflect.Method matchingMethod(java.lang.String methodName, java.lang.Object[] params)
Retrieves the best matching method for the given method name and parameters.Subclasses may override this if they have specialised dispatching requirements, so long as they continue to honour their ServiceDescription.
-
errorResponse
private java.lang.String errorResponse(java.lang.Object id, int code, java.lang.String message, java.lang.Object errorArg)
Construct and encode a JSON-RPC error response for the request ID given, using the code, message, and possible (JSON-encodable) argument passed in.
-
resultResponse
private java.lang.String resultResponse(java.lang.Object id, java.lang.Object result)
Construct and encode a JSON-RPC success response for the request ID given, using the result value passed in.
-
response
private java.lang.String response(java.lang.Object id, java.lang.String label, java.lang.Object value)
Private API - used by errorResponse and resultResponse.
-
getServiceDescription
public ServiceDescription getServiceDescription()
Public API - gets the service description record that this service built from interfaceClass at construction time.
-
-