use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class NestedGatewayTests method errorChannelRetained.
@Test
public void errorChannelRetained() {
DirectChannel requestChannel = new DirectChannel();
DirectChannel errorChannel = new DirectChannel();
requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return requestMessage.getPayload() + "-reply";
}
});
MessagingGatewaySupport gateway = new MessagingGatewaySupport() {
};
gateway.setRequestChannel(requestChannel);
gateway.setBeanFactory(mock(BeanFactory.class));
gateway.afterPropertiesSet();
Message<?> message = MessageBuilder.withPayload("test").setErrorChannel(errorChannel).build();
Message<?> reply = gateway.sendAndReceiveMessage(message);
assertEquals("test-reply", reply.getPayload());
assertEquals(errorChannel, reply.getHeaders().getErrorChannel());
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method defaultStatefulRetrySucceedOnThirdTry.
@Test
public void defaultStatefulRetrySucceedOnThirdTry() {
final AtomicInteger counter = new AtomicInteger(2);
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
if (counter.getAndDecrement() > 0) {
throw new RuntimeException("foo");
}
return "bar";
}
};
QueueChannel replies = new QueueChannel();
handler.setOutputChannel(replies);
RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
advice.setRetryStateGenerator(message -> new DefaultRetryState(message.getHeaders().getId()));
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!");
for (int i = 0; i < 3; i++) {
try {
handler.handleMessage(message);
} catch (Exception e) {
assertTrue(i < 2);
}
}
assertTrue(counter.get() == -1);
Message<?> reply = replies.receive(10000);
assertNotNull(reply);
assertEquals("bar", reply.getPayload());
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method testINT2858ExpressionAdviceWithSendFailureOnEachRetry.
@Test
public void testINT2858ExpressionAdviceWithSendFailureOnEachRetry() {
final AtomicInteger counter = new AtomicInteger(0);
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return "foo";
}
};
QueueChannel errors = new QueueChannel();
List<Advice> adviceChain = new ArrayList<Advice>();
ExpressionEvaluatingRequestHandlerAdvice expressionAdvice = new ExpressionEvaluatingRequestHandlerAdvice();
expressionAdvice.setBeanFactory(mock(BeanFactory.class));
expressionAdvice.setOnFailureExpressionString("#exception.message");
expressionAdvice.setFailureChannel(errors);
adviceChain.add(new RequestHandlerRetryAdvice());
adviceChain.add(expressionAdvice);
adviceChain.add((MethodInterceptor) invocation -> {
throw new RuntimeException("intentional: " + counter.incrementAndGet());
});
handler.setAdviceChain(adviceChain);
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
try {
handler.handleMessage(new GenericMessage<String>("test"));
} catch (Exception e) {
assertEquals("intentional: 3", e.getCause().getMessage());
}
for (int i = 1; i <= 3; i++) {
Message<?> receive = errors.receive(10000);
assertNotNull(receive);
assertEquals("intentional: " + i, ((MessageHandlingExpressionEvaluatingAdviceException) receive.getPayload()).getEvaluationResult());
}
assertNull(errors.receive(1));
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method defaultStatefulRetryRecoverAfterThirdTrySpelState.
@Test
public void defaultStatefulRetryRecoverAfterThirdTrySpelState() {
final AtomicInteger counter = new AtomicInteger(3);
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
if (counter.getAndDecrement() > 0) {
throw new RuntimeException("foo");
}
return "bar";
}
};
QueueChannel replies = new QueueChannel();
handler.setOutputChannel(replies);
RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
advice.setRetryStateGenerator(new SpelExpressionRetryStateGenerator("headers['id']"));
defaultStatefulRetryRecoverAfterThirdTryGuts(counter, handler, replies, advice);
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method defaultStatefulRetryRecoverAfterThirdTry.
@Test
public void defaultStatefulRetryRecoverAfterThirdTry() {
final AtomicInteger counter = new AtomicInteger(3);
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
if (counter.getAndDecrement() > 0) {
throw new RuntimeException("foo");
}
return "bar";
}
};
QueueChannel replies = new QueueChannel();
handler.setOutputChannel(replies);
RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
advice.setRetryStateGenerator(message -> new DefaultRetryState(message.getHeaders().getId()));
defaultStatefulRetryRecoverAfterThirdTryGuts(counter, handler, replies, advice);
}
Aggregations