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());
}
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());
}
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());
}
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);
}
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);
}
Aggregations