Search in sources :

Example 1 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project rabbitmq-java-client by rabbitmq.

the class QosScaling method run.

public long run() throws IOException, TimeoutException {
    connectionFactory.setHost(params.host);
    connectionFactory.setPort(params.port);
    connection = connectionFactory.newConnection();
    channel = connection.createChannel();
    channel.basicQos(1);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    try {
        publish(consume(consumer));
        return drain(consumer);
    } finally {
        connection.abort();
    }
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer)

Example 2 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project wso2-axis2-transports by wso2.

the class RabbitMQRPCMessageSender method processResponse.

private RabbitMQMessage processResponse(String correlationID) throws IOException {
    QueueingConsumer consumer = dualChannel.getConsumer();
    QueueingConsumer.Delivery delivery = null;
    RabbitMQMessage message = new RabbitMQMessage();
    String replyToQueue = dualChannel.getReplyToQueue();
    String queueAutoDeclareStr = epProperties.get(RabbitMQConstants.QUEUE_AUTODECLARE);
    boolean queueAutoDeclare = true;
    if (!StringUtils.isEmpty(queueAutoDeclareStr)) {
        queueAutoDeclare = Boolean.parseBoolean(queueAutoDeclareStr);
    }
    if (queueAutoDeclare && !RabbitMQUtils.isQueueAvailable(dualChannel.getChannel(), replyToQueue)) {
        log.info("Reply-to queue : " + replyToQueue + " not available, hence creating a new one");
        RabbitMQUtils.declareQueue(dualChannel, replyToQueue, epProperties);
    }
    int timeout = RabbitMQConstants.DEFAULT_REPLY_TO_TIMEOUT;
    String timeoutStr = epProperties.get(RabbitMQConstants.REPLY_TO_TIMEOUT);
    if (!StringUtils.isEmpty(timeoutStr)) {
        try {
            timeout = Integer.parseInt(timeoutStr);
        } catch (NumberFormatException e) {
            log.warn("Number format error in reading replyto timeout value. Proceeding with default value (30000ms)", e);
        }
    }
    try {
        if (log.isDebugEnabled()) {
            log.debug("Waiting for delivery from reply to queue " + replyToQueue + " corr id : " + correlationID);
        }
        delivery = consumer.nextDelivery(timeout);
        if (delivery != null) {
            if (!StringUtils.isEmpty(delivery.getProperties().getCorrelationId())) {
                if (delivery.getProperties().getCorrelationId().equals(correlationID)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Found matching response with correlation ID : " + correlationID + ".");
                    }
                } else {
                    log.error("Response not queued in " + replyToQueue + " for correlation ID : " + correlationID);
                    return null;
                }
            }
        } else {
            log.error("Response not queued in " + replyToQueue);
        }
    } catch (ShutdownSignalException e) {
        log.error("Error receiving message from RabbitMQ broker " + e.getLocalizedMessage());
    } catch (InterruptedException e) {
        log.error("Error receiving message from RabbitMQ broker " + e.getLocalizedMessage());
    } catch (ConsumerCancelledException e) {
        log.error("Error receiving message from RabbitMQ broker" + e.getLocalizedMessage());
    }
    if (delivery != null) {
        log.debug("Processing response from reply-to queue");
        AMQP.BasicProperties properties = delivery.getProperties();
        Map<String, Object> headers = properties.getHeaders();
        message.setBody(delivery.getBody());
        message.setDeliveryTag(delivery.getEnvelope().getDeliveryTag());
        message.setReplyTo(properties.getReplyTo());
        message.setMessageId(properties.getMessageId());
        // get content type from message
        String contentType = properties.getContentType();
        if (contentType == null) {
            // if not get content type from transport parameter
            contentType = epProperties.get(RabbitMQConstants.REPLY_TO_CONTENT_TYPE);
            if (contentType == null) {
                // if none is given, set to default content type
                log.warn("Setting default content type " + RabbitMQConstants.DEFAULT_CONTENT_TYPE);
                contentType = RabbitMQConstants.DEFAULT_CONTENT_TYPE;
            }
        }
        message.setContentType(contentType);
        message.setContentEncoding(properties.getContentEncoding());
        message.setCorrelationId(properties.getCorrelationId());
        if (headers != null) {
            message.setHeaders(headers);
            if (headers.get(RabbitMQConstants.SOAP_ACTION) != null) {
                message.setSoapAction(headers.get(RabbitMQConstants.SOAP_ACTION).toString());
            }
        }
    }
    return message;
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) RabbitMQMessage(org.apache.axis2.transport.rabbitmq.RabbitMQMessage) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) AMQP(com.rabbitmq.client.AMQP)

Example 3 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project microservices by pwillhan.

the class FailoverReceiver method receive.

public void receive() {
    Connection connection = null;
    Channel channel = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(REQUEST_QUEUE, false, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        LOGGER.info("Request received: " + message);
        channel.txSelect();
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        channel.txCommit();
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        if (channel != null) {
            try {
                channel.txRollback();
            } catch (IOException re) {
                LOGGER.error("Rollback failed: " + re.getMessage(), re);
            }
        }
    } catch (ShutdownSignalException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ConsumerCancelledException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                LOGGER.warn("Failed to close connection: " + e.getMessage(), e);
            }
        }
    }
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Example 4 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project microservices by pwillhan.

the class WSO2Sender method waitForResponse.

public String waitForResponse(final String correlationId) {
    QueueingConsumer consumer = new QueueingConsumer(channel);
    String result = null;
    try {
        channel.basicConsume(RESPONSE_QUEUE, true, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(3000);
        String message = new String(delivery.getBody());
        if (delivery.getProperties() != null) {
            String msgCorrelationId = delivery.getProperties().getCorrelationId();
            if (!correlationId.equals(msgCorrelationId)) {
                LOGGER.warn("Received response of another request.");
            } else {
                result = message;
            }
        }
        LOGGER.info("Message received: " + message);
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ShutdownSignalException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ConsumerCancelledException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return result;
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Example 5 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project microservices by pwillhan.

the class Sender method waitForResponse.

public String waitForResponse(final String correlationId) {
    QueueingConsumer consumer = new QueueingConsumer(channel);
    String result = null;
    try {
        channel.basicConsume(RESPONSE_QUEUE, true, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(3000);
        String message = new String(delivery.getBody());
        if (delivery.getProperties() != null) {
            String msgCorrelationId = delivery.getProperties().getCorrelationId();
            if (!correlationId.equals(msgCorrelationId)) {
                LOGGER.warn("Received response of another request.");
            } else {
                result = message;
            }
        }
        LOGGER.info("Message received: " + message);
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ShutdownSignalException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ConsumerCancelledException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return result;
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Aggregations

QueueingConsumer (com.rabbitmq.client.QueueingConsumer)55 Test (org.junit.Test)30 IOException (java.io.IOException)13 ConsumerCancelledException (com.rabbitmq.client.ConsumerCancelledException)12 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)12 Channel (com.rabbitmq.client.Channel)11 Connection (com.rabbitmq.client.Connection)9 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)9 Delivery (com.rabbitmq.client.QueueingConsumer.Delivery)9 AMQP (com.rabbitmq.client.AMQP)2 ArrayList (java.util.ArrayList)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 Queue (com.rabbitmq.client.AMQP.Queue)1 Envelope (com.rabbitmq.client.Envelope)1 GetResponse (com.rabbitmq.client.GetResponse)1 List (java.util.List)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RabbitMQMessage (org.apache.axis2.transport.rabbitmq.RabbitMQMessage)1