List of usage examples for org.springframework.integration.channel QueueChannel QueueChannel
public QueueChannel()
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//ww w . jav a 2 s . 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//from w w 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/* ww w . j a v a2s . com*/ 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 w w w. j a v a2 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(); 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 ww w .j a va 2s.com*/ 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 errorMessageSendingRecovererTests() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override/* w ww . j av a2 s.c o m*/ 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.afterPropertiesSet(); Message<String> message = new GenericMessage<String>("Hello, world!"); handler.handleMessage(message); Message<?> error = errors.receive(1000); assertNotNull(error); assertEquals("fooException", ((Exception) error.getPayload()).getCause().getMessage()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void errorMessageSendingRecovererTestsNoThrowable() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override// w w w . j a v a 2 s .co m 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() { @Override public boolean canRetry(RetryContext context) { return false; } }); advice.setRetryTemplate(retryTemplate); advice.afterPropertiesSet(); 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); Message<?> error = errors.receive(1000); assertNotNull(error); assertTrue(error.getPayload() instanceof ErrorMessageSendingRecoverer.RetryExceptionNotAvailableException); assertNotNull(((MessagingException) error.getPayload()).getFailedMessage()); assertSame(message, ((MessagingException) error.getPayload()).getFailedMessage()); }
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/* w w w . j a v a 2s.com*/ 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()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void testINT2858ExpressionAdviceWithSendFailureOnEachRetry() { final AtomicInteger counter = new AtomicInteger(0); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override/*from w w w . j ava2 s .c om*/ 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.setOnFailureExpression("#exception.message"); expressionAdvice.setFailureChannel(errors); adviceChain.add(new RequestHandlerRetryAdvice()); adviceChain.add(expressionAdvice); adviceChain.add(new MethodInterceptor() { public Object invoke(MethodInvocation invocation) throws Throwable { throw new RuntimeException("intentional: " + counter.incrementAndGet()); } }); handler.setAdviceChain(adviceChain); 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(1000); assertNotNull(receive); assertEquals("intentional: " + i, ((MessageHandlingExpressionEvaluatingAdviceException) receive.getPayload()) .getEvaluationResult()); } assertNull(errors.receive(1)); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
/** * Verify that Errors such as OOM are properly propagated and we suppress the * ThrowableHolderException from the output message. *//*from w ww . ja v a 2 s .co m*/ @Test public void throwableProperlyPropagatedAndReported() throws Exception { QueueChannel errors = new QueueChannel(); ExpressionEvaluatingRequestHandlerAdvice expressionAdvice = new ExpressionEvaluatingRequestHandlerAdvice(); expressionAdvice.setOnFailureExpression("'foo'"); expressionAdvice.setFailureChannel(errors); Throwable theThrowable = new Throwable("foo"); ProxyFactory proxyFactory = new ProxyFactory(new Foo(theThrowable)); proxyFactory.addAdvice(expressionAdvice); Bar fooHandler = (Bar) proxyFactory.getProxy(); try { fooHandler.handleRequestMessage(new GenericMessage<String>("foo")); fail("Expected throwable"); } catch (Throwable t) { assertSame(theThrowable, t); ErrorMessage error = (ErrorMessage) errors.receive(1000); assertNotNull(error); assertSame(theThrowable, error.getPayload().getCause()); } }