use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.
the class HeaderAnnotationTransformerTests method headerAnnotationWithUnprefixedHeaderAndRelativeExpression.
@Test
public void headerAnnotationWithUnprefixedHeaderAndRelativeExpression() {
Object target = new TestTransformer();
MethodInvokingTransformer transformer = new MethodInvokingTransformer(target, "evalFoo");
MessageTransformingHandler handler = new MessageTransformingHandler(transformer);
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
QueueChannel outputChannel = new QueueChannel();
handler.setOutputChannel(outputChannel);
handler.handleMessage(MessageBuilder.withPayload("test").setHeader("foo", "bar").build());
Message<?> result = outputChannel.receive(0);
assertNotNull(result);
assertEquals("BAR", result.getPayload());
assertEquals("bar", result.getHeaders().get("foo"));
}
use of org.springframework.integration.channel.QueueChannel 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.integration.channel.QueueChannel 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.channel.QueueChannel in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method testInappropriateAdvice.
@Test
public void testInappropriateAdvice() throws Exception {
final AtomicBoolean called = new AtomicBoolean(false);
Advice advice = new AbstractRequestHandlerAdvice() {
@Override
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
called.set(true);
return callback.execute();
}
};
PollableChannel inputChannel = new QueueChannel();
PollingConsumer consumer = new PollingConsumer(inputChannel, message -> {
});
consumer.setAdviceChain(Collections.singletonList(advice));
ExecutorService exec = Executors.newSingleThreadExecutor();
consumer.setTaskExecutor(new ErrorHandlingTaskExecutor(exec, t -> {
}));
consumer.setBeanFactory(mock(BeanFactory.class));
consumer.afterPropertiesSet();
consumer.setTaskScheduler(mock(TaskScheduler.class));
consumer.start();
Callable<?> pollingTask = TestUtils.getPropertyValue(consumer, "poller.pollingTask", Callable.class);
assertTrue(AopUtils.isAopProxy(pollingTask));
Log logger = TestUtils.getPropertyValue(advice, "logger", Log.class);
logger = spy(logger);
when(logger.isWarnEnabled()).thenReturn(Boolean.TRUE);
final AtomicReference<String> logMessage = new AtomicReference<String>();
doAnswer(invocation -> {
logMessage.set(invocation.getArgument(0));
return null;
}).when(logger).warn(Mockito.anyString());
DirectFieldAccessor accessor = new DirectFieldAccessor(advice);
accessor.setPropertyValue("logger", logger);
pollingTask.call();
assertFalse(called.get());
assertNotNull(logMessage.get());
assertThat(logMessage.get(), Matchers.containsString("can only be used for MessageHandlers; " + "an attempt to advise method 'call' in " + "'org.springframework.integration.endpoint.AbstractPollingEndpoint"));
consumer.stop();
exec.shutdownNow();
}
use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method testInt2943RetryWithExceptionClassifier.
private void testInt2943RetryWithExceptionClassifier(boolean retryForMyException, int expected) {
final AtomicInteger counter = new AtomicInteger(0);
@SuppressWarnings("serial")
class MyException extends RuntimeException {
}
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
counter.incrementAndGet();
throw new MyException();
}
};
QueueChannel replies = new QueueChannel();
handler.setOutputChannel(replies);
RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
RetryTemplate retryTemplate = new RetryTemplate();
Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<Class<? extends Throwable>, Boolean>();
retryableExceptions.put(MyException.class, retryForMyException);
retryableExceptions.put(MessagingException.class, true);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3, retryableExceptions));
advice.setRetryTemplate(retryTemplate);
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!");
try {
handler.handleMessage(message);
fail("MessagingException expected.");
} catch (Exception e) {
assertThat(e, Matchers.instanceOf(MessagingException.class));
assertThat(e.getCause(), Matchers.instanceOf(MyException.class));
}
assertEquals(expected, counter.get());
}
Aggregations