Search in sources :

Example 1 with ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException 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 ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException in project rabbitmq-jms-client by rabbitmq.

the class RMQSession method getBrowsingChannel.

/**
 * Get a (new) channel for queue browsing.
 * @return channel for browsing queues
 * @throws JMSException if channel not available
 */
Channel getBrowsingChannel() throws JMSException {
    try {
        synchronized (this.bcLock) {
            // not transactional
            Channel chan = this.getConnection().createRabbitChannel(false);
            this.browsingChannels.add(chan);
            return chan;
        }
    } catch (Exception e) {
        // includes unchecked exceptions, e.g. ShutdownSignalException
        throw new RMQJMSException("Cannot create browsing channel", e);
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) Channel(com.rabbitmq.client.Channel) TimeoutException(java.util.concurrent.TimeoutException) RMQJMSSelectorException(com.rabbitmq.jms.util.RMQJMSSelectorException) IllegalStateException(javax.jms.IllegalStateException) JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) IOException(java.io.IOException) InvalidSelectorException(javax.jms.InvalidSelectorException)

Example 3 with ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException in project rabbitmq-jms-client by rabbitmq.

the class MessageListenerConsumer method stop.

@Override
public void stop() {
    String cT = this.getConsTag();
    logger.trace("consumerTag='{}'", cT);
    TimeTracker tt = new TimeTracker(this.terminationTimeout, TimeUnit.NANOSECONDS);
    try {
        if (!this.completion.isComplete()) {
            logger.debug("consumerTag='{}' basicCancel:", cT);
            this.channel.basicCancel(cT);
            this.completion.waitUntilComplete(tt);
            this.clearConsTag();
        }
    } catch (TimeoutException te) {
        Thread.currentThread().interrupt();
    } catch (ShutdownSignalException sse) {
        // TODO check if basicCancel really necessary in this case.
        if (!sse.isInitiatedByApplication()) {
            logger.error("basicCancel (consumerTag='{}') threw exception", cT, sse);
            throw sse;
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (IOException e) {
        if (!e.getMessage().equals("Unknown consumerTag")) {
            logger.error("basicCancel (consumerTag='{}') threw unexpected exception", cT, e);
        }
    }
}
Also used : ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) TimeTracker(com.rabbitmq.jms.util.TimeTracker) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException 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 5 with ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException 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)

Aggregations

ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)24 IOException (java.io.IOException)20 ConsumerCancelledException (com.rabbitmq.client.ConsumerCancelledException)12 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)12 Channel (com.rabbitmq.client.Channel)5 Connection (com.rabbitmq.client.Connection)4 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)4 ShutdownListener (com.rabbitmq.client.ShutdownListener)4 TimeoutException (java.util.concurrent.TimeoutException)4 Test (org.junit.Test)4 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)3 AMQP (com.rabbitmq.client.AMQP)2 RMQJMSSelectorException (com.rabbitmq.jms.util.RMQJMSSelectorException)2 IllegalStateException (javax.jms.IllegalStateException)2 InvalidSelectorException (javax.jms.InvalidSelectorException)2 JMSException (javax.jms.JMSException)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 Consumer (com.rabbitmq.client.Consumer)1 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 GetResponse (com.rabbitmq.client.GetResponse)1