Example usage for org.springframework.amqp AmqpConnectException AmqpConnectException

List of usage examples for org.springframework.amqp AmqpConnectException AmqpConnectException

Introduction

In this page you can find the example usage for org.springframework.amqp AmqpConnectException AmqpConnectException.

Prototype

public AmqpConnectException(Exception cause) 

Source Link

Document

Construct an instance with the supplied message and cause.

Usage

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

@Test
public void testCheckoutLimitWithFailures() throws Exception {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    final com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    final AtomicBoolean brokerDown = new AtomicBoolean();

    willAnswer(i -> {/*from   www  .  j  a v  a2 s  .c  o m*/
        if (brokerDown.get()) {
            throw new AmqpConnectException(null);
        }
        return mockConnection;
    }).given(mockConnectionFactory).newConnection((ExecutorService) isNull(), anyString());

    when(mockConnection.createChannel()).thenReturn(mockChannel1);

    doAnswer(i -> !brokerDown.get()).when(mockConnection).isOpen();

    // Called during physical close
    doAnswer(i -> !brokerDown.get()).when(mockChannel1).isOpen();

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setChannelCacheSize(1);
    ccf.setChannelCheckoutTimeout(10);

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);

    try {
        con.createChannel(false);
        fail("Exception expected");
    } catch (AmqpTimeoutException e) {
    }

    // should be ignored, and added last into channel cache.
    channel1.close();

    // remove first entry in cache (channel1)
    Channel ch1 = con.createChannel(false);

    assertSame(ch1, channel1);

    ch1.close();

    brokerDown.set(true);
    try {
        con.createChannel(false);
        fail("Exception expected");
    } catch (AmqpConnectException e) {
    }
    brokerDown.set(false);
    ch1 = con.createChannel(false);
    ch1.close();

    ccf.destroy();
}

From source file:org.springframework.amqp.rabbit.connection.RabbitUtils.java

public static RuntimeException convertRabbitAccessException(Throwable ex) {
    Assert.notNull(ex, "Exception must not be null");
    if (ex instanceof AmqpException) {
        return (AmqpException) ex;
    }/*from ww  w .  j a v  a  2 s.com*/
    if (ex instanceof ShutdownSignalException) {
        return new AmqpConnectException((ShutdownSignalException) ex);
    }
    if (ex instanceof ConnectException) {
        return new AmqpConnectException((ConnectException) ex);
    }
    if (ex instanceof IOException) {
        return new AmqpIOException((IOException) ex);
    }
    if (ex instanceof UnsupportedEncodingException) {
        return new AmqpUnsupportedEncodingException(ex);
    }
    // fallback
    return new UncategorizedAmqpException(ex);
}

From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer.java

private void doConsumeFromQueue(String queue) {
    if (!isActive()) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Consume from queue " + queue + " ignore, container stopping");
        }//from w  w  w .  j  ava2  s  .c  o  m
        return;
    }
    Connection connection = null;
    try {
        connection = getConnectionFactory().createConnection();
    } catch (Exception e) {
        this.consumersToRestart.add(new SimpleConsumer(null, null, queue));
        throw new AmqpConnectException(e);
    }
    Channel channel = null;
    SimpleConsumer consumer = null;
    try {
        channel = connection.createChannel(isChannelTransacted());
        channel.basicQos(getPrefetchCount());
        consumer = new SimpleConsumer(connection, channel, queue);
        channel.queueDeclarePassive(queue);
        consumer.consumerTag = channel.basicConsume(queue, getAcknowledgeMode().isAutoAck(),
                (getConsumerTagStrategy() != null ? getConsumerTagStrategy().createConsumerTag(queue) : ""),
                false, isExclusive(), getConsumerArguments(), consumer);
    } catch (IOException e) {
        if (channel != null) {
            RabbitUtils.closeChannel(channel);
        }
        if (connection != null) {
            RabbitUtils.closeConnection(connection);
        }
        if (e.getCause() instanceof ShutdownSignalException
                && e.getCause().getMessage().contains("in exclusive use")) {
            getExclusiveConsumerExceptionLogger().log(logger, "Exclusive consumer failure", e.getCause());
            publishConsumerFailedEvent("Consumer raised exception, attempting restart", false, e);
        } else if (this.logger.isDebugEnabled()) {
            this.logger.debug("Queue not present or basicConsume failed, scheduling consumer " + consumer
                    + " for restart");
        }
        this.consumersToRestart.add(consumer);
        consumer = null;
    }
    synchronized (this.consumersMonitor) {
        if (consumer != null) {
            this.cancellationLock.add(consumer);
            this.consumers.add(consumer);
            this.consumersByQueue.add(queue, consumer);
            if (this.logger.isInfoEnabled()) {
                this.logger.info(consumer + " started");
            }
        }
    }
}

From source file:org.springframework.amqp.rabbit.support.RabbitUtils.java

public static AmqpException convertRabbitAccessException(Throwable ex) {
    Assert.notNull(ex, "Exception must not be null");
    if (ex instanceof AmqpException) {
        return (AmqpException) ex;
    }/*from ww  w.j a  va2  s. c  o m*/
    if (ex instanceof ConnectException) {
        return new AmqpConnectException((ConnectException) ex);
    }
    if (ex instanceof IOException) {
        return new AmqpIOException((IOException) ex);
    }
    if (ex instanceof UnsupportedEncodingException) {
        return new AmqpUnsupportedEncodingException(ex);
    }
    // fallback
    return new UncategorizedAmqpException(ex);
}