Search in sources :

Example 6 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 7 with ShutdownSignalException

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

the class CompetingReceiver method receive.

public String receive() {
    if (channel == null) {
        initialize();
    }
    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 8 with ShutdownSignalException

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

the class ChannelLimitNegotiation method openingTooManyChannels.

@Test
public void openingTooManyChannels() throws Exception {
    int n = 48;
    Connection conn = null;
    try {
        Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, " + n + ").'");
        ConnectionFactory cf = TestUtils.connectionFactory();
        conn = cf.newConnection();
        assertEquals(n, conn.getChannelMax());
        for (int i = 1; i <= n; i++) {
            assertNotNull(conn.createChannel(i));
        }
        // ChannelManager guards against channel.open being sent
        assertNull(conn.createChannel(n + 1));
        // Construct a channel directly
        final ChannelN ch = new ChannelN(((AutorecoveringConnection) conn).getDelegate(), n + 1, new ConsumerWorkService(Executors.newSingleThreadExecutor(), Executors.defaultThreadFactory(), ConnectionFactory.DEFAULT_SHUTDOWN_TIMEOUT));
        conn.addShutdownListener(new ShutdownListener() {

            public void shutdownCompleted(ShutdownSignalException cause) {
                // make sure channel.open continuation is released
                ch.processShutdownSignal(cause, true, true);
            }
        });
        ch.open();
        fail("expected channel.open to cause a connection exception");
    } catch (IOException e) {
        checkShutdownSignal(530, e);
    } finally {
        TestUtils.abort(conn);
        Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, 0).'");
    }
}
Also used : ShutdownListener(com.rabbitmq.client.ShutdownListener) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) ChannelN(com.rabbitmq.client.impl.ChannelN) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) ConsumerWorkService(com.rabbitmq.client.impl.ConsumerWorkService) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with ShutdownSignalException

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

the class Bug19219Test method it.

@Test
public void it() throws IOException, InterruptedException {
    final Consumer c = new DefaultConsumer(channel);
    // each.
    for (int i = 0; i < Q_COUNT; i++) {
        String qName = channel.queueDeclare().getQueue();
        channel.queueBind(qName, "amq.fanout", "");
        channel.basicConsume(qName, false, c);
    }
    // 2. send lots of messages in background, to keep the server,
    // and especially the queues, busy
    final Runnable r = new Runnable() {

        public void run() {
            try {
                startPublisher();
            } catch (IOException e) {
            } catch (InterruptedException e) {
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
        }
    };
    for (int i = 0; i < PUB_THREAD_COUNT; i++) {
        final Thread t = new Thread(r);
        t.start();
        // wait for thread to finish initialisation
        init.acquire();
    }
    // tell all threads to resume
    resume.countDown();
    // wait for threads to get into full swing
    Thread.sleep(CLOSE_DELAY);
    // 3. close channel. This will result in the server notifying
    // all the queues in parallel, which in turn will requeue all
    // the messages. The combined workload may result in some
    // notifications timing out.
    boolean success = false;
    try {
        channel.abort();
        success = true;
    } catch (ShutdownSignalException e) {
    } finally {
        // We deliberately do not perform a clean shutdown of all
        // the connections. This test is pushing the server really
        // hard, so we chose the quickest way to end things.
        channel = null;
        connection = null;
        assertTrue(success);
    }
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 10 with ShutdownSignalException

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

the class QueueingConsumerTests method nThreadShutdown.

@Test
public void nThreadShutdown() throws Exception {
    Channel channel = connection.createChannel();
    final QueueingConsumer c = new QueueingConsumer(channel);
    channel.queueDeclare(QUEUE, false, true, true, null);
    channel.basicConsume(QUEUE, c);
    final AtomicInteger count = new AtomicInteger(THREADS);
    final CountDownLatch latch = new CountDownLatch(THREADS);
    for (int i = 0; i < THREADS; i++) {
        new Thread() {

            @Override
            public void run() {
                try {
                    while (true) {
                        c.nextDelivery();
                    }
                } catch (ShutdownSignalException sig) {
                    count.decrementAndGet();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } finally {
                    latch.countDown();
                }
            }
        }.start();
    }
    connection.close();
    // Far longer than this could reasonably take
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertEquals(0, count.get());
}
Also used : ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(com.rabbitmq.client.Channel) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) IOException(java.io.IOException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) Test(org.junit.Test)

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