Package com.rabbitmq.client
Class RpcClient
- java.lang.Object
-
- com.rabbitmq.client.RpcClient
-
- Direct Known Subclasses:
JsonRpcClient
public class RpcClient extends Object
Convenience class which manages simple RPC-style communication. The class is agnostic about the format of RPC arguments / return values. It simply provides a mechanism for sending a message to an exchange with a given routing key, and waiting for a response.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
RpcClient.CorrelationIdSupplier
Contract to generate correlation IDs.static class
RpcClient.Response
The response object is an envelope that contains all of the data provided to the `handleDelivery` consumerstatic interface
RpcClient.RpcClientReplyHandler
Behavior to handle RPC reply.
-
Field Summary
Fields Modifier and Type Field Description static RpcClient.RpcClientReplyHandler
DEFAULT_REPLY_HANDLER
protected static int
NO_TIMEOUT
NO_TIMEOUT value must match convention onBlockingCell.uninterruptibleGet(int)
-
Constructor Summary
Constructors Constructor Description RpcClient(Channel channel, String exchange, String routingKey)
Deprecated.useRpcClient(RpcClientParams)
insteadRpcClient(Channel channel, String exchange, String routingKey, int timeout)
Deprecated.useRpcClient(RpcClientParams)
insteadRpcClient(Channel channel, String exchange, String routingKey, String replyTo)
Deprecated.useRpcClient(RpcClientParams)
insteadRpcClient(Channel channel, String exchange, String routingKey, String replyTo, int timeout)
Deprecated.useRpcClient(RpcClientParams)
insteadRpcClient(RpcClientParams params)
Construct aRpcClient
with the passed-inRpcClientParams
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
checkConsumer()
Private API - ensures the RpcClient is correctly open.void
close()
Public API - cancels the consumer, thus deleting the temporary queue, and marks the RpcClient as closed.RpcClient.Response
doCall(AMQP.BasicProperties props, byte[] message)
RpcClient.Response
doCall(AMQP.BasicProperties props, byte[] message, int timeout)
Channel
getChannel()
Retrieve the channel.Consumer
getConsumer()
Retrieve the consumer.Map<String,BlockingCell<Object>>
getContinuationMap()
Retrieve the continuation map.int
getCorrelationId()
Retrieve the last correlation id used.String
getExchange()
Retrieve the exchange.String
getRoutingKey()
Retrieve the routing key.static RpcClient.CorrelationIdSupplier
incrementingCorrelationIdSupplier()
Creates generation IDs as a sequence of integers.static RpcClient.CorrelationIdSupplier
incrementingCorrelationIdSupplier(String prefix)
Creates generation IDs as a sequence of integers, with the provided prefix.Map<String,Object>
mapCall(Object[] keyValuePairs)
Perform an AMQP wire-protocol-table based RPC roundtrip, first constructing the table from an array of alternating keys (in even-numbered elements, starting at zero) and values (in odd-numbered elements, starting at one)
Restrictions on value arguments apply as inmapCall(Map)
.Map<String,Object>
mapCall(Map<String,Object> message)
Perform an AMQP wire-protocol-table based RPC roundtrip
There are some restrictions on the values appearing in the table:
they must be of typeString
,LongString
,Integer
,BigDecimal
,Date
, or (recursively) aMap
of the enclosing type.byte[]
primitiveCall(byte[] message)
Perform a simple byte-array-based RPC roundtrip.byte[]
primitiveCall(AMQP.BasicProperties props, byte[] message)
byte[]
primitiveCall(AMQP.BasicProperties props, byte[] message, int timeout)
void
publish(AMQP.BasicProperties props, byte[] message)
RpcClient.Response
responseCall(byte[] message)
Perform a simple byte-array-based RPC roundtrip Useful if you need to get at more than just the body of the messageRpcClient.Response
responseCall(byte[] message, int timeout)
Perform a simple byte-array-based RPC roundtrip Useful if you need to get at more than just the body of the messageprotected DefaultConsumer
setupConsumer()
Registers a consumer on the reply queue.String
stringCall(String message)
Perform a simple string-based RPC roundtrip.
-
-
-
Field Detail
-
NO_TIMEOUT
protected static final int NO_TIMEOUT
NO_TIMEOUT value must match convention onBlockingCell.uninterruptibleGet(int)
- See Also:
- Constant Field Values
-
DEFAULT_REPLY_HANDLER
public static final RpcClient.RpcClientReplyHandler DEFAULT_REPLY_HANDLER
-
-
Constructor Detail
-
RpcClient
public RpcClient(RpcClientParams params) throws IOException
Construct aRpcClient
with the passed-inRpcClientParams
.- Parameters:
params
-- Throws:
IOException
- Since:
- 4.10.0
- See Also:
RpcClientParams
-
RpcClient
@Deprecated public RpcClient(Channel channel, String exchange, String routingKey, String replyTo, int timeout) throws IOException
Deprecated.useRpcClient(RpcClientParams)
insteadConstruct a new RpcClient that will communicate on the given channel, sending requests to the given exchange with the given routing key. Causes the creation of a temporary private autodelete queue. The name of this queue can be specified.- Parameters:
channel
- the channel to use for communicationexchange
- the exchange to connect toroutingKey
- the routing keyreplyTo
- the queue where the server should put the replytimeout
- milliseconds before timing out on wait for response- Throws:
IOException
- if an error is encountered
-
RpcClient
@Deprecated public RpcClient(Channel channel, String exchange, String routingKey, String replyTo) throws IOException
Deprecated.useRpcClient(RpcClientParams)
insteadConstruct a new RpcClient that will communicate on the given channel, sending requests to the given exchange with the given routing key. Causes the creation of a temporary private autodelete queue. The name of the queue can be provided (only relevant for RabbitMQ servers that do not support Direct Reply-to. Waits forever for responses (that is, no timeout).- Parameters:
channel
- the channel to use for communicationexchange
- the exchange to connect toroutingKey
- the routing keyreplyTo
- the queue where the server should put the reply- Throws:
IOException
- if an error is encountered
-
RpcClient
@Deprecated public RpcClient(Channel channel, String exchange, String routingKey) throws IOException
Deprecated.useRpcClient(RpcClientParams)
insteadConstruct a new RpcClient that will communicate on the given channel, sending requests to the given exchange with the given routing key. Direct Reply-to will be used for response propagation. Waits forever for responses (that is, no timeout).- Parameters:
channel
- the channel to use for communicationexchange
- the exchange to connect toroutingKey
- the routing key- Throws:
IOException
- if an error is encountered
-
RpcClient
@Deprecated public RpcClient(Channel channel, String exchange, String routingKey, int timeout) throws IOException
Deprecated.useRpcClient(RpcClientParams)
insteadConstruct a new RpcClient that will communicate on the given channel, sending requests to the given exchange with the given routing key.
Causes the creation of a temporary private autodelete queue. The name of this queue will be "amq.rabbitmq.reply-to".- Parameters:
channel
- the channel to use for communicationexchange
- the exchange to connect toroutingKey
- the routing keytimeout
- milliseconds before timing out on wait for response- Throws:
IOException
- if an error is encountered
-
-
Method Detail
-
checkConsumer
public void checkConsumer() throws IOException
Private API - ensures the RpcClient is correctly open.- Throws:
IOException
- if an error is encountered
-
close
public void close() throws IOException
Public API - cancels the consumer, thus deleting the temporary queue, and marks the RpcClient as closed.- Throws:
IOException
- if an error is encountered
-
setupConsumer
protected DefaultConsumer setupConsumer() throws IOException
Registers a consumer on the reply queue.- Returns:
- the newly created and registered consumer
- Throws:
IOException
- if an error is encountered
-
publish
public void publish(AMQP.BasicProperties props, byte[] message) throws IOException
- Throws:
IOException
-
doCall
public RpcClient.Response doCall(AMQP.BasicProperties props, byte[] message) throws IOException, TimeoutException
- Throws:
IOException
TimeoutException
-
doCall
public RpcClient.Response doCall(AMQP.BasicProperties props, byte[] message, int timeout) throws IOException, ShutdownSignalException, TimeoutException
-
primitiveCall
public byte[] primitiveCall(AMQP.BasicProperties props, byte[] message) throws IOException, ShutdownSignalException, TimeoutException
-
primitiveCall
public byte[] primitiveCall(AMQP.BasicProperties props, byte[] message, int timeout) throws IOException, ShutdownSignalException, TimeoutException
-
primitiveCall
public byte[] primitiveCall(byte[] message) throws IOException, ShutdownSignalException, TimeoutException
Perform a simple byte-array-based RPC roundtrip.- Parameters:
message
- the byte array request message to send- Returns:
- the byte array response received
- Throws:
ShutdownSignalException
- if the connection dies during our waitIOException
- if an error is encounteredTimeoutException
- if a response is not received within the configured timeout
-
responseCall
public RpcClient.Response responseCall(byte[] message) throws IOException, ShutdownSignalException, TimeoutException
Perform a simple byte-array-based RPC roundtrip Useful if you need to get at more than just the body of the message- Parameters:
message
- the byte array request message to send- Returns:
- The response object is an envelope that contains all of the data provided to the `handleDelivery` consumer
- Throws:
ShutdownSignalException
- if the connection dies during our waitIOException
- if an error is encounteredTimeoutException
- if a response is not received within the configured timeout
-
responseCall
public RpcClient.Response responseCall(byte[] message, int timeout) throws IOException, ShutdownSignalException, TimeoutException
Perform a simple byte-array-based RPC roundtrip Useful if you need to get at more than just the body of the message- Parameters:
message
- the byte array request message to sendtimeout
- milliseconds before timing out on wait for response- Returns:
- The response object is an envelope that contains all of the data provided to the `handleDelivery` consumer
- Throws:
ShutdownSignalException
- if the connection dies during our waitIOException
- if an error is encounteredTimeoutException
- if a response is not received within the configured timeout
-
stringCall
public String stringCall(String message) throws IOException, ShutdownSignalException, TimeoutException
Perform a simple string-based RPC roundtrip.- Parameters:
message
- the string request message to send- Returns:
- the string response received
- Throws:
ShutdownSignalException
- if the connection dies during our waitIOException
- if an error is encounteredTimeoutException
- if a timeout occurs before the response is received
-
mapCall
public Map<String,Object> mapCall(Map<String,Object> message) throws IOException, ShutdownSignalException, TimeoutException
Perform an AMQP wire-protocol-table based RPC roundtrip
There are some restrictions on the values appearing in the table:
they must be of typeString
,LongString
,Integer
,BigDecimal
,Date
, or (recursively) aMap
of the enclosing type.- Parameters:
message
- the table to send- Returns:
- the table received
- Throws:
ShutdownSignalException
- if the connection dies during our waitIOException
- if an error is encounteredTimeoutException
- if a timeout occurs before a response is received
-
mapCall
public Map<String,Object> mapCall(Object[] keyValuePairs) throws IOException, ShutdownSignalException, TimeoutException
Perform an AMQP wire-protocol-table based RPC roundtrip, first constructing the table from an array of alternating keys (in even-numbered elements, starting at zero) and values (in odd-numbered elements, starting at one)
Restrictions on value arguments apply as inmapCall(Map)
.- Parameters:
keyValuePairs
- alternating {key, value, key, value, ...} data to send- Returns:
- the table received
- Throws:
ShutdownSignalException
- if the connection dies during our waitIOException
- if an error is encounteredTimeoutException
- if a timeout occurs before a response is received
-
getChannel
public Channel getChannel()
Retrieve the channel.- Returns:
- the channel to which this client is connected
-
getExchange
public String getExchange()
Retrieve the exchange.- Returns:
- the exchange to which this client is connected
-
getRoutingKey
public String getRoutingKey()
Retrieve the routing key.- Returns:
- the routing key for messages to this client
-
getContinuationMap
public Map<String,BlockingCell<Object>> getContinuationMap()
Retrieve the continuation map.- Returns:
- the map of objects to blocking cells for this client
-
getCorrelationId
public int getCorrelationId()
Retrieve the last correlation id used.Note as of 4.12.0, correlation IDs may not always be integers (by default, they are). This method will try to parse the last correlation ID string as an integer, so this may result in
NumberFormatException
if the correlation ID supplier provided byRpcClientParams.correlationIdSupplier(CorrelationIdSupplier)
does not generate appropriate IDs.- Returns:
- the most recently used correlation id
- See Also:
RpcClientParams.correlationIdSupplier(CorrelationIdSupplier)
-
getConsumer
public Consumer getConsumer()
Retrieve the consumer.- Returns:
- an interface to the client's consumer object
-
incrementingCorrelationIdSupplier
public static RpcClient.CorrelationIdSupplier incrementingCorrelationIdSupplier()
Creates generation IDs as a sequence of integers.- Returns:
- Since:
- 4.12.0
- See Also:
RpcClientParams.correlationIdSupplier(CorrelationIdSupplier)
-
incrementingCorrelationIdSupplier
public static RpcClient.CorrelationIdSupplier incrementingCorrelationIdSupplier(String prefix)
Creates generation IDs as a sequence of integers, with the provided prefix.- Parameters:
prefix
-- Returns:
- Since:
- 4.12.0
- See Also:
RpcClientParams.correlationIdSupplier(CorrelationIdSupplier)
-
-