Package com.rabbitmq.client
Class RpcServer
java.lang.Object
com.rabbitmq.client.RpcServer
- Direct Known Subclasses:
MapRpcServer
,StringRpcServer
Class which manages a request queue for a simple RPC-style service.
The class is agnostic about the format of RPC arguments / return values.
-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionCreates an RpcServer listening on a temporary exclusive autodelete queue.If the passed-in queue name is null, creates a server-named temporary exclusive autodelete queue to use; otherwise expects the queue to have already been declared. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Public API - cancels the consumer, thus deleting the queue, if it was a temporary queue, and marks the RpcServer as closed.Retrieve the channel.Retrieve the queue name.byte[]
handleCall
(byte[] requestBody, AMQP.BasicProperties replyProperties) High-level response method.byte[]
handleCall
(AMQP.BasicProperties requestProperties, byte[] requestBody, AMQP.BasicProperties replyProperties) Mid-level response method.byte[]
handleCall
(Delivery request, AMQP.BasicProperties replyProperties) Lowest-level response method.void
handleCast
(byte[] requestBody) High-level handler method.void
handleCast
(AMQP.BasicProperties requestProperties, byte[] requestBody) Mid-level handler method.void
handleCast
(Delivery request) Lowest-level handler method.mainloop()
Public API - main server loop.protected AMQP.BasicProperties
postprocessReplyProperties
(Delivery request, AMQP.BasicProperties.Builder builder) Gives a chance to set/modify reply properties after the handling callprotected AMQP.BasicProperties
preprocessReplyProperties
(Delivery request, AMQP.BasicProperties.Builder builder) Gives a chance to set/modify reply properties before handling call.void
processRequest
(Delivery request) Private API - Process a single request.protected RpcServer.RpcConsumer
Registers a consumer on the reply queue.void
Call this method to terminate the mainloop.
-
Constructor Details
-
RpcServer
Creates an RpcServer listening on a temporary exclusive autodelete queue.- Throws:
IOException
-
RpcServer
If the passed-in queue name is null, creates a server-named temporary exclusive autodelete queue to use; otherwise expects the queue to have already been declared.- Throws:
IOException
-
-
Method Details
-
close
Public API - cancels the consumer, thus deleting the queue, if it was a temporary queue, and marks the RpcServer as closed.- Throws:
IOException
- if an error is encountered
-
setupConsumer
Registers a consumer on the reply queue.- Returns:
- the newly created and registered consumer
- Throws:
IOException
- if an error is encountered
-
mainloop
Public API - main server loop. Call this to begin processing requests. Request processing will continue until the Channel (or its underlying Connection) is shut down, or until terminateMainloop() is called, or until the thread running the loop is interrupted. Note that if the mainloop is blocked waiting for a request, the termination flag is not checked until a request is received, so a good time to call terminateMainloop() is during a request handler.- Returns:
- the exception that signalled the Channel shutdown, or null for orderly shutdown
- Throws:
IOException
-
terminateMainloop
public void terminateMainloop()Call this method to terminate the mainloop. Note that if the mainloop is blocked waiting for a request, the termination flag is not checked until a request is received, so a good time to call terminateMainloop() is during a request handler. -
processRequest
Private API - Process a single request. Called from mainloop().- Throws:
IOException
-
handleCall
Lowest-level response method. Calls handleCall(AMQP.BasicProperties,byte[],AMQP.BasicProperties). -
handleCall
public byte[] handleCall(AMQP.BasicProperties requestProperties, byte[] requestBody, AMQP.BasicProperties replyProperties) Mid-level response method. Calls handleCall(byte[],AMQP.BasicProperties). -
handleCall
High-level response method. Returns an empty response by default - override this (or other handleCall and handleCast methods) in subclasses. -
preprocessReplyProperties
protected AMQP.BasicProperties preprocessReplyProperties(Delivery request, AMQP.BasicProperties.Builder builder) Gives a chance to set/modify reply properties before handling call. Note the correlationId property is already set.- Parameters:
request
- the inbound messagebuilder
- the reply properties builder- Returns:
- the properties to pass in to the handling call
-
postprocessReplyProperties
protected AMQP.BasicProperties postprocessReplyProperties(Delivery request, AMQP.BasicProperties.Builder builder) Gives a chance to set/modify reply properties after the handling call- Parameters:
request
- the inbound messagebuilder
- the reply properties builder- Returns:
- the properties to pass in to the response message
-
handleCast
Lowest-level handler method. Calls handleCast(AMQP.BasicProperties,byte[]). -
handleCast
Mid-level handler method. Calls handleCast(byte[]). -
handleCast
public void handleCast(byte[] requestBody) High-level handler method. Does nothing by default - override this (or other handleCast and handleCast methods) in subclasses. -
getChannel
Retrieve the channel.- Returns:
- the channel to which this server is connected
-
getQueueName
Retrieve the queue name.- Returns:
- the queue which this server is consuming from
-