com.rabbitmq.client.RpcClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of amqp-client Show documentation
Show all versions of amqp-client Show documentation
The RabbitMQ Java client library allows Java applications to interface with RabbitMQ.
// Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// [email protected].
package com.rabbitmq.client;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.impl.MethodArgumentReader;
import com.rabbitmq.client.impl.MethodArgumentWriter;
import com.rabbitmq.client.impl.ValueReader;
import com.rabbitmq.client.impl.ValueWriter;
import com.rabbitmq.utility.BlockingCell;
/**
* 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.
*/
public class RpcClient {
/** Channel we are communicating on */
private final Channel _channel;
/** Exchange to send requests to */
private final String _exchange;
/** Routing key to use for requests */
private final String _routingKey;
/** Queue where the server should put the reply */
private final String _replyTo;
/** timeout to use on call responses */
private final int _timeout;
/** NO_TIMEOUT value must match convention on {@link BlockingCell#uninterruptibleGet(int)} */
protected final static int NO_TIMEOUT = -1;
/** Map from request correlation ID to continuation BlockingCell */
private final Map> _continuationMap = new HashMap>();
/** Contains the most recently-used request correlation ID */
private int _correlationId;
/** Consumer attached to our reply queue */
private DefaultConsumer _consumer;
/**
* 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.
* @param channel the channel to use for communication
* @param exchange the exchange to connect to
* @param routingKey the routing key
* @param replyTo the queue where the server should put the reply
* @param timeout milliseconds before timing out on wait for response
* @throws IOException if an error is encountered
*/
public RpcClient(Channel channel, String exchange, String routingKey, String replyTo, int timeout) throws
IOException {
_channel = channel;
_exchange = exchange;
_routingKey = routingKey;
_replyTo = replyTo;
if (timeout < NO_TIMEOUT) throw new IllegalArgumentException("Timeout arguument must be NO_TIMEOUT(-1) or non-negative.");
_timeout = timeout;
_correlationId = 0;
_consumer = setupConsumer();
}
/**
* 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).
* @param channel the channel to use for communication
* @param exchange the exchange to connect to
* @param routingKey the routing key
* @param replyTo the queue where the server should put the reply
* @throws IOException if an error is encountered
*/
public RpcClient(Channel channel, String exchange, String routingKey, String replyTo) throws IOException {
this(channel, exchange, routingKey, replyTo, NO_TIMEOUT);
}
/**
* 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).
* @param channel the channel to use for communication
* @param exchange the exchange to connect to
* @param routingKey the routing key
* @throws IOException if an error is encountered
*/
public RpcClient(Channel channel, String exchange, String routingKey) throws IOException {
this(channel, exchange, routingKey, "amq.rabbitmq.reply-to", NO_TIMEOUT);
}
/**
*
* 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".
* @param channel the channel to use for communication
* @param exchange the exchange to connect to
* @param routingKey the routing key
* @param timeout milliseconds before timing out on wait for response
* @throws IOException if an error is encountered
*/
public RpcClient(Channel channel, String exchange, String routingKey, int timeout) throws IOException {
this(channel, exchange, routingKey, "amq.rabbitmq.reply-to", timeout);
}
/**
* Private API - ensures the RpcClient is correctly open.
* @throws IOException if an error is encountered
*/
public void checkConsumer() throws IOException {
if (_consumer == null) {
throw new EOFException("RpcClient is closed");
}
}
/**
* Public API - cancels the consumer, thus deleting the temporary queue, and marks the RpcClient as closed.
* @throws IOException if an error is encountered
*/
public void close() throws IOException {
if (_consumer != null) {
_channel.basicCancel(_consumer.getConsumerTag());
_consumer = null;
}
}
/**
* Registers a consumer on the reply queue.
* @throws IOException if an error is encountered
* @return the newly created and registered consumer
*/
protected DefaultConsumer setupConsumer() throws IOException {
DefaultConsumer consumer = new DefaultConsumer(_channel) {
@Override
public void handleShutdownSignal(String consumerTag,
ShutdownSignalException signal) {
synchronized (_continuationMap) {
for (Entry> entry : _continuationMap.entrySet()) {
entry.getValue().set(signal);
}
_consumer = null;
}
}
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body)
throws IOException {
synchronized (_continuationMap) {
String replyId = properties.getCorrelationId();
BlockingCell