Example usage for org.springframework.amqp.rabbit.listener DirectMessageListenerContainer setShutdownTimeout

List of usage examples for org.springframework.amqp.rabbit.listener DirectMessageListenerContainer setShutdownTimeout

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.listener DirectMessageListenerContainer setShutdownTimeout.

Prototype

public void setShutdownTimeout(long shutdownTimeout) 

Source Link

Document

The time to wait for workers in milliseconds after the container is stopped.

Usage

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

@Test
public void testRecoverBrokerLoss() throws Exception {
    ConnectionFactory mockCF = mock(ConnectionFactory.class);
    Connection connection = mock(Connection.class);
    Channel channel = mock(Channel.class);
    given(connection.isOpen()).willReturn(true);
    given(mockCF.createConnection()).willReturn(connection);
    given(connection.createChannel(false)).willReturn(channel);
    given(channel.isOpen()).willReturn(true);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class);
    final String tag = "tag";
    willAnswer(i -> {//from  www . j  a v  a  2s .c o m
        latch1.countDown();
        latch2.countDown();
        return tag;
    }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            consumerCaptor.capture());
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF);
    container.setQueueNames("foo");
    container.setBeanName("brokerLost");
    container.setConsumerTagStrategy(q -> "tag");
    container.setShutdownTimeout(1);
    container.setMonitorInterval(200);
    container.setFailedDeclarationRetryInterval(200);
    container.afterPropertiesSet();
    container.start();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    given(channel.isOpen()).willReturn(false);
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    container.setShutdownTimeout(1);
    container.stop();
}

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

@Test
public void testCancelConsumerBeforeConsumeOk() throws Exception {
    ConnectionFactory mockCF = mock(ConnectionFactory.class);
    Connection connection = mock(Connection.class);
    Channel channel = mock(Channel.class);
    given(connection.isOpen()).willReturn(true);
    given(mockCF.createConnection()).willReturn(connection);
    given(connection.createChannel(false)).willReturn(channel);
    given(channel.isOpen()).willReturn(true);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class);
    final String tag = "tag";
    willAnswer(i -> {/*from   w  w w.  j  a  v  a  2 s . co  m*/
        latch1.countDown();
        return tag;
    }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            consumerCaptor.capture());
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF);
    container.setQueueNames("foo");
    container.setBeanName("backOff");
    container.setConsumerTagStrategy(q -> "tag");
    container.setShutdownTimeout(1);
    container.afterPropertiesSet();
    container.start();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    Consumer consumer = consumerCaptor.getValue();
    Executors.newSingleThreadExecutor().execute(() -> {
        container.stop();
        latch2.countDown();
    });
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    verify(channel).basicCancel(tag); // canceled properly even without consumeOk
    consumer.handleCancelOk(tag);
}

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

@SuppressWarnings("unchecked")
@Test/*from ww  w  .  ja  v a 2s.  c  o  m*/
public void testRecoverBrokerLoss() throws Exception {
    ConnectionFactory mockCF = mock(ConnectionFactory.class);
    Connection connection = mock(Connection.class);
    Channel channel = mock(Channel.class);
    given(connection.isOpen()).willReturn(true);
    given(mockCF.createConnection()).willReturn(connection);
    given(connection.createChannel(false)).willReturn(channel);
    given(channel.isOpen()).willReturn(true);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class);
    final String tag = "tag";
    willAnswer(i -> {
        latch1.countDown();
        latch2.countDown();
        return tag;
    }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            consumerCaptor.capture());
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF);
    container.setQueueNames("foo");
    container.setBeanName("brokerLost");
    container.setConsumerTagStrategy(q -> "tag");
    container.setShutdownTimeout(1);
    container.setMonitorInterval(200);
    container.setFailedDeclarationRetryInterval(200);
    container.afterPropertiesSet();
    container.start();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    given(channel.isOpen()).willReturn(false);
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    container.setShutdownTimeout(1);
    container.stop();
}

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

@SuppressWarnings("unchecked")
@Test/*from  www  . jav a2 s  . c om*/
public void testCancelConsumerBeforeConsumeOk() throws Exception {
    ConnectionFactory mockCF = mock(ConnectionFactory.class);
    Connection connection = mock(Connection.class);
    Channel channel = mock(Channel.class);
    given(connection.isOpen()).willReturn(true);
    given(mockCF.createConnection()).willReturn(connection);
    given(connection.createChannel(false)).willReturn(channel);
    given(channel.isOpen()).willReturn(true);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class);
    final String tag = "tag";
    willAnswer(i -> {
        latch1.countDown();
        return tag;
    }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            consumerCaptor.capture());
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF);
    container.setQueueNames("foo");
    container.setBeanName("backOff");
    container.setConsumerTagStrategy(q -> "tag");
    container.setShutdownTimeout(1);
    container.afterPropertiesSet();
    container.start();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    Consumer consumer = consumerCaptor.getValue();
    Executors.newSingleThreadExecutor().execute(() -> {
        container.stop();
        latch2.countDown();
    });
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    verify(channel).basicCancel(tag); // canceled properly even without consumeOk
    consumer.handleCancelOk(tag);
}