Search in sources :

Example 6 with AdviceMessage

use of org.springframework.integration.message.AdviceMessage in project spring-integration by spring-projects.

the class MessageHistory method write.

@SuppressWarnings("unchecked")
public static <T> Message<T> write(Message<T> message, NamedComponent component, MessageBuilderFactory messageBuilderFactory) {
    Assert.notNull(message, "Message must not be null");
    Assert.notNull(component, "Component must not be null");
    Properties metadata = extractMetadata(component);
    if (!metadata.isEmpty()) {
        MessageHistory previousHistory = message.getHeaders().get(HEADER_NAME, MessageHistory.class);
        List<Properties> components = (previousHistory != null) ? new ArrayList<Properties>(previousHistory) : new ArrayList<Properties>();
        components.add(metadata);
        MessageHistory history = new MessageHistory(components);
        if (message instanceof MutableMessage) {
            message.getHeaders().put(HEADER_NAME, history);
        } else if (message instanceof ErrorMessage) {
            IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
            headerAccessor.setHeader(HEADER_NAME, history);
            Throwable payload = ((ErrorMessage) message).getPayload();
            ErrorMessage errorMessage = new ErrorMessage(payload, headerAccessor.toMessageHeaders());
            message = (Message<T>) errorMessage;
        } else if (message instanceof AdviceMessage) {
            IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
            headerAccessor.setHeader(HEADER_NAME, history);
            message = new AdviceMessage<T>(message.getPayload(), headerAccessor.toMessageHeaders(), ((AdviceMessage<?>) message).getInputMessage());
        } else {
            if (!(message instanceof GenericMessage) && (messageBuilderFactory instanceof DefaultMessageBuilderFactory || messageBuilderFactory instanceof MutableMessageBuilderFactory)) {
                if (logger.isWarnEnabled()) {
                    logger.warn("MessageHistory rebuilds the message and produces the result of the [" + messageBuilderFactory + "], not an instance of the provided type [" + message.getClass() + "]. Consider to supply a custom MessageBuilderFactory " + "to retain custom messages during MessageHistory tracking.");
                }
            }
            message = messageBuilderFactory.fromMessage(message).setHeader(HEADER_NAME, history).build();
        }
    }
    return message;
}
Also used : AdviceMessage(org.springframework.integration.message.AdviceMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Properties(java.util.Properties) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) MutableMessageBuilderFactory(org.springframework.integration.support.MutableMessageBuilderFactory) DefaultMessageBuilderFactory(org.springframework.integration.support.DefaultMessageBuilderFactory) ErrorMessage(org.springframework.messaging.support.ErrorMessage)

Example 7 with AdviceMessage

use of org.springframework.integration.message.AdviceMessage in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method propagateOnSuccessExpressionFailures.

@Test
public void propagateOnSuccessExpressionFailures() {
    final AtomicBoolean doFail = new AtomicBoolean();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (doFail.get()) {
                throw new RuntimeException("qux");
            }
            return "baz";
        }
    };
    QueueChannel replies = new QueueChannel();
    handler.setOutputChannel(replies);
    Message<String> message = new GenericMessage<String>("Hello, world!");
    PollableChannel successChannel = new QueueChannel();
    PollableChannel failureChannel = new QueueChannel();
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setBeanFactory(mock(BeanFactory.class));
    advice.setSuccessChannel(successChannel);
    advice.setFailureChannel(failureChannel);
    advice.setOnSuccessExpressionString("1/0");
    advice.setOnFailureExpressionString("1/0");
    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    // failing advice with success
    handler.handleMessage(message);
    Message<?> reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("baz", reply.getPayload());
    Message<?> success = successChannel.receive(1000);
    assertNotNull(success);
    assertEquals("Hello, world!", ((AdviceMessage<?>) success).getInputMessage().getPayload());
    assertEquals(ArithmeticException.class, success.getPayload().getClass());
    assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());
    // propagate failing advice with success
    advice.setPropagateEvaluationFailures(true);
    try {
        handler.handleMessage(message);
        fail("Expected Exception");
    } catch (MessageHandlingException e) {
        assertEquals("/ by zero", e.getCause().getMessage());
    }
    reply = replies.receive(1);
    assertNull(reply);
    success = successChannel.receive(1000);
    assertNotNull(success);
    assertEquals("Hello, world!", ((AdviceMessage<?>) success).getInputMessage().getPayload());
    assertEquals(ArithmeticException.class, success.getPayload().getClass());
    assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) AdviceMessage(org.springframework.integration.message.AdviceMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GenericMessage(org.springframework.messaging.support.GenericMessage) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Aggregations

AdviceMessage (org.springframework.integration.message.AdviceMessage)7 Test (org.junit.Test)6 GenericMessage (org.springframework.messaging.support.GenericMessage)6 Message (org.springframework.messaging.Message)4 ErrorMessage (org.springframework.messaging.support.ErrorMessage)4 ArrayList (java.util.ArrayList)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Advice (org.aopalliance.aop.Advice)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)2 MongoDbAvailable (org.springframework.integration.mongodb.rules.MongoDbAvailable)2 MessageStore (org.springframework.integration.store.MessageStore)2 MutableMessage (org.springframework.integration.support.MutableMessage)2 MessageHandlingException (org.springframework.messaging.MessageHandlingException)2 PollableChannel (org.springframework.messaging.PollableChannel)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Date (java.util.Date)1 Properties (java.util.Properties)1