EndpointInterceptor postHandle now accepts the replyMessage only. InterceptingMessageHandler has been renamed MessageHandlerDecorator. Either the decorator or the interceptor's aroundHandle method may be removed since they are redundant.

This commit is contained in:
Mark Fisher
2008-08-12 17:32:51 +00:00
parent ac770ce178
commit d0581e881f
10 changed files with 56 additions and 44 deletions

View File

@@ -229,7 +229,7 @@ public abstract class AbstractEndpoint implements MessageEndpoint, ChannelRegist
Message<?> reply = this.handleMessage(message);
for (int i = index - 1; i >= 0; i--) {
EndpointInterceptor interceptor = this.interceptors.get(i);
reply = interceptor.postHandle(message, reply);
reply = interceptor.postHandle(reply);
}
if (reply != null) {
this.getMessageExchangeTemplate().send(message, this.target);

View File

@@ -26,8 +26,8 @@ public interface EndpointInterceptor {
Message<?> preHandle(Message<?> requestMessage);
Message<?> aroundHandle(Message<?> message, MessageHandler handler);
Message<?> aroundHandle(Message<?> requestMessage, MessageHandler handler);
Message<?> postHandle(Message<?> requestMessage, Message<?> replyMessage);
Message<?> postHandle(Message<?> replyMessage);
}

View File

@@ -231,7 +231,7 @@ public class SimpleEndpoint<T extends MessageHandler> implements MessageEndpoint
Message<?> replyMessage = this.handler.handle(requestMessage);
for (int i = index - 1; i >= 0; i--) {
EndpointInterceptor interceptor = this.interceptors.get(i);
replyMessage = interceptor.postHandle(requestMessage, replyMessage);
replyMessage = interceptor.postHandle(replyMessage);
}
return replyMessage;
}

View File

@@ -31,11 +31,11 @@ public class EndpointInterceptorAdapter implements EndpointInterceptor {
return requestMessage;
}
public Message<?> aroundHandle(Message<?> message, MessageHandler handler) {
return handler.handle(message);
public Message<?> aroundHandle(Message<?> requestMessage, MessageHandler handler) {
return handler.handle(requestMessage);
}
public Message<?> postHandle(Message<?> requestMessage, Message<?> replyMessage) {
public Message<?> postHandle(Message<?> replyMessage) {
return replyMessage;
}

View File

@@ -17,6 +17,7 @@
package org.springframework.integration.handler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.util.Assert;
/**
@@ -24,18 +25,31 @@ import org.springframework.util.Assert;
*
* @author Mark Fisher
*/
public abstract class InterceptingMessageHandler implements MessageHandler {
public abstract class MessageHandlerDecorator implements MessageHandler {
private MessageHandler target;
private MessageHandler handler;
public InterceptingMessageHandler(MessageHandler target) {
Assert.notNull(target, "target must not be null");
this.target = target;
public MessageHandlerDecorator(MessageHandler handler) {
Assert.notNull(handler, "handler must not be null");
this.handler = handler;
}
public MessageHandlerDecorator() {
}
public void setHandler(MessageHandler handler) {
Assert.notNull(handler, "handler must not be null");
this.handler = handler;
}
public final Message<?> handle(Message<?> message) {
return handle(message, this.target);
if (this.handler == null) {
throw new MessageHandlingException(message,
"MessageHandlerDecorator's handler must not be null");
}
return this.handleInternal(message, this.handler);
}
@@ -43,9 +57,9 @@ public abstract class InterceptingMessageHandler implements MessageHandler {
* The handler method for subclasses to implement.
*
* @param message the message to handle
* @param target the intercepted handler
* @param handler the intercepted handler
* @return a reply message or null
*/
public abstract Message<?> handle(Message<?> message, MessageHandler target);
public abstract Message<?> handleInternal(Message<?> message, MessageHandler handler);
}

View File

@@ -52,23 +52,23 @@ public class EndpointInterceptorTests {
@SuppressWarnings("unchecked")
private static void testInterceptors(MessageEndpoint endpoint, ClassPathXmlApplicationContext context, boolean innerBeans) {
TestPreSendInterceptor preInterceptor = null;
TestAroundSendEndpointInterceptor aroundInterceptor = null;
TestPreHandleInterceptor preInterceptor = null;
TestPostHandleInterceptor postInterceptor = null;
if (innerBeans) {
DirectFieldAccessor accessor = new DirectFieldAccessor(endpoint);
List<EndpointInterceptor> interceptors = (List<EndpointInterceptor>) accessor.getPropertyValue("interceptors");
preInterceptor = (TestPreSendInterceptor) interceptors.get(0);
aroundInterceptor = (TestAroundSendEndpointInterceptor) interceptors.get(1);
preInterceptor = (TestPreHandleInterceptor) interceptors.get(0);
postInterceptor = (TestPostHandleInterceptor) interceptors.get(1);
}
else {
preInterceptor = (TestPreSendInterceptor) context.getBean("preInterceptor");
aroundInterceptor = (TestAroundSendEndpointInterceptor) context.getBean("aroundInterceptor");
preInterceptor = (TestPreHandleInterceptor) context.getBean("preInterceptor");
postInterceptor = (TestPostHandleInterceptor) context.getBean("postInterceptor");
}
assertEquals(0, preInterceptor.getCount());
assertEquals(0, aroundInterceptor.getCount());
assertEquals(0, postInterceptor.getCount());
endpoint.send(new StringMessage("test"));
assertEquals(1, preInterceptor.getCount());
assertEquals(2, aroundInterceptor.getCount());
assertEquals(1, postInterceptor.getCount());
context.stop();
}

View File

@@ -19,13 +19,12 @@ package org.springframework.integration.config;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.integration.endpoint.interceptor.EndpointInterceptorAdapter;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class TestAroundSendEndpointInterceptor extends EndpointInterceptorAdapter {
public class TestPostHandleInterceptor extends EndpointInterceptorAdapter {
private AtomicInteger counter = new AtomicInteger();
@@ -35,11 +34,9 @@ public class TestAroundSendEndpointInterceptor extends EndpointInterceptorAdapte
}
@Override
public Message<?> aroundHandle(Message<?> message, MessageHandler handler) {
public Message<?> postHandle(Message<?> replyMessage) {
this.counter.incrementAndGet();
Message<?> reply = handler.handle(message);
this.counter.incrementAndGet();
return reply;
return replyMessage;
}
}

View File

@@ -24,7 +24,7 @@ import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class TestPreSendInterceptor extends EndpointInterceptorAdapter {
public class TestPreHandleInterceptor extends EndpointInterceptorAdapter {
private AtomicInteger counter = new AtomicInteger();

View File

@@ -19,8 +19,8 @@
output-channel="replyChannel">
<poller period="100"/>
<interceptors>
<beans:bean class="org.springframework.integration.config.TestPreSendInterceptor"/>
<beans:bean class="org.springframework.integration.config.TestAroundSendEndpointInterceptor"/>
<beans:bean class="org.springframework.integration.config.TestPreHandleInterceptor"/>
<beans:bean class="org.springframework.integration.config.TestPostHandleInterceptor"/>
</interceptors>
</service-activator>
@@ -31,15 +31,15 @@
<poller period="100"/>
<interceptors>
<ref bean="preInterceptor"/>
<ref bean="aroundInterceptor"/>
<ref bean="postInterceptor"/>
</interceptors>
</service-activator>
<beans:bean id="testHandler" class="org.springframework.integration.config.TestHandler"/>
<beans:bean id="preInterceptor" class="org.springframework.integration.config.TestPreSendInterceptor"/>
<beans:bean id="preInterceptor" class="org.springframework.integration.config.TestPreHandleInterceptor"/>
<beans:bean id="aroundInterceptor" class="org.springframework.integration.config.TestAroundSendEndpointInterceptor"/>
<beans:bean id="postInterceptor" class="org.springframework.integration.config.TestPostHandleInterceptor"/>
</beans:beans>

View File

@@ -40,11 +40,11 @@ public class MessageHandlerChainTests {
}
@Test
public void testChainWithInterceptors() {
public void testChainWithDecorators() {
MessageHandler handler1 = new TestHandler("*");
MessageHandler handler2 = new TestInterceptingHandler("2", handler1);
MessageHandler handler3 = new TestInterceptingHandler("3", handler2);
MessageHandler handler4 = new TestInterceptingHandler("4", handler3);
MessageHandler handler2 = new TestHandlerDecorator("2", handler1);
MessageHandler handler3 = new TestHandlerDecorator("3", handler2);
MessageHandler handler4 = new TestHandlerDecorator("4", handler3);
MessageHandlerChain chain = new MessageHandlerChain();
chain.add(new TestHandler("a"));
chain.add(handler4);
@@ -68,17 +68,18 @@ public class MessageHandlerChainTests {
}
private static class TestInterceptingHandler extends InterceptingMessageHandler {
private static class TestHandlerDecorator extends MessageHandlerDecorator {
private String text;
TestInterceptingHandler(String text, MessageHandler target) {
super(target);
TestHandlerDecorator(String text, MessageHandler handler) {
super(handler);
this.text = text;
}
public Message<?> handle(Message<?> message, MessageHandler target) {
message = target.handle(new StringMessage(text + message.getPayload()));
@Override
public Message<?> handleInternal(Message<?> message, MessageHandler handler) {
message = handler.handle(new StringMessage(text + message.getPayload()));
return new StringMessage(message.getPayload() + text);
}
}