Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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 {
private static final Logger LOGGER = LoggerFactory.getLogger(RpcClient.class);
/** 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;
/** Whether to publish RPC requests with the mandatory flag or not. */
private final boolean _useMandatory;
public final static RpcClientReplyHandler DEFAULT_REPLY_HANDLER = new RpcClientReplyHandler() {
@Override
public Response handle(Object reply) {
if (reply instanceof ShutdownSignalException) {
ShutdownSignalException sig = (ShutdownSignalException) reply;
ShutdownSignalException wrapper =
new ShutdownSignalException(sig.isHardError(),
sig.isInitiatedByApplication(),
sig.getReason(),
sig.getReference());
wrapper.initCause(sig);
throw wrapper;
} else if (reply instanceof UnroutableRpcRequestException) {
throw (UnroutableRpcRequestException) reply;
} else {
return (Response) reply;
}
}
};
private final RpcClientReplyHandler _replyHandler;
/** 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 {@link RpcClient} with the passed-in {@link RpcClientParams}.
*
* @param params
* @throws IOException
* @see RpcClientParams
* @since 4.10.0
*/
public RpcClient(RpcClientParams params) throws
IOException {
_channel = params.getChannel();
_exchange = params.getExchange();
_routingKey = params.getRoutingKey();
_replyTo = params.getReplyTo();
if (params.getTimeout() < NO_TIMEOUT) {
throw new IllegalArgumentException("Timeout argument must be NO_TIMEOUT(-1) or non-negative.");
}
_timeout = params.getTimeout();
_useMandatory = params.shouldUseMandatory();
_replyHandler = params.getReplyHandler();
_correlationId = 0;
_consumer = setupConsumer();
if (_useMandatory) {
this._channel.addReturnListener(new ReturnListener() {
@Override
public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
synchronized (_continuationMap) {
String replyId = properties.getCorrelationId();
BlockingCell