List of usage examples for org.springframework.integration.handler.advice AbstractRequestHandlerAdvice AbstractRequestHandlerAdvice
AbstractRequestHandlerAdvice
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/*from w w 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()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
/** * Verify that Errors such as OOM are properly propagated. *///from w ww .j a v a 2s . co m @Test public void throwableProperlyPropagated() throws Exception { AbstractRequestHandlerAdvice advice = new AbstractRequestHandlerAdvice() { @Override protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception { Object result; try { result = callback.execute(); } catch (Exception e) { // should not be unwrapped because the cause is a Throwable throw this.unwrapExceptionIfNecessary(e); } return result; } }; final Throwable theThrowable = new Throwable("foo"); MethodInvocation methodInvocation = mock(MethodInvocation.class); Method method = AbstractReplyProducingMessageHandler.class.getDeclaredMethod("handleRequestMessage", Message.class); when(methodInvocation.getMethod()).thenReturn(method); when(methodInvocation.getArguments()).thenReturn(new Object[] { new GenericMessage<String>("foo") }); try { doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws Throwable { throw theThrowable; } }).when(methodInvocation).proceed(); advice.invoke(methodInvocation); fail("Expected throwable"); } catch (Throwable t) { assertSame(theThrowable, t); } }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void testInappropriateAdvice() throws Exception { final AtomicBoolean called = new AtomicBoolean(false); Advice advice = new AbstractRequestHandlerAdvice() { @Override/* w ww . ja v a2 s.c o m*/ 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, new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { } }); consumer.setAdviceChain(Collections.singletonList(advice)); consumer.setTaskExecutor( new ErrorHandlingTaskExecutor(Executors.newSingleThreadExecutor(), new ErrorHandler() { @Override public void handleError(Throwable t) { } })); consumer.afterPropertiesSet(); 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(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { logMessage.set((String) invocation.getArguments()[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()); assertTrue(logMessage.get() .endsWith("can only be used for MessageHandlers; " + "an attempt to advise method 'call' in " + "'org.springframework.integration.endpoint.AbstractPollingEndpoint$1' is ignored")); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void filterDiscardWithinAdvice() { MessageFilter filter = new MessageFilter(new MessageSelector() { @Override/*from w ww.java2 s. c om*/ public boolean accept(Message<?> message) { return false; } }); final QueueChannel discardChannel = new QueueChannel(); filter.setDiscardChannel(discardChannel); List<Advice> adviceChain = new ArrayList<Advice>(); final AtomicReference<Message<?>> discardedWithinAdvice = new AtomicReference<Message<?>>(); adviceChain.add(new AbstractRequestHandlerAdvice() { @Override protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception { Object result = callback.execute(); discardedWithinAdvice.set(discardChannel.receive(0)); return result; } }); filter.setAdviceChain(adviceChain); filter.afterPropertiesSet(); filter.handleMessage(new GenericMessage<String>("foo")); assertNotNull(discardedWithinAdvice.get()); assertNull(discardChannel.receive(0)); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void filterDiscardOutsideAdvice() { MessageFilter filter = new MessageFilter(new MessageSelector() { @Override/* ww w . j a va 2 s.co m*/ public boolean accept(Message<?> message) { return false; } }); final QueueChannel discardChannel = new QueueChannel(); filter.setDiscardChannel(discardChannel); List<Advice> adviceChain = new ArrayList<Advice>(); final AtomicReference<Message<?>> discardedWithinAdvice = new AtomicReference<Message<?>>(); final AtomicBoolean adviceCalled = new AtomicBoolean(); adviceChain.add(new AbstractRequestHandlerAdvice() { @Override protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception { Object result = callback.execute(); discardedWithinAdvice.set(discardChannel.receive(0)); adviceCalled.set(true); return result; } }); filter.setAdviceChain(adviceChain); filter.setDiscardWithinAdvice(false); filter.afterPropertiesSet(); filter.handleMessage(new GenericMessage<String>("foo")); assertTrue(adviceCalled.get()); assertNull(discardedWithinAdvice.get()); assertNotNull(discardChannel.receive(0)); }