Search in sources :

Example 1 with ConsumerCancelledException

use of com.rabbitmq.client.ConsumerCancelledException 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 2 with ConsumerCancelledException

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

the class QueueingConsumerTests method consumerCancellationInterruptsQueuingConsumerWait.

@Test
public void consumerCancellationInterruptsQueuingConsumerWait() throws IOException, InterruptedException {
    String queue = "cancel_notification_queue_for_queueing_consumer";
    final BlockingQueue<Boolean> result = new ArrayBlockingQueue<Boolean>(1);
    channel.queueDeclare(queue, false, true, false, null);
    final QueueingConsumer consumer = new QueueingConsumer(channel);
    Runnable receiver = new Runnable() {

        public void run() {
            try {
                try {
                    consumer.nextDelivery();
                } catch (ConsumerCancelledException e) {
                    result.put(true);
                    return;
                } catch (ShutdownSignalException e) {
                } catch (InterruptedException e) {
                }
                result.put(false);
            } catch (InterruptedException e) {
                fail();
            }
        }
    };
    Thread t = new Thread(receiver);
    t.start();
    channel.basicConsume(queue, consumer);
    channel.queueDelete(queue);
    assertTrue(result.take());
    t.join();
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Test(org.junit.Test)

Aggregations

ConsumerCancelledException (com.rabbitmq.client.ConsumerCancelledException)2 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)2 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)2 AMQP (com.rabbitmq.client.AMQP)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 RabbitMQMessage (org.apache.axis2.transport.rabbitmq.RabbitMQMessage)1 Test (org.junit.Test)1