use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method errorMessageSendingRecovererTests.
@Test
public void errorMessageSendingRecovererTests() {
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
throw new RuntimeException("fooException");
}
};
QueueChannel errors = new QueueChannel();
RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
ErrorMessageSendingRecoverer recoverer = new ErrorMessageSendingRecoverer(errors);
advice.setRecoveryCallback(recoverer);
List<Advice> adviceChain = new ArrayList<Advice>();
adviceChain.add(advice);
handler.setAdviceChain(adviceChain);
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
Message<String> message = new GenericMessage<String>("Hello, world!");
handler.handleMessage(message);
Message<?> error = errors.receive(10000);
assertNotNull(error);
assertEquals("fooException", ((Exception) error.getPayload()).getCause().getMessage());
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method testINT2858RetryAdviceAsFirstInAdviceChain.
@Test
public void testINT2858RetryAdviceAsFirstInAdviceChain() {
final AtomicInteger counter = new AtomicInteger(3);
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return "foo";
}
};
List<Advice> adviceChain = new ArrayList<Advice>();
adviceChain.add(new RequestHandlerRetryAdvice());
adviceChain.add((MethodInterceptor) invocation -> {
counter.getAndDecrement();
throw new RuntimeException("intentional");
});
handler.setBeanFactory(mock(BeanFactory.class));
handler.setAdviceChain(adviceChain);
handler.afterPropertiesSet();
try {
handler.handleMessage(new GenericMessage<String>("test"));
} catch (Exception e) {
Throwable cause = e.getCause();
assertEquals(RuntimeException.class, cause.getClass());
assertEquals("intentional", cause.getMessage());
}
assertTrue(counter.get() == 0);
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method testINT2858RetryAdviceAsNestedInAdviceChain.
@Test
public void testINT2858RetryAdviceAsNestedInAdviceChain() {
final AtomicInteger counter = new AtomicInteger(0);
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return "foo";
}
};
QueueChannel replies = new QueueChannel();
handler.setOutputChannel(replies);
List<Advice> adviceChain = new ArrayList<Advice>();
ExpressionEvaluatingRequestHandlerAdvice expressionAdvice = new ExpressionEvaluatingRequestHandlerAdvice();
expressionAdvice.setBeanFactory(mock(BeanFactory.class));
// MessagingException / RuntimeException
expressionAdvice.setOnFailureExpressionString("#exception.cause.message");
expressionAdvice.setReturnFailureExpressionResult(true);
final AtomicInteger outerCounter = new AtomicInteger();
adviceChain.add(new AbstractRequestHandlerAdvice() {
@Override
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
outerCounter.incrementAndGet();
return callback.execute();
}
});
adviceChain.add(expressionAdvice);
adviceChain.add(new RequestHandlerRetryAdvice());
adviceChain.add((MethodInterceptor) invocation -> {
throw new RuntimeException("intentional: " + counter.incrementAndGet());
});
handler.setAdviceChain(adviceChain);
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
handler.handleMessage(new GenericMessage<String>("test"));
Message<?> receive = replies.receive(10000);
assertNotNull(receive);
assertEquals("intentional: 3", receive.getPayload());
assertEquals(1, outerCounter.get());
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method propagateOnFailureExpressionFailures.
@Test
public void propagateOnFailureExpressionFailures() {
final AtomicBoolean doFail = new AtomicBoolean(true);
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 failure
try {
handler.handleMessage(message);
fail("Expected exception");
} catch (Exception e) {
assertEquals("qux", e.getCause().getMessage());
}
Message<?> reply = replies.receive(1);
assertNull(reply);
Message<?> failure = failureChannel.receive(10000);
assertNotNull(failure);
assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass());
assertEquals("qux", ((Exception) failure.getPayload()).getCause().getMessage());
// propagate failing advice with failure; expect original exception
advice.setPropagateEvaluationFailures(true);
try {
handler.handleMessage(message);
fail("Expected Exception");
} catch (MessageHandlingException e) {
assertEquals("qux", e.getCause().getMessage());
}
reply = replies.receive(1);
assertNull(reply);
failure = failureChannel.receive(10000);
assertNotNull(failure);
assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass());
assertEquals("qux", ((Exception) failure.getPayload()).getCause().getMessage());
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class NestedGatewayTests method nestedWithinHandler.
@Test
public void nestedWithinHandler() {
DirectChannel innerChannel = new DirectChannel();
DirectChannel outerChannel = new DirectChannel();
innerChannel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return requestMessage.getPayload() + "-reply";
}
});
final MessagingGatewaySupport innerGateway = new MessagingGatewaySupport() {
};
innerGateway.setRequestChannel(innerChannel);
innerGateway.setBeanFactory(mock(BeanFactory.class));
innerGateway.afterPropertiesSet();
outerChannel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return innerGateway.sendAndReceiveMessage("pre-" + requestMessage.getPayload()).getPayload() + "-post";
}
});
MessagingGatewaySupport outerGateway = new MessagingGatewaySupport() {
};
outerGateway.setRequestChannel(outerChannel);
outerGateway.setBeanFactory(mock(BeanFactory.class));
outerGateway.afterPropertiesSet();
Message<?> reply = outerGateway.sendAndReceiveMessage("test");
assertEquals("pre-test-reply-post", reply.getPayload());
}
Aggregations