INT-2214, INT-343, INT-2250 MessageHandler Advice

Add general capability to advise just the handleRequestMessage
part of an AbstractReplyProducingMessageHandler.

This is to advise just the immediate operation, and not the
entire downstream flow.

Uses include:

* outbound gateway post processing
* adding retry behavior using spring-retry
* adding circuit breaker functionality

Initial commit for review.

Also need to advise simple message handlers (such as file
etc) to allow them to post-process file operations
with payload.delete(), payload.renameTo(...) etc.

INT-2250 Add Circuit Breaker Advice

INT-343 Add Retry Advice

Stateless and Stateful retry using spring-retry. Stateless
means the RetryTemplate performs the retries internally.
Stateful means the exception is thrown (e.g. to JMS container)
and the retry state is maintained by spring-retry.

INT-2215, INT-343, INT-2250 Refactoring

Factor out common abstract Advice class.

INT-2214 Catch Evaluation Expression Exceptions

If an onSuccess expression evaluation fails, add an
option so the user can decide whether such an exception is
caught, or propagated to the caller.

INT-2214 etc PR Review Polishing

INT-2214 etc Namespace Core, File, FTP

Add <request-handler-advice-chain/> to outbound endpoints.

INT-2214 etc. More Namespace Support

amqp, event, gemfire, groovy, http, ip, jdbc, jms, jmx, jpa, mail, rmi, sftp, twitter, ws, xmpp

INT-2214 etc Polishing

PR Review

INT-2214 etc Polishing

Don't catch Throwable.

Move Advice classes to handler.advice package.
This commit is contained in:
Gary Russell
2012-07-14 13:00:38 -04:00
committed by Oleg Zhurakousky
parent 56e3c22970
commit 08cbab08c2
114 changed files with 2661 additions and 296 deletions

View File

@@ -43,6 +43,13 @@
xmpp-connection="testConnection">
</int-xmpp:outbound-channel-adapter>
<int-xmpp:outbound-channel-adapter id="advised" channel="errorChannel"
xmpp-connection="testConnection">
<int-xmpp:request-handler-advice-chain>
<bean class="org.springframework.integration.xmpp.config.ChatMessageOutboundChannelAdapterParserTests$FooAdvice" />
</int-xmpp:request-handler-advice-chain>
</int-xmpp:outbound-channel-adapter>
<int:chain input-channel="outboundChainChannel">
<int-xmpp:outbound-channel-adapter xmpp-connection="testConnection"/>
</int:chain>

View File

@@ -16,6 +16,11 @@
package org.springframework.integration.xmpp.config;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.List;
import org.jivesoftware.smack.XMPPConnection;
@@ -24,15 +29,17 @@ import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.endpoint.PollingConsumer;
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.xmpp.XmppHeaders;
@@ -41,11 +48,6 @@ import org.springframework.integration.xmpp.support.XmppHeaderMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* @author Oleg Zhurakousky
* @author Mark Fisher
@@ -57,10 +59,12 @@ public class ChatMessageOutboundChannelAdapterParserTests {
@Autowired
private ApplicationContext context;
@Autowired
private XmppHeaderMapper headerMapper;
private static volatile int adviceCalled;
@Test
public void testPollingConsumer() {
Object pollingConsumer = context.getBean("withHeaderMapper");
@@ -75,11 +79,19 @@ public class ChatMessageOutboundChannelAdapterParserTests {
assertTrue(eventConsumer instanceof SubscribableChannel);
}
@Test
public void advised() {
MessageHandler handler = TestUtils.getPropertyValue(context.getBean("advised"),
"handler", MessageHandler.class);
handler.handleMessage(new GenericMessage<String>("foo"));
assertEquals(1, adviceCalled);
}
@SuppressWarnings("unchecked")
@Test
public void testEventConsumer() {
Object eventConsumer = context.getBean("outboundEventAdapter");
DefaultXmppHeaderMapper headerMapper =
DefaultXmppHeaderMapper headerMapper =
TestUtils.getPropertyValue(eventConsumer, "handler.headerMapper", DefaultXmppHeaderMapper.class);
List<String> requestHeaderNames = TestUtils.getPropertyValue(headerMapper, "requestHeaderNames", List.class);
assertEquals(2, requestHeaderNames.size());
@@ -98,7 +110,7 @@ public class ChatMessageOutboundChannelAdapterParserTests {
Message<?> message = MessageBuilder.withPayload("hello").setHeader(XmppHeaders.TO, "oleg").
setHeader("foobar", "foobar").build();
XMPPConnection connection = context.getBean("testConnection", XMPPConnection.class);
Mockito.doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
@@ -108,9 +120,9 @@ public class ChatMessageOutboundChannelAdapterParserTests {
return null;
}})
.when(connection).sendPacket(Mockito.any(org.jivesoftware.smack.packet.Message.class));
channel.send(message);
verify(connection, times(1)).sendPacket(Mockito.any(org.jivesoftware.smack.packet.Message.class));
Mockito.reset(connection);
}
@@ -137,4 +149,13 @@ public class ChatMessageOutboundChannelAdapterParserTests {
Mockito.reset(connection);
}
public static class FooAdvice extends AbstractRequestHandlerAdvice {
@Override
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
adviceCalled++;
return null;
}
}
}

View File

@@ -29,6 +29,16 @@
xmpp-connection="testConnection"
channel="eventChannel"
order="34"/>
<int-xmpp:presence-outbound-channel-adapter id="advised"
xmpp-connection="testConnection"
channel="eventChannel"
order="34">
<int-xmpp:request-handler-advice-chain>
<bean class="org.springframework.integration.xmpp.config.PresenceOutboundChannelAdapterParserTests$FooAdvice" />
</int-xmpp:request-handler-advice-chain>
</int-xmpp:presence-outbound-channel-adapter>
<int-xmpp:presence-outbound-channel-adapter id="eventOutboundRosterChannel"
xmpp-connection="testConnection"

View File

@@ -23,14 +23,16 @@ import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.dispatcher.UnicastingDispatcher;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.endpoint.PollingConsumer;
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareMessageHandler;
import org.springframework.test.context.ContextConfiguration;
@@ -48,6 +50,8 @@ public class PresenceOutboundChannelAdapterParserTests {
@Autowired
private ApplicationContext context;
private static volatile int adviceCalled;
@Test
public void testRosterEventOutboundChannelAdapterParserAsPollingConsumer(){
Object pollingConsumer = context.getBean("pollingOutboundRosterAdapter");
@@ -66,6 +70,15 @@ public class PresenceOutboundChannelAdapterParserTests {
assertEquals(34, TestUtils.getPropertyValue(handler, "order"));
}
@Test
public void advised(){
Object eventConsumer = context.getBean("advised");
assertTrue(eventConsumer instanceof EventDrivenConsumer);
MessageHandler handler = TestUtils.getPropertyValue(eventConsumer, "handler", MessageHandler.class);
handler.handleMessage(new GenericMessage<String>("foo"));
assertEquals(1, adviceCalled);
}
@Test
public void testRosterEventOutboundChannel(){
Object channel = context.getBean("eventOutboundRosterChannel");
@@ -78,4 +91,13 @@ public class PresenceOutboundChannelAdapterParserTests {
assertEquals(45, TestUtils.getPropertyValue(handlers.toArray()[0], "order"));
}
public static class FooAdvice extends AbstractRequestHandlerAdvice {
@Override
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
adviceCalled++;
return null;
}
}
}