Example usage for org.springframework.amqp.rabbit.listener ConditionalRejectingErrorHandler ConditionalRejectingErrorHandler

List of usage examples for org.springframework.amqp.rabbit.listener ConditionalRejectingErrorHandler ConditionalRejectingErrorHandler

Introduction

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

Prototype

public ConditionalRejectingErrorHandler() 

Source Link

Document

Create a handler with the ConditionalRejectingErrorHandler.DefaultExceptionStrategy .

Usage

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  w w w  . jav  a  2  s  .co  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.listener.ErrorHandlerTests.java

@Test
public void testFatalsAreRejected() throws Exception {
    ConditionalRejectingErrorHandler handler = new ConditionalRejectingErrorHandler();
    Log logger = spy(TestUtils.getPropertyValue(handler, "logger", Log.class));
    willDoNothing().given(logger).warn(anyString(), any(Throwable.class));
    new DirectFieldAccessor(handler).setPropertyValue("logger", logger);
    handler.handleError(new ListenerExecutionFailedException("intended", new RuntimeException()));

    try {//from w w w.j a  va 2  s  .  com
        handler.handleError(
                new ListenerExecutionFailedException("intended", new MessageConversionException("")));
        fail("Expected exception");
    } catch (AmqpRejectAndDontRequeueException e) {
    }

    try {
        handler.handleError(new ListenerExecutionFailedException("intended",
                new org.springframework.messaging.converter.MessageConversionException("")));
        fail("Expected exception");
    } catch (AmqpRejectAndDontRequeueException e) {
    }

    Message<?> message = mock(Message.class);
    MethodParameter mp = new MethodParameter(Foo.class.getMethod("foo", String.class), 0);
    try {
        handler.handleError(new ListenerExecutionFailedException("intended",
                new MethodArgumentNotValidException(message, mp)));
        fail("Expected exception");
    } catch (AmqpRejectAndDontRequeueException e) {
    }

    try {
        handler.handleError(new ListenerExecutionFailedException("intended",
                new MethodArgumentTypeMismatchException(message, mp, "")));
        fail("Expected exception");
    } catch (AmqpRejectAndDontRequeueException e) {
    }
}