Example usage for org.springframework.amqp.rabbit.listener.exception FatalListenerExecutionException FatalListenerExecutionException

List of usage examples for org.springframework.amqp.rabbit.listener.exception FatalListenerExecutionException FatalListenerExecutionException

Introduction

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

Prototype

public FatalListenerExecutionException(String msg) 

Source Link

Document

Constructor for ListenerExecutionFailedException.

Usage

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.//www .j a  v a 2s .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;
    }
}

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

/**
 * Invoke the specified listener: either as standard MessageListener or (preferably) as SessionAwareMessageListener.
 * @param channel the Rabbit Channel to operate on
 * @param message the received Rabbit Message
 * @throws Exception if thrown by Rabbit API methods
 * @see #setMessageListener//from  www .ja v  a  2  s  .  co m
 */
protected void actualInvokeListener(Channel channel, Message message) throws Exception {
    Object listener = getMessageListener();
    if (listener instanceof ChannelAwareMessageListener) {
        doInvokeListener((ChannelAwareMessageListener) listener, channel, message);
    } else if (listener instanceof MessageListener) {
        boolean bindChannel = isExposeListenerChannel() && isChannelLocallyTransacted();
        if (bindChannel) {
            RabbitResourceHolder resourceHolder = new RabbitResourceHolder(channel, false);
            resourceHolder.setSynchronizedWithTransaction(true);
            TransactionSynchronizationManager.bindResource(this.getConnectionFactory(), resourceHolder);
        }
        try {
            doInvokeListener((MessageListener) listener, message);
        } finally {
            if (bindChannel) {
                // unbind if we bound
                TransactionSynchronizationManager.unbindResource(this.getConnectionFactory());
            }
        }
    } else if (listener != null) {
        throw new FatalListenerExecutionException(
                "Only MessageListener and SessionAwareMessageListener supported: " + listener);
    } else {
        throw new FatalListenerExecutionException(
                "No message listener specified - see property 'messageListener'");
    }
}