Example usage for org.springframework.integration.handler AbstractReplyProducingMessageHandler setOutputChannel

List of usage examples for org.springframework.integration.handler AbstractReplyProducingMessageHandler setOutputChannel

Introduction

In this page you can find the example usage for org.springframework.integration.handler AbstractReplyProducingMessageHandler setOutputChannel.

Prototype

@Override
    public void setOutputChannel(MessageChannel outputChannel) 

Source Link

Usage

From source file:nl.rav.comparision.integration.unitofwork.java.UnitOfWorkSpringTest.java

private void createRouteBuilder() {
    handler = new AbstractReplyProducingMessageHandler() {
        @Override//from w ww  . jav a2 s. c  o  m
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return "baz";
        }
    };
    AbstractReplyProducingMessageHandler handler2 = new AbstractReplyProducingMessageHandler() {
        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (doFail.get()) {
                throw new RuntimeException("qux");
            }
            return requestMessage.getPayload();
        }
    };

    SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
    taskExecutor.setThreadNamePrefix("test-");
    ExecutorChannel intermediate = new ExecutorChannel(taskExecutor);
    handler.setOutputChannel(intermediate);
    intermediate.subscribe(handler2);

    replies = new QueueChannel();
    handler2.setOutputChannel(replies);

    successChannel = new QueueChannel();
    failureChannel = new QueueChannel();
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setBeanFactory(mock(BeanFactory.class));
    advice.setSuccessChannel(successChannel);
    advice.setFailureChannel(failureChannel);
    advice.setOnSuccessExpression("'foo'");
    advice.setOnFailureExpression("'bar:' + #exception.message");

    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.afterPropertiesSet();
    handler2.setAdviceChain(adviceChain);
    handler2.afterPropertiesSet();
}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void successFailureAdvice() {
    final AtomicBoolean doFail = new AtomicBoolean();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override// w  w  w  . j  a va 2 s .c  o  m
        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!");

    // 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.setOnSuccessExpression("'foo'");
    advice.setOnFailureExpression("'bar:' + #exception.message");

    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.afterPropertiesSet();

    // advice with success
    handler.handleMessage(message);
    reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("baz", reply.getPayload());

    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());

}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void propagateOnSuccessExpressionFailures() {
    final AtomicBoolean doFail = new AtomicBoolean();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override//from   ww w. j av a2s.  c o m
        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.setOnSuccessExpression("1/0");
    advice.setOnFailureExpression("1/0");

    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.afterPropertiesSet();

    // failing advice with success
    handler.handleMessage(message);
    Message<?> reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("baz", reply.getPayload());

    Message<?> success = successChannel.receive(1000);
    assertNotNull(success);
    assertEquals("Hello, world!", ((AdviceMessage) success).getInputMessage().getPayload());
    assertEquals(ArithmeticException.class, success.getPayload().getClass());
    assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());

    // propagate failing advice with success
    advice.setPropagateEvaluationFailures(true);
    try {
        handler.handleMessage(message);
        fail("Expected Exception");
    } catch (MessageHandlingException e) {
        assertEquals("/ by zero", e.getCause().getMessage());
    }
    reply = replies.receive(1);
    assertNull(reply);

    success = successChannel.receive(1000);
    assertNotNull(success);
    assertEquals("Hello, world!", ((AdviceMessage) success).getInputMessage().getPayload());
    assertEquals(ArithmeticException.class, success.getPayload().getClass());
    assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());

}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void propagateOnFailureExpressionFailures() {
    final AtomicBoolean doFail = new AtomicBoolean(true);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override//from w  w w . ja  v  a2  s  .  c  o  m
        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.setSuccessChannel(successChannel);
    advice.setFailureChannel(failureChannel);
    advice.setOnSuccessExpression("1/0");
    advice.setOnFailureExpression("1/0");

    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    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(1000);
    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(1000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass());
    assertEquals("qux", ((Exception) failure.getPayload()).getCause().getMessage());

}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void circuitBreakerTests() throws Exception {
    final AtomicBoolean doFail = new AtomicBoolean();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override//  w w  w  .  j av a2s  .  c  o  m
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (doFail.get()) {
                throw new RuntimeException("foo");
            }
            return "bar";
        }

    };
    handler.setBeanName("baz");
    handler.setOutputChannel(new QueueChannel());
    RequestHandlerCircuitBreakerAdvice advice = new RequestHandlerCircuitBreakerAdvice();
    /*
     * Circuit breaker opens after 2 failures; allows a new attempt after 100ms and
     * immediately opens again if that attempt fails. After a successful attempt,
     * we reset the failure counter.
     */
    advice.setThreshold(2);
    advice.setHalfOpenAfter(100);

    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.afterPropertiesSet();

    doFail.set(true);
    Message<String> message = new GenericMessage<String>("Hello, world!");
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("Circuit Breaker is Open for baz", e.getCause().getMessage());
    }
    Thread.sleep(100);
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("Circuit Breaker is Open for baz", e.getCause().getMessage());
    }
    Thread.sleep(100);
    doFail.set(false);
    handler.handleMessage(message);
    doFail.set(true);
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("Circuit Breaker is Open for baz", e.getCause().getMessage());
    }
}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void defaultRetrySucceedOnThirdTry() {
    final AtomicInteger counter = new AtomicInteger(2);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override//  ww w  . j  av a 2 s  .c  o m
        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();

    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.afterPropertiesSet();

    Message<String> message = new GenericMessage<String>("Hello, world!");
    handler.handleMessage(message);
    assertTrue(counter.get() == -1);
    Message<?> reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("bar", reply.getPayload());

}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void defaultStatefulRetrySucceedOnThirdTry() {
    final AtomicInteger counter = new AtomicInteger(2);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override/*from ww w. ja v a 2 s. co  m*/
        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 RetryStateGenerator() {
        public RetryState determineRetryState(Message<?> message) {
            return new DefaultRetryState(message.getHeaders().getId());
        }
    });

    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    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(1000);
    assertNotNull(reply);
    assertEquals("bar", reply.getPayload());

}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void defaultStatefulRetryRecoverAfterThirdTry() {
    final AtomicInteger counter = new AtomicInteger(3);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override//from ww w  .j  ava  2s.  c om
        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 RetryStateGenerator() {
        public RetryState determineRetryState(Message<?> message) {
            return new DefaultRetryState(message.getHeaders().getId());
        }
    });

    defaultStatefulRetryRecoverAfterThirdTryGuts(counter, handler, replies, advice);

}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void defaultStatefulRetryRecoverAfterThirdTrySpelState() {
    final AtomicInteger counter = new AtomicInteger(3);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override//from w w  w. java 2 s .  co  m
        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);

}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void testINT2858RetryAdviceAsNestedInAdviceChain() {
    final AtomicInteger counter = new AtomicInteger(0);

    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override/*ww  w .  j  av a 2 s. c o  m*/
        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.setOnFailureExpression("#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(new MethodInterceptor() {
        public Object invoke(MethodInvocation invocation) throws Throwable {
            throw new RuntimeException("intentional: " + counter.incrementAndGet());
        }
    });

    handler.setAdviceChain(adviceChain);
    handler.afterPropertiesSet();

    handler.handleMessage(new GenericMessage<String>("test"));
    Message<?> receive = replies.receive(1000);
    assertNotNull(receive);
    assertEquals("intentional: 3", receive.getPayload());
    assertEquals(1, outerCounter.get());
}