Search in sources :

Example 26 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method errorMessageSendingRecovererTestsNoThrowable.

@Test
public void errorMessageSendingRecovererTestsNoThrowable() {
    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);
    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setRetryPolicy(new SimpleRetryPolicy() {

        static final long serialVersionUID = -1;

        @Override
        public boolean canRetry(RetryContext context) {
            return false;
        }
    });
    advice.setRetryTemplate(retryTemplate);
    advice.setBeanFactory(mock(BeanFactory.class));
    advice.afterPropertiesSet();
    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);
    assertTrue(error.getPayload() instanceof ErrorMessageSendingRecoverer.RetryExceptionNotAvailableException);
    assertNotNull(((MessagingException) error.getPayload()).getFailedMessage());
    assertSame(message, ((MessagingException) error.getPayload()).getFailedMessage());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) RetryContext(org.springframework.retry.RetryContext) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) GenericMessage(org.springframework.messaging.support.GenericMessage) RetryTemplate(org.springframework.retry.support.RetryTemplate) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 27 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method filterDiscardNoAdvice.

public void filterDiscardNoAdvice() {
    MessageFilter filter = new MessageFilter(message -> false);
    QueueChannel discardChannel = new QueueChannel();
    filter.setDiscardChannel(discardChannel);
    filter.handleMessage(new GenericMessage<String>("foo"));
    assertNotNull(discardChannel.receive(0));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageFilter(org.springframework.integration.filter.MessageFilter)

Example 28 with QueueChannel

use of org.springframework.integration.channel.QueueChannel 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());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 29 with QueueChannel

use of org.springframework.integration.channel.QueueChannel 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());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) DefaultRetryState(org.springframework.retry.support.DefaultRetryState) AopUtils(org.springframework.aop.support.AopUtils) Autowired(org.springframework.beans.factory.annotation.Autowired) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) MethodInvocation(org.aopalliance.intercept.MethodInvocation) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) Method(java.lang.reflect.Method) AdviceMessage(org.springframework.integration.message.AdviceMessage) TaskScheduler(org.springframework.scheduling.TaskScheduler) MessageChannel(org.springframework.messaging.MessageChannel) Executors(java.util.concurrent.Executors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) MessageFilter(org.springframework.integration.filter.MessageFilter) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ExecutorService(java.util.concurrent.ExecutorService) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Mockito(org.mockito.Mockito) Assert.assertNull(org.junit.Assert.assertNull) RetryContext(org.springframework.retry.RetryContext) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) RetryTemplate(org.springframework.retry.support.RetryTemplate) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 30 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class MethodInvokingRouterTests method testClassAsKeyResolution.

@Test
public void testClassAsKeyResolution() {
    QueueChannel stringsChannel = new QueueChannel();
    QueueChannel numbersChannel = new QueueChannel();
    TestChannelResolver channelResolver = new TestChannelResolver();
    channelResolver.addChannel("stringsChannel", stringsChannel);
    channelResolver.addChannel("numbersChannel", numbersChannel);
    MethodInvokingRouter router = new MethodInvokingRouter(new ClassAsKeyTestBean());
    router.setChannelResolver(channelResolver);
    router.setChannelMapping(String.class.getName(), "stringsChannel");
    router.setChannelMapping(Integer.class.getName(), "numbersChannel");
    Message<?> message = new GenericMessage<>("bar");
    router.handleMessage(message);
    Message<?> replyMessage = stringsChannel.receive(10000);
    assertNotNull(replyMessage);
    assertEquals(message, replyMessage);
    message = new GenericMessage<>(11);
    router.handleMessage(message);
    replyMessage = numbersChannel.receive(10000);
    assertNotNull(replyMessage);
    assertEquals(message, replyMessage);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) TestChannelResolver(org.springframework.integration.channel.TestChannelResolver) Test(org.junit.Test)

Aggregations

QueueChannel (org.springframework.integration.channel.QueueChannel)709 Test (org.junit.Test)669 GenericMessage (org.springframework.messaging.support.GenericMessage)186 Message (org.springframework.messaging.Message)173 BeanFactory (org.springframework.beans.factory.BeanFactory)162 MessageChannel (org.springframework.messaging.MessageChannel)100 Matchers.containsString (org.hamcrest.Matchers.containsString)66 CountDownLatch (java.util.concurrent.CountDownLatch)59 DirectChannel (org.springframework.integration.channel.DirectChannel)57 ArrayList (java.util.ArrayList)55 PollableChannel (org.springframework.messaging.PollableChannel)55 MessagingException (org.springframework.messaging.MessagingException)53 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)51 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 Socket (java.net.Socket)44 ErrorMessage (org.springframework.messaging.support.ErrorMessage)42 ServerSocket (java.net.ServerSocket)41 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)39 IOException (java.io.IOException)35 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)35