use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class AbstractMessageProducingHandler method sendErrorMessage.
protected void sendErrorMessage(final Message<?> requestMessage, Throwable ex) {
Object errorChannel = resolveErrorChannel(requestMessage.getHeaders());
Throwable result = ex;
if (!(ex instanceof MessagingException)) {
result = new MessageHandlingException(requestMessage, ex);
}
if (errorChannel == null) {
logger.error("Async exception received and no 'errorChannel' header exists and no default " + "'errorChannel' found", result);
} else {
try {
sendOutput(new ErrorMessage(result), errorChannel, true);
} catch (Exception e) {
Exception exceptionToLog = e;
if (!(e instanceof MessagingException)) {
exceptionToLog = new MessageHandlingException(requestMessage, e);
}
logger.error("Failed to send async reply", exceptionToLog);
}
}
}
use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class MessageProducerSupportTests method validateSuccessfulErrorFlowDoesNotThrowErrors.
@Test
public void validateSuccessfulErrorFlowDoesNotThrowErrors() {
TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
testApplicationContext.refresh();
DirectChannel outChannel = new DirectChannel();
outChannel.subscribe(message -> {
throw new RuntimeException("problems");
});
PublishSubscribeChannel errorChannel = new PublishSubscribeChannel();
SuccessfulErrorService errorService = new SuccessfulErrorService();
ServiceActivatingHandler handler = new ServiceActivatingHandler(errorService);
handler.setBeanFactory(testApplicationContext);
handler.afterPropertiesSet();
errorChannel.subscribe(handler);
MessageProducerSupport mps = new MessageProducerSupport() {
};
mps.setOutputChannel(outChannel);
mps.setErrorChannel(errorChannel);
mps.setBeanFactory(testApplicationContext);
mps.afterPropertiesSet();
mps.start();
Message<?> message = new GenericMessage<String>("hello");
mps.sendMessage(message);
assertThat(errorService.lastMessage, instanceOf(ErrorMessage.class));
ErrorMessage errorMessage = (ErrorMessage) errorService.lastMessage;
assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
MessageDeliveryException exception = (MessageDeliveryException) errorMessage.getPayload();
assertEquals(message, exception.getFailedMessage());
testApplicationContext.close();
}
use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method throwableProperlyPropagatedAndReported.
/**
* Verify that Errors such as OOM are properly propagated and we suppress the
* ThrowableHolderException from the output message.
*/
@Test
public void throwableProperlyPropagatedAndReported() throws Exception {
QueueChannel errors = new QueueChannel();
ExpressionEvaluatingRequestHandlerAdvice expressionAdvice = new ExpressionEvaluatingRequestHandlerAdvice();
expressionAdvice.setBeanFactory(mock(BeanFactory.class));
expressionAdvice.setOnFailureExpressionString("'foo'");
expressionAdvice.setFailureChannel(errors);
Throwable theThrowable = new Throwable("foo");
ProxyFactory proxyFactory = new ProxyFactory(new Foo(theThrowable));
proxyFactory.addAdvice(expressionAdvice);
Bar fooHandler = (Bar) proxyFactory.getProxy();
try {
fooHandler.handleRequestMessage(new GenericMessage<String>("foo"));
fail("Expected throwable");
} catch (Throwable t) {
assertSame(theThrowable, t);
ErrorMessage error = (ErrorMessage) errors.receive(10000);
assertNotNull(error);
assertSame(theThrowable, error.getPayload().getCause());
}
}
use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class PayloadTypeSelectorTests method testSubclassOfAcceptedTypeIsSelected.
@Test
public void testSubclassOfAcceptedTypeIsSelected() {
PayloadTypeSelector selector = new PayloadTypeSelector(RuntimeException.class);
assertTrue(selector.accept(new ErrorMessage(new MessagingException("test"))));
}
use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.
the class PayloadTypeSelectorTests method testMultipleAcceptedTypes.
@Test
public void testMultipleAcceptedTypes() {
PayloadTypeSelector selector = new PayloadTypeSelector(String.class, Integer.class);
assertTrue(selector.accept(new GenericMessage<String>("test1")));
assertTrue(selector.accept(new GenericMessage<Integer>(2)));
assertFalse(selector.accept(new ErrorMessage(new RuntimeException())));
}
Aggregations