use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class AsyncHandlerTests method testMessagingException.
@Test
public void testMessagingException() {
QueueChannel errorChannel = new QueueChannel();
Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
this.handler.handleMessage(message);
assertNull(this.output.receive(0));
this.whichTest = 2;
this.latch.countDown();
Message<?> received = errorChannel.receive(10000);
assertNotNull(received);
assertThat(received.getPayload(), instanceOf(MessagingException.class));
assertSame(message, ((MessagingException) received.getPayload()).getFailedMessage());
assertNull(this.failedCallbackException);
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class AsyncHandlerTests method setup.
@Before
public void setup() {
this.executor = Executors.newSingleThreadExecutor();
this.handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
final SettableListenableFuture<String> future = new SettableListenableFuture<String>();
AsyncHandlerTests.this.executor.execute(() -> {
try {
latch.await(10, TimeUnit.SECONDS);
switch(whichTest) {
case 0:
future.set("reply");
break;
case 1:
future.setException(new RuntimeException("foo"));
break;
case 2:
future.setException(new MessagingException(requestMessage));
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
return future;
}
};
this.handler.setAsync(true);
this.handler.setOutputChannel(this.output);
this.handler.setBeanFactory(mock(BeanFactory.class));
this.latch = new CountDownLatch(1);
Log logger = spy(TestUtils.getPropertyValue(this.handler, "logger", Log.class));
new DirectFieldAccessor(this.handler).setPropertyValue("logger", logger);
doAnswer(invocation -> {
failedCallbackMessage = invocation.getArgument(0);
failedCallbackException = invocation.getArgument(1);
exceptionLatch.countDown();
return null;
}).when(logger).error(anyString(), any(Throwable.class));
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class MethodInvokingMessageProcessorTests method testProxyInvocation.
@Test
public void testProxyInvocation() {
final AtomicReference<Object> result = new AtomicReference<>();
class MyHandler implements MessageHandler {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
result.set(message.getPayload());
}
}
MessageHandler service = new MyHandler();
final AtomicBoolean adviceCalled = new AtomicBoolean();
ProxyFactory proxyFactory = new ProxyFactory(service);
proxyFactory.addAdvice((MethodInterceptor) i -> {
adviceCalled.set(true);
return i.proceed();
});
service = (MessageHandler) proxyFactory.getProxy(getClass().getClassLoader());
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, "handleMessage");
processor.processMessage(new GenericMessage<>("foo"));
assertEquals("foo", result.get());
assertTrue(adviceCalled.get());
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method enhancedRecoverer.
@Test
public void enhancedRecoverer() throws Exception {
QueueChannel channel = new QueueChannel();
ErrorMessageSendingRecoverer recoverer = new ErrorMessageSendingRecoverer(channel);
recoverer.publish(new GenericMessage<>("foo"), new GenericMessage<>("bar"), new RuntimeException("baz"));
Message<?> error = channel.receive(0);
assertThat(error, instanceOf(ErrorMessage.class));
assertThat(error.getPayload(), instanceOf(MessagingException.class));
MessagingException payload = (MessagingException) error.getPayload();
assertThat(payload.getCause(), instanceOf(RuntimeException.class));
assertThat(payload.getCause().getMessage(), equalTo("baz"));
assertThat(payload.getFailedMessage().getPayload(), equalTo("bar"));
assertThat(((ErrorMessage) error).getOriginalMessage().getPayload(), equalTo("foo"));
}
use of org.springframework.messaging.MessagingException 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());
}
Aggregations