Example usage for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer setReceiveTimeout

List of usage examples for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer setReceiveTimeout

Introduction

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

Prototype

public void setReceiveTimeout(long receiveTimeout) 

Source Link

Document

The time (in milliseconds) that a consumer should wait for data.

Usage

From source file:org.springframework.amqp.rabbit.core.BatchingRabbitTemplateTests.java

@Test
public void testDebatchByContainer() throws Exception {
    final List<Message> received = new ArrayList<Message>();
    final CountDownLatch latch = new CountDownLatch(2);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory);
    container.setQueueNames(ROUTE);/*from  ww w . jav  a2 s.  c o m*/
    container.setMessageListener((MessageListener) message -> {
        received.add(message);
        latch.countDown();
    });
    container.setReceiveTimeout(100);
    container.afterPropertiesSet();
    container.start();
    try {
        BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
        BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
        template.setConnectionFactory(this.connectionFactory);
        MessageProperties props = new MessageProperties();
        Message message = new Message("foo".getBytes(), props);
        template.send("", ROUTE, message);
        message = new Message("bar".getBytes(), props);
        template.send("", ROUTE, message);
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        assertEquals(2, received.size());
        assertEquals("foo", new String(received.get(0).getBody()));
        assertEquals(3, received.get(0).getMessageProperties().getContentLength());
        assertEquals("bar", new String(received.get(1).getBody()));
        assertEquals(3, received.get(0).getMessageProperties().getContentLength());
    } finally {
        container.stop();
    }
}

From source file:org.springframework.amqp.rabbit.core.BatchingRabbitTemplateTests.java

@Test
public void testDebatchByContainerPerformance() throws Exception {
    final List<Message> received = new ArrayList<Message>();
    int count = 100000;
    final CountDownLatch latch = new CountDownLatch(count);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory);
    container.setQueueNames(ROUTE);//ww  w.  ja v  a  2 s .c o m
    container.setMessageListener((MessageListener) message -> {
        received.add(message);
        latch.countDown();
    });
    container.setReceiveTimeout(100);
    container.setPrefetchCount(1000);
    container.setTxSize(1000);
    container.afterPropertiesSet();
    container.start();
    try {
        BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(1000, Integer.MAX_VALUE, 30000);
        BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
        //         RabbitTemplate template = new RabbitTemplate();
        template.setConnectionFactory(this.connectionFactory);
        MessageProperties props = new MessageProperties();
        props.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        Message message = new Message(new byte[256], props);
        StopWatch watch = new StopWatch();
        watch.start();
        for (int i = 0; i < count; i++) {
            template.send("", ROUTE, message);
        }
        assertTrue(latch.await(60, TimeUnit.SECONDS));
        watch.stop();
        // System .out .println(watch.getTotalTimeMillis());
        assertEquals(count, received.size());
    } finally {
        container.stop();
    }
}

From source file:org.springframework.amqp.rabbit.core.BatchingRabbitTemplateTests.java

@Test
public void testDebatchByContainerBadMessageRejected() throws Exception {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory);
    container.setQueueNames(ROUTE);//from   www .ja  v  a  2  s  . c o m
    container.setMessageListener((MessageListener) message -> {
    });
    container.setReceiveTimeout(100);
    ConditionalRejectingErrorHandler errorHandler = new ConditionalRejectingErrorHandler();
    container.setErrorHandler(errorHandler);
    container.afterPropertiesSet();
    container.start();
    Log logger = spy(TestUtils.getPropertyValue(errorHandler, "logger", Log.class));
    doReturn(true).when(logger).isWarnEnabled();
    doAnswer(new DoesNothing()).when(logger).warn(anyString(), any(Throwable.class));
    new DirectFieldAccessor(errorHandler).setPropertyValue("logger", logger);
    try {
        RabbitTemplate template = new RabbitTemplate();
        template.setConnectionFactory(this.connectionFactory);
        MessageProperties props = new MessageProperties();
        props.getHeaders().put(MessageProperties.SPRING_BATCH_FORMAT,
                MessageProperties.BATCH_FORMAT_LENGTH_HEADER4);
        Message message = new Message("\u0000\u0000\u0000\u0004foo".getBytes(), props);
        template.send("", ROUTE, message);
        Thread.sleep(1000);
        ArgumentCaptor<Object> arg1 = ArgumentCaptor.forClass(Object.class);
        ArgumentCaptor<Throwable> arg2 = ArgumentCaptor.forClass(Throwable.class);
        verify(logger).warn(arg1.capture(), arg2.capture());
        assertThat(arg2.getValue().getMessage(), containsString("Bad batched message received"));
    } finally {
        container.stop();
    }
}

From source file:org.springframework.amqp.rabbit.core.BatchingRabbitTemplateTests.java

@Test
public void testCompressionWithContainer() throws Exception {
    final List<Message> received = new ArrayList<Message>();
    final CountDownLatch latch = new CountDownLatch(2);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory);
    container.setQueueNames(ROUTE);// ww  w . ja va  2 s. com
    container.setMessageListener((MessageListener) message -> {
        received.add(message);
        latch.countDown();
    });
    container.setReceiveTimeout(100);
    container.setAfterReceivePostProcessors(new DelegatingDecompressingPostProcessor());
    container.afterPropertiesSet();
    container.start();
    try {
        BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
        BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
        template.setConnectionFactory(this.connectionFactory);
        template.setBeforePublishPostProcessors(new GZipPostProcessor());
        MessageProperties props = new MessageProperties();
        Message message = new Message("foo".getBytes(), props);
        template.send("", ROUTE, message);
        message = new Message("bar".getBytes(), props);
        template.send("", ROUTE, message);
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        assertEquals(2, received.size());
        assertEquals("foo", new String(received.get(0).getBody()));
        assertEquals(3, received.get(0).getMessageProperties().getContentLength());
        assertEquals("bar", new String(received.get(1).getBody()));
        assertEquals(3, received.get(0).getMessageProperties().getContentLength());
    } finally {
        container.stop();
    }
}

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

@Test
public void testChangeQueues() throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    Connection connection = mock(Connection.class);
    Channel channel1 = mock(Channel.class);
    Channel channel2 = mock(Channel.class);
    when(channel1.isOpen()).thenReturn(true);
    when(channel2.isOpen()).thenReturn(true);
    when(connectionFactory.createConnection()).thenReturn(connection);
    when(connection.createChannel(false)).thenReturn(channel1, channel2);
    List<Consumer> consumers = new ArrayList<Consumer>();
    AtomicInteger consumerTag = new AtomicInteger();
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(2);
    setupMockConsume(channel1, consumers, consumerTag, latch1);
    setUpMockCancel(channel1, consumers);
    setupMockConsume(channel2, consumers, consumerTag, latch2);
    setUpMockCancel(channel2, consumers);

    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames("foo");
    container.setReceiveTimeout(1);
    container.setMessageListener((MessageListener) message -> {
    });// w w w.  j ava  2  s  .  c o m
    container.afterPropertiesSet();
    container.start();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    container.addQueueNames("bar");
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    container.stop();
    verify(channel1).basicCancel("0");
    verify(channel2, atLeastOnce()).basicCancel("1");
    verify(channel2, atLeastOnce()).basicCancel("2");
}

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

@SuppressWarnings("unchecked")
@Test/*from  w ww .  j a va2 s .c o  m*/
public void testConsumerCancel() throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    Connection connection = mock(Connection.class);
    Channel channel = mock(Channel.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    when(connection.createChannel(false)).thenReturn(channel);
    final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>();
    doAnswer(invocation -> {
        consumer.set((Consumer) invocation.getArguments()[6]);
        consumer.get().handleConsumeOk("foo");
        return "foo";
    }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            any(Consumer.class));

    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames("foo");
    container.setMessageListener((MessageListener) message -> {
    });
    container.setReceiveTimeout(1);
    container.afterPropertiesSet();
    container.start();
    verify(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            any(Consumer.class));
    Log logger = spy(TestUtils.getPropertyValue(container, "logger", Log.class));
    doReturn(false).when(logger).isDebugEnabled();
    final CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocation -> {
        if (((String) invocation.getArguments()[0]).startsWith("Consumer raised exception")) {
            latch.countDown();
        }
        return invocation.callRealMethod();
    }).when(logger).warn(any());
    new DirectFieldAccessor(container).setPropertyValue("logger", logger);
    consumer.get().handleCancel("foo");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    container.stop();
}