Example usage for com.rabbitmq.client Consumer handleCancelOk

List of usage examples for com.rabbitmq.client Consumer handleCancelOk

Introduction

In this page you can find the example usage for com.rabbitmq.client Consumer handleCancelOk.

Prototype

void handleCancelOk(String consumerTag);

Source Link

Document

Called when the consumer is cancelled by a call to Channel#basicCancel .

Usage

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

License:Apache License

@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  .  c  o 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

License:Apache License

@SuppressWarnings("unchecked")
@Test/*w  w  w  .j  a va  2  s.  c  o m*/
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);
}

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

License:Apache License

private Answer<Object> messageToConsumer(final Channel mockChannel,
        final SimpleMessageListenerContainer container, final boolean cancel, final CountDownLatch latch) {
    return new Answer<Object>() {

        @Override//from   w w  w.j  ava 2  s .  c om
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Set<?> consumers = TestUtils.getPropertyValue(container, "consumers", Map.class).keySet();
            for (Object consumer : consumers) {
                ChannelProxy channel = TestUtils.getPropertyValue(consumer, "channel", ChannelProxy.class);
                if (channel != null && channel.getTargetChannel() == mockChannel) {
                    Consumer rabbitConsumer = TestUtils.getPropertyValue(consumer, "consumer", Consumer.class);
                    if (cancel) {
                        rabbitConsumer.handleCancelOk((String) invocation.getArguments()[0]);
                    } else {
                        rabbitConsumer.handleConsumeOk("foo");
                    }
                    latch.countDown();
                }
            }
            return null;
        }
    };

}