Example usage for org.springframework.amqp.core MessageProperties SPRING_BATCH_FORMAT

List of usage examples for org.springframework.amqp.core MessageProperties SPRING_BATCH_FORMAT

Introduction

In this page you can find the example usage for org.springframework.amqp.core MessageProperties SPRING_BATCH_FORMAT.

Prototype

String SPRING_BATCH_FORMAT

To view the source code for org.springframework.amqp.core MessageProperties SPRING_BATCH_FORMAT.

Click Source Link

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  ww.j  av  a 2s  .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.listener.AbstractMessageListenerContainer.java

/**
 * Execute the specified listener, committing or rolling back the transaction afterwards (if necessary).
 *
 * @param channel the Rabbit Channel to operate on
 * @param messageIn the received Rabbit Message
 * @throws Exception Any Exception.//from   w ww. j a v a  2  s  .  c o m
 *
 * @see #invokeListener
 * @see #handleListenerException
 */
protected void executeListener(Channel channel, Message messageIn) throws Exception {
    if (!isRunning()) {
        if (logger.isWarnEnabled()) {
            logger.warn(
                    "Rejecting received message because the listener container has been stopped: " + messageIn);
        }
        throw new MessageRejectedWhileStoppingException();
    }
    try {
        Message message = messageIn;
        if (this.afterReceivePostProcessors != null) {
            for (MessagePostProcessor processor : this.afterReceivePostProcessors) {
                message = processor.postProcessMessage(message);
            }
        }
        Object batchFormat = message.getMessageProperties().getHeaders()
                .get(MessageProperties.SPRING_BATCH_FORMAT);
        if (MessageProperties.BATCH_FORMAT_LENGTH_HEADER4.equals(batchFormat) && this.deBatchingEnabled) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(message.getBody());
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.getHeaders().remove(MessageProperties.SPRING_BATCH_FORMAT);
            while (byteBuffer.hasRemaining()) {
                int length = byteBuffer.getInt();
                if (length < 0 || length > byteBuffer.remaining()) {
                    throw new ListenerExecutionFailedException("Bad batched message received",
                            new MessageConversionException(
                                    "Insufficient batch data at offset " + byteBuffer.position()),
                            message);
                }
                byte[] body = new byte[length];
                byteBuffer.get(body);
                messageProperties.setContentLength(length);
                // Caveat - shared MessageProperties.
                Message fragment = new Message(body, messageProperties);
                invokeListener(channel, fragment);
            }
        } else {
            invokeListener(channel, message);
        }
    } catch (Exception ex) {
        if (messageIn.getMessageProperties().isFinalRetryForMessageWithNoId()) {
            if (this.statefulRetryFatalWithNullMessageId) {
                throw new FatalListenerExecutionException(
                        "Illegal null id in message. Failed to manage retry for message: " + messageIn);
            } else {
                throw new ListenerExecutionFailedException("Cannot retry message more than once without an ID",
                        new AmqpRejectAndDontRequeueException("Not retryable; rejecting and not requeuing", ex),
                        messageIn);
            }
        }
        handleListenerException(ex);
        throw ex;
    }
}