Package com.rabbitmq.client
Class RpcClient
java.lang.Object
com.rabbitmq.client.RpcClient
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
JsonRpcClient
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
Modifier and TypeClassDescriptionstatic class
The response object is an envelope that contains all of the data provided to the `handleDelivery` consumer -
Field Summary
Modifier and TypeFieldDescriptionstatic final Function
<Object, RpcClient.Response> protected static final int
NO_TIMEOUT value must match convention onBlockingCell.uninterruptibleGet(int)
-
Constructor Summary
ConstructorDescriptionDeprecated.Deprecated.useRpcClient(RpcClientParams)
instead, will be removed in 6.0.0Deprecated.useRpcClient(RpcClientParams)
instead, will be removed in 6.0.0Deprecated.useRpcClient(RpcClientParams)
instead, will be removed in 6.0.0RpcClient
(RpcClientParams params) Construct aRpcClient
with the passed-inRpcClientParams
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Public API - cancels the consumer, thus deleting the temporary queue, and marks the RpcClient as closed.doCall
(AMQP.BasicProperties props, byte[] message) doCall
(AMQP.BasicProperties props, byte[] message, int timeout) Retrieve the channel.Retrieve the consumer.Retrieve the continuation map.int
Retrieve the last correlation id used.Retrieve the exchange.Retrieve the routing key.Creates generation IDs as a sequence of integers.Creates generation IDs as a sequence of integers, with the provided prefix.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)
.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) 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 messageresponseCall
(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
Registers a consumer on the reply queue.stringCall
(String message) Perform a simple string-based RPC roundtrip.
-
Field Details
-
NO_TIMEOUT
protected static final int NO_TIMEOUTNO_TIMEOUT value must match convention onBlockingCell.uninterruptibleGet(int)
- See Also:
-
DEFAULT_REPLY_HANDLER
-
-
Constructor Details
-
RpcClient
Construct aRpcClient
with the passed-inRpcClientParams
.- Parameters:
params
-- Throws:
IOException
- Since:
- 5.6.0
- See Also:
-
RpcClient
@Deprecated public RpcClient(Channel channel, String exchange, String routingKey, String replyTo, int timeout) throws IOException Deprecated.useRpcClient(RpcClientParams)
instead, will be removed in 6.0.0Construct 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)
instead, will be removed in 6.0.0Construct 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)
instead, will be removed in 6.0.0Construct 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)
instead, will be removed in 6.0.0Construct 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 Details
-
close
Public API - cancels the consumer, thus deleting the temporary queue, and marks the RpcClient as closed.- Specified by:
close
in interfaceAutoCloseable
- 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
-
publish
- 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, TimeoutExceptionPerform 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
Retrieve the channel.- Returns:
- the channel to which this client is connected
-
getExchange
Retrieve the exchange.- Returns:
- the exchange to which this client is connected
-
getRoutingKey
Retrieve the routing key.- Returns:
- the routing key for messages to this client
-
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 5.9.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(Supplier)
does not generate appropriate IDs.- Returns:
- the most recently used correlation id
- See Also:
-
getConsumer
Retrieve the consumer.- Returns:
- an interface to the client's consumer object
-
incrementingCorrelationIdSupplier
Creates generation IDs as a sequence of integers.- Returns:
- Since:
- 5.9.0
- See Also:
-
incrementingCorrelationIdSupplier
Creates generation IDs as a sequence of integers, with the provided prefix.- Parameters:
prefix
-- Returns:
- Since:
- 5.9.0
- See Also:
-
RpcClient(RpcClientParams)
instead, will be removed in 6.0.0