Example usage for org.springframework.amqp.rabbit.config StatefulRetryOperationsInterceptorFactoryBean setRetryOperations

List of usage examples for org.springframework.amqp.rabbit.config StatefulRetryOperationsInterceptorFactoryBean setRetryOperations

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.config StatefulRetryOperationsInterceptorFactoryBean setRetryOperations.

Prototype

public void setRetryOperations(RetryOperations retryTemplate) 

Source Link

Usage

From source file:org.springframework.amqp.rabbit.retry.MissingIdRetryTests.java

@SuppressWarnings("rawtypes")
@Test//from ww w .  jav a2 s.  c  o  m
public void testWithNoId() throws Exception {
    // 2 messages; each retried once by missing id interceptor
    this.latch = new CountDownLatch(4);
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("retry-context.xml",
            this.getClass());
    RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
    ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setStatefulRetryFatalWithNullMessageId(false);
    container.setMessageListener(new MessageListenerAdapter(new POJO()));
    container.setQueueNames("retry.test.queue");

    StatefulRetryOperationsInterceptorFactoryBean fb = new StatefulRetryOperationsInterceptorFactoryBean();

    // use an external template so we can share his cache
    RetryTemplate retryTemplate = new RetryTemplate();
    RetryContextCache cache = spy(new MapRetryContextCache());
    retryTemplate.setRetryContextCache(cache);
    fb.setRetryOperations(retryTemplate);

    Advice retryInterceptor = fb.getObject();
    container.setAdviceChain(retryInterceptor);
    container.start();

    template.convertAndSend("retry.test.exchange", "retry.test.binding", "Hello, world!");
    template.convertAndSend("retry.test.exchange", "retry.test.binding", "Hello, world!");
    try {
        assertTrue(latch.await(30, TimeUnit.SECONDS));
        Map map = (Map) new DirectFieldAccessor(cache).getPropertyValue("map");
        int n = 0;
        while (n++ < 100 && map.size() != 0) {
            Thread.sleep(100);
        }
        verify(cache, never()).put(any(), any(RetryContext.class));
        verify(cache, never()).remove(any());
        assertEquals("Expected map.size() = 0, was: " + map.size(), 0, map.size());
    } finally {
        container.stop();
        ctx.close();
    }
}

From source file:org.springframework.amqp.rabbit.retry.MissingIdRetryTests.java

@SuppressWarnings("rawtypes")
@Test//from  www .j a v a  2 s.  co  m
public void testWithId() throws Exception {
    // 2 messages; each retried twice by retry interceptor
    this.latch = new CountDownLatch(6);
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("retry-context.xml",
            this.getClass());
    RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
    ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener(new MessageListenerAdapter(new POJO()));
    container.setQueueNames("retry.test.queue");

    StatefulRetryOperationsInterceptorFactoryBean fb = new StatefulRetryOperationsInterceptorFactoryBean();

    // use an external template so we can share his cache
    RetryTemplate retryTemplate = new RetryTemplate();
    RetryContextCache cache = spy(new MapRetryContextCache());
    retryTemplate.setRetryContextCache(cache);
    fb.setRetryOperations(retryTemplate);
    fb.setMessageRecoverer(new RejectAndDontRequeueRecoverer());

    Advice retryInterceptor = fb.getObject();
    container.setAdviceChain(retryInterceptor);
    container.start();

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("text/plain");
    messageProperties.setMessageId("foo");
    Message message = new Message("Hello, world!".getBytes(), messageProperties);
    template.send("retry.test.exchange", "retry.test.binding", message);
    template.send("retry.test.exchange", "retry.test.binding", message);
    try {
        assertTrue(latch.await(30, TimeUnit.SECONDS));
        Map map = (Map) new DirectFieldAccessor(cache).getPropertyValue("map");
        int n = 0;
        while (n++ < 100 && map.size() != 0) {
            Thread.sleep(100);
        }
        ArgumentCaptor putCaptor = ArgumentCaptor.forClass(Object.class);
        ArgumentCaptor getCaptor = ArgumentCaptor.forClass(Object.class);
        ArgumentCaptor removeCaptor = ArgumentCaptor.forClass(Object.class);
        verify(cache, times(6)).put(putCaptor.capture(), any(RetryContext.class));
        verify(cache, times(6)).get(getCaptor.capture());
        verify(cache, atLeast(2)).remove(removeCaptor.capture());
        verify(cache, atMost(4)).remove(removeCaptor.capture());
        logger.debug("puts:" + putCaptor.getAllValues());
        logger.debug("gets:" + putCaptor.getAllValues());
        logger.debug("removes:" + removeCaptor.getAllValues());
        assertEquals("Expected map.size() = 0, was: " + map.size(), 0, map.size());
    } finally {
        container.stop();
        ctx.close();
    }
}

From source file:org.springframework.amqp.rabbit.retry.MissingIdRetryTests.java

@SuppressWarnings("rawtypes")
@Test//from ww  w . j ava 2 s. com
public void testWithIdAndSuccess() throws Exception {
    // 2 messages; each retried twice by retry interceptor
    this.latch = new CountDownLatch(6);
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("retry-context.xml",
            this.getClass());
    RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
    ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    final Set<String> processed = new HashSet<>();
    final CountDownLatch latch = new CountDownLatch(4);
    container.setMessageListener(m -> {
        latch.countDown();
        if (!processed.contains(m.getMessageProperties().getMessageId())) {
            processed.add(m.getMessageProperties().getMessageId());
            throw new RuntimeException("fail");
        }
    });
    container.setQueueNames("retry.test.queue");

    StatefulRetryOperationsInterceptorFactoryBean fb = new StatefulRetryOperationsInterceptorFactoryBean();

    // use an external template so we can share his cache
    RetryTemplate retryTemplate = new RetryTemplate();
    RetryContextCache cache = spy(new MapRetryContextCache());
    retryTemplate.setRetryContextCache(cache);
    fb.setRetryOperations(retryTemplate);
    fb.setMessageRecoverer(new RejectAndDontRequeueRecoverer());

    Advice retryInterceptor = fb.getObject();
    container.setAdviceChain(retryInterceptor);
    container.start();

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("text/plain");
    messageProperties.setMessageId("foo");
    Message message = new Message("Hello, world!".getBytes(), messageProperties);
    template.send("retry.test.exchange", "retry.test.binding", message);
    messageProperties.setMessageId("bar");
    template.send("retry.test.exchange", "retry.test.binding", message);
    try {
        assertTrue(latch.await(30, TimeUnit.SECONDS));
        Map map = (Map) new DirectFieldAccessor(cache).getPropertyValue("map");
        int n = 0;
        while (n++ < 100 && map.size() != 0) {
            Thread.sleep(100);
        }
        ArgumentCaptor putCaptor = ArgumentCaptor.forClass(Object.class);
        ArgumentCaptor getCaptor = ArgumentCaptor.forClass(Object.class);
        ArgumentCaptor removeCaptor = ArgumentCaptor.forClass(Object.class);
        verify(cache, times(2)).put(putCaptor.capture(), any(RetryContext.class));
        verify(cache, times(2)).get(getCaptor.capture());
        verify(cache, atLeast(2)).remove(removeCaptor.capture());
        verify(cache, atMost(4)).remove(removeCaptor.capture());
        logger.debug("puts:" + putCaptor.getAllValues());
        logger.debug("gets:" + putCaptor.getAllValues());
        logger.debug("removes:" + removeCaptor.getAllValues());
        assertEquals("Expected map.size() = 0, was: " + map.size(), 0, map.size());
    } finally {
        container.stop();
        ctx.close();
    }
}