Search in sources :

Example 16 with ShutdownSignalException

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

the class RequestReceiver method receive.

public void receive() {
    if (channel == null) {
        initialize();
    }
    String message = null;
    try {
        channel.queueDeclare(REQUEST_QUEUE, false, false, false, null);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(REQUEST_QUEUE, true, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        message = new String(delivery.getBody());
        LOGGER.info("Request received: " + message);
        // do something with the request message ...
        BasicProperties properties = delivery.getProperties();
        if (properties != null) {
            AMQP.BasicProperties amqpProps = new AMQP.BasicProperties();
            amqpProps = amqpProps.builder().correlationId(String.valueOf(properties.getCorrelationId())).build();
            channel.basicPublish(DEFAULT_QUEUE, properties.getReplyTo(), amqpProps, "Response message.".getBytes());
        } else {
            LOGGER.warn("Cannot determine response destination for 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);
    }
}
Also used : BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) AMQP(com.rabbitmq.client.AMQP) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Example 17 with ShutdownSignalException

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

the class ClusterReceiver method receive.

public String receive(Address... hosts) {
    if (channel == null) {
        initialize(hosts);
    }
    String message = null;
    try {
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, true, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        message = new String(delivery.getBody());
        LOGGER.info("Message received: " + message);
        return 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 message;
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Example 18 with ShutdownSignalException

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

the class TransactionalReceiver 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 19 with ShutdownSignalException

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

Example 20 with ShutdownSignalException

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

the class PublishSubscribeReceiver method receive.

public String receive(String queue) {
    if (channel == null) {
        initialize();
    }
    String message = null;
    try {
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        channel.queueDeclare(queue, false, false, false, null);
        channel.queueBind(queue, EXCHANGE_NAME, "");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queue, true, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        message = new String(delivery.getBody());
        LOGGER.info("Message received: " + message);
        return 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 message;
}
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