Search in sources :

Example 1 with AdviceMessage

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

the class AdvisedMessageHandlerTests method successFailureAdvice.

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

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (doFail.get()) {
                throw new RuntimeException("qux");
            }
            return "baz";
        }
    };
    String componentName = "testComponentName";
    handler.setComponentName(componentName);
    QueueChannel replies = new QueueChannel();
    handler.setOutputChannel(replies);
    Message<String> message = new GenericMessage<String>("Hello, world!");
    // no advice
    handler.handleMessage(message);
    Message<?> reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("baz", reply.getPayload());
    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("'foo'");
    advice.setOnFailureExpressionString("'bar:' + #exception.message");
    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    final AtomicReference<String> compName = new AtomicReference<String>();
    adviceChain.add(new AbstractRequestHandlerAdvice() {

        @Override
        protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
            compName.set(((AbstractReplyProducingMessageHandler.RequestHandler) target).getAdvisedHandler().getComponentName());
            return callback.execute();
        }
    });
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    // advice with success
    handler.handleMessage(message);
    reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("baz", reply.getPayload());
    assertEquals(componentName, compName.get());
    Message<?> success = successChannel.receive(1000);
    assertNotNull(success);
    assertEquals("Hello, world!", ((AdviceMessage<?>) success).getInputMessage().getPayload());
    assertEquals("foo", success.getPayload());
    // advice with failure, not trapped
    doFail.set(true);
    try {
        handler.handleMessage(message);
        fail("Expected exception");
    } catch (Exception e) {
        assertEquals("qux", e.getCause().getMessage());
    }
    Message<?> failure = failureChannel.receive(1000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult());
    // advice with failure, trapped
    advice.setTrapException(true);
    handler.handleMessage(message);
    failure = failureChannel.receive(1000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult());
    assertNull(replies.receive(1));
    // advice with failure, eval is result
    advice.setReturnFailureExpressionResult(true);
    handler.handleMessage(message);
    failure = failureChannel.receive(1000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult());
    reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("bar:qux", reply.getPayload());
}
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) MessagingException(org.springframework.messaging.MessagingException) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) AdviceMessage(org.springframework.integration.message.AdviceMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) 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)

Example 2 with AdviceMessage

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

the class MessageHistoryTests method testCorrectAdviceMessageAfterWrite.

@Test
public void testCorrectAdviceMessageAfterWrite() {
    Message<?> inputMessage = new GenericMessage<>("input");
    AdviceMessage<String> original = new AdviceMessage<>("foo", inputMessage);
    assertNull(MessageHistory.read(original));
    Message<String> result1 = MessageHistory.write(original, new TestComponent(1));
    assertThat(result1, instanceOf(AdviceMessage.class));
    assertNotSame(original, result1);
    assertSame(original.getPayload(), result1.getPayload());
    assertSame(original.getInputMessage(), ((AdviceMessage<?>) result1).getInputMessage());
    MessageHistory history1 = MessageHistory.read(result1);
    assertNotNull(history1);
    assertEquals("testComponent-1", history1.toString());
    Message<String> result2 = MessageHistory.write(result1, new TestComponent(2));
    assertThat(result2, instanceOf(AdviceMessage.class));
    assertNotSame(original, result2);
    assertSame(original.getPayload(), result2.getPayload());
    assertSame(original.getInputMessage(), ((AdviceMessage<?>) result2).getInputMessage());
    assertNotSame(result1, result2);
    MessageHistory history2 = MessageHistory.read(result2);
    assertNotNull(history2);
    assertEquals("testComponent-1,testComponent-2", history2.toString());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) AdviceMessage(org.springframework.integration.message.AdviceMessage) Test(org.junit.Test)

Example 3 with AdviceMessage

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

the class RedisMessageGroupStoreTests method testJsonSerialization.

@Test
@RedisAvailable
public void testJsonSerialization() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper();
    GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    Message<?> genericMessage = new GenericMessage<>(new Date());
    Message<?> mutableMessage = new MutableMessage<>(UUID.randomUUID());
    Message<?> adviceMessage = new AdviceMessage<>("foo", genericMessage);
    ErrorMessage errorMessage = new ErrorMessage(new RuntimeException("test exception"), mutableMessage);
    store.addMessagesToGroup(this.groupId, genericMessage, mutableMessage, adviceMessage, errorMessage);
    MessageGroup messageGroup = store.getMessageGroup(this.groupId);
    assertEquals(4, messageGroup.size());
    List<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
    assertEquals(genericMessage, messages.get(0));
    assertEquals(mutableMessage, messages.get(1));
    assertEquals(adviceMessage, messages.get(2));
    Message<?> errorMessageResult = messages.get(3);
    assertEquals(errorMessage.getHeaders(), errorMessageResult.getHeaders());
    assertThat(errorMessageResult, instanceOf(ErrorMessage.class));
    assertEquals(errorMessage.getOriginalMessage(), ((ErrorMessage) errorMessageResult).getOriginalMessage());
    assertEquals(errorMessage.getPayload().getMessage(), ((ErrorMessage) errorMessageResult).getPayload().getMessage());
    Message<Foo> fooMessage = new GenericMessage<>(new Foo("foo"));
    try {
        store.addMessageToGroup(this.groupId, fooMessage).getMessages().iterator().next();
        fail("SerializationException expected");
    } catch (Exception e) {
        assertThat(e.getCause().getCause(), instanceOf(IllegalArgumentException.class));
        assertThat(e.getMessage(), containsString("The class with " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo and name of " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo " + "is not in the trusted packages:"));
    }
    mapper = JacksonJsonUtils.messagingAwareMapper(getClass().getPackage().getName());
    serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    store.removeMessageGroup(this.groupId);
    messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
    assertEquals(1, messageGroup.size());
    assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
    mapper = JacksonJsonUtils.messagingAwareMapper("*");
    serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    store.removeMessageGroup(this.groupId);
    messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
    assertEquals(1, messageGroup.size());
    assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
}
Also used : GenericJackson2JsonRedisSerializer(org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) AdviceMessage(org.springframework.integration.message.AdviceMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) ArrayList(java.util.ArrayList) AdviceMessage(org.springframework.integration.message.AdviceMessage) Date(java.util.Date) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 4 with AdviceMessage

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

the class AbstractMongoDbMessageStoreTests method testAdviceMessageAsPayload.

@Test
@MongoDbAvailable
public void testAdviceMessageAsPayload() throws Exception {
    MessageStore store = this.getMessageStore();
    Person p = new Person();
    p.setFname("John");
    p.setLname("Doe");
    Message<Person> inputMessage = MessageBuilder.withPayload(p).build();
    Message<?> messageToStore = new GenericMessage<Message<?>>(new AdviceMessage<String>("foo", inputMessage));
    store.addMessage(messageToStore);
    Message<?> retrievedMessage = store.getMessage(messageToStore.getHeaders().getId());
    assertNotNull(retrievedMessage);
    assertTrue(retrievedMessage.getPayload() instanceof AdviceMessage);
    AdviceMessage<?> adviceMessage = (AdviceMessage<?>) retrievedMessage.getPayload();
    assertEquals("foo", adviceMessage.getPayload());
    assertEquals(messageToStore.getHeaders(), retrievedMessage.getHeaders());
    assertEquals(inputMessage, adviceMessage.getInputMessage());
    assertEquals(messageToStore, retrievedMessage);
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) AdviceMessage(org.springframework.integration.message.AdviceMessage) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 5 with AdviceMessage

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

the class AbstractMongoDbMessageStoreTests method testInt3076AdviceMessage.

@Test
@MongoDbAvailable
public void testInt3076AdviceMessage() throws Exception {
    MessageStore store = this.getMessageStore();
    Person p = new Person();
    p.setFname("John");
    p.setLname("Doe");
    Message<Person> inputMessage = MessageBuilder.withPayload(p).build();
    Message<?> messageToStore = new AdviceMessage<String>("foo", inputMessage);
    store.addMessage(messageToStore);
    Message<?> retrievedMessage = store.getMessage(messageToStore.getHeaders().getId());
    assertNotNull(retrievedMessage);
    assertTrue(retrievedMessage instanceof AdviceMessage);
    assertEquals(messageToStore.getPayload(), retrievedMessage.getPayload());
    assertEquals(messageToStore.getHeaders(), retrievedMessage.getHeaders());
    assertEquals(inputMessage, ((AdviceMessage<?>) retrievedMessage).getInputMessage());
    assertEquals(messageToStore, retrievedMessage);
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) AdviceMessage(org.springframework.integration.message.AdviceMessage) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

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