Search in sources :

Example 11 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class AsyncHandlerTests method testMessagingException.

@Test
public void testMessagingException() {
    QueueChannel errorChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
    this.handler.handleMessage(message);
    assertNull(this.output.receive(0));
    this.whichTest = 2;
    this.latch.countDown();
    Message<?> received = errorChannel.receive(10000);
    assertNotNull(received);
    assertThat(received.getPayload(), instanceOf(MessagingException.class));
    assertSame(message, ((MessagingException) received.getPayload()).getFailedMessage());
    assertNull(this.failedCallbackException);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 12 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class AsyncHandlerTests method setup.

@Before
public void setup() {
    this.executor = Executors.newSingleThreadExecutor();
    this.handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            final SettableListenableFuture<String> future = new SettableListenableFuture<String>();
            AsyncHandlerTests.this.executor.execute(() -> {
                try {
                    latch.await(10, TimeUnit.SECONDS);
                    switch(whichTest) {
                        case 0:
                            future.set("reply");
                            break;
                        case 1:
                            future.setException(new RuntimeException("foo"));
                            break;
                        case 2:
                            future.setException(new MessagingException(requestMessage));
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
            return future;
        }
    };
    this.handler.setAsync(true);
    this.handler.setOutputChannel(this.output);
    this.handler.setBeanFactory(mock(BeanFactory.class));
    this.latch = new CountDownLatch(1);
    Log logger = spy(TestUtils.getPropertyValue(this.handler, "logger", Log.class));
    new DirectFieldAccessor(this.handler).setPropertyValue("logger", logger);
    doAnswer(invocation -> {
        failedCallbackMessage = invocation.getArgument(0);
        failedCallbackException = invocation.getArgument(1);
        exceptionLatch.countDown();
        return null;
    }).when(logger).error(anyString(), any(Throwable.class));
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) MessagingException(org.springframework.messaging.MessagingException) Log(org.apache.commons.logging.Log) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) Before(org.junit.Before)

Example 13 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class MethodInvokingMessageProcessorTests method testProxyInvocation.

@Test
public void testProxyInvocation() {
    final AtomicReference<Object> result = new AtomicReference<>();
    class MyHandler implements MessageHandler {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            result.set(message.getPayload());
        }
    }
    MessageHandler service = new MyHandler();
    final AtomicBoolean adviceCalled = new AtomicBoolean();
    ProxyFactory proxyFactory = new ProxyFactory(service);
    proxyFactory.addAdvice((MethodInterceptor) i -> {
        adviceCalled.set(true);
        return i.proceed();
    });
    service = (MessageHandler) proxyFactory.getProxy(getClass().getClassLoader());
    MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, "handleMessage");
    processor.processMessage(new GenericMessage<>("foo"));
    assertEquals("foo", result.get());
    assertTrue(adviceCalled.get());
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Assert.assertThat(org.junit.Assert.assertThat) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) Method(java.lang.reflect.Method) Header(org.springframework.messaging.handler.annotation.Header) StopWatch(org.springframework.util.StopWatch) UUID(java.util.UUID) EnableIntegration(org.springframework.integration.config.EnableIntegration) Collectors(java.util.stream.Collectors) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) AdditionalAnswers.returnsFirstArg(org.mockito.AdditionalAnswers.returnsFirstArg) Configuration(org.springframework.context.annotation.Configuration) ServiceActivator(org.springframework.integration.annotation.ServiceActivator) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Payload(org.springframework.messaging.handler.annotation.Payload) LogFactory(org.apache.commons.logging.LogFactory) Mockito.mock(org.mockito.Mockito.mock) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) MessagingException(org.springframework.messaging.MessagingException) UseSpelInvoker(org.springframework.integration.annotation.UseSpelInvoker) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Assert.assertSame(org.junit.Assert.assertSame) LinkedHashMap(java.util.LinkedHashMap) MessageBuilder(org.springframework.integration.support.MessageBuilder) Message(org.springframework.messaging.Message) ExpectedException(org.junit.rules.ExpectedException) Description(org.hamcrest.Description) Properties(java.util.Properties) Assert.assertNotNull(org.junit.Assert.assertNotNull) MessagingMethodInvokerHelper(org.springframework.integration.handler.support.MessagingMethodInvokerHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SpelParserConfiguration(org.springframework.expression.spel.SpelParserConfiguration) ApplicationContext(org.springframework.context.ApplicationContext) MessageHeaders(org.springframework.messaging.MessageHeaders) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) GatewayProxyFactoryBean(org.springframework.integration.gateway.GatewayProxyFactoryBean) BeanFactory(org.springframework.beans.factory.BeanFactory) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Expression(org.springframework.expression.Expression) Log(org.apache.commons.logging.Log) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelCompilerMode(org.springframework.expression.spel.SpelCompilerMode) Collections(java.util.Collections) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 14 with MessagingException

use of org.springframework.messaging.MessagingException 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"));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 15 with MessagingException

use of org.springframework.messaging.MessagingException 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());
}
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) MessagingException(org.springframework.messaging.MessagingException) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) AdviceMessage(org.springframework.integration.message.AdviceMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GenericMessage(org.springframework.messaging.support.GenericMessage) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Aggregations

MessagingException (org.springframework.messaging.MessagingException)145 Test (org.junit.Test)61 IOException (java.io.IOException)25 Message (org.springframework.messaging.Message)23 QueueChannel (org.springframework.integration.channel.QueueChannel)22 ErrorMessage (org.springframework.messaging.support.ErrorMessage)21 CountDownLatch (java.util.concurrent.CountDownLatch)17 MessageHandlingException (org.springframework.messaging.MessageHandlingException)16 BeanFactory (org.springframework.beans.factory.BeanFactory)15 MessageChannel (org.springframework.messaging.MessageChannel)15 GenericMessage (org.springframework.messaging.support.GenericMessage)15 MessageHandler (org.springframework.messaging.MessageHandler)14 File (java.io.File)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 DirectChannel (org.springframework.integration.channel.DirectChannel)12 ArrayList (java.util.ArrayList)11 PollableChannel (org.springframework.messaging.PollableChannel)11 Lock (java.util.concurrent.locks.Lock)8 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)8