Package com.rabbitmq.client
Class RpcClient
java.lang.Object
com.rabbitmq.client.RpcClient
- 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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intNO_TIMEOUT value must match convention onBlockingCell.uninterruptibleGet(int) -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new RpcClient that will communicate on the given channel, sending requests to the given exchange with the given routing key.Construct a new RpcClient that will communicate on the given channel, sending requests to the given exchange with the given routing key.Construct a new RpcClient that will communicate on the given channel, sending requests to the given exchange with the given routing key.Construct a new RpcClient that will communicate on the given channel, sending requests to the given exchange with the given routing key. -
Method Summary
Modifier and TypeMethodDescriptionvoidPrivate API - ensures the RpcClient is correctly open.voidclose()Public API - cancels the consumer, thus deleting the temporary queue, and marks the RpcClient as closed.Retrieve the channel.Retrieve the consumer.Retrieve the continuation map.intRetrieve the correlation id.Retrieve the exchange.Retrieve the routing key.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) aMapof the enclosing type.byte[]primitiveCall(byte[] message) Perform a simple byte-array-based RPC roundtrip.byte[]primitiveCall(AMQP.BasicProperties props, byte[] message) voidpublish(AMQP.BasicProperties props, byte[] message) protected DefaultConsumerRegisters 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:
-
-
Constructor Details
-
RpcClient
public RpcClient(Channel channel, String exchange, String routingKey, String replyTo, int timeout) throws IOException Construct 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
public RpcClient(Channel channel, String exchange, String routingKey, String replyTo) throws IOException Construct 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
Construct 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
public RpcClient(Channel channel, String exchange, String routingKey, int timeout) throws IOException Construct 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
-
checkConsumer
Private API - ensures the RpcClient is correctly open.- Throws:
IOException- if an error is encountered
-
close
Public API - cancels the consumer, thus deleting the temporary queue, and marks the RpcClient 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
-
publish
- Throws:
IOException
-
primitiveCall
public byte[] primitiveCall(AMQP.BasicProperties props, byte[] message) 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
-
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) aMapof 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 correlation id.- Returns:
- the most recently used correlation id
-
getConsumer
Retrieve the consumer.- Returns:
- an interface to the client's consumer object
-