Refactored interceptors to use EndpointInterceptor interface rather than Advice.

This commit is contained in:
Mark Fisher
2008-07-09 12:24:16 +00:00
parent f9150886db
commit dfd7bf551c
17 changed files with 376 additions and 428 deletions

View File

@@ -0,0 +1,112 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.config;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.endpoint.EndpointPoller;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.integration.endpoint.SourceEndpoint;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class EndpointInterceptorTests {
@Test
public void testHandlerEndpointWithBeanInterceptors() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointInterceptorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("handlerEndpointWithBeanInterceptors");
testInterceptors(endpoint, context, true);
}
@Test
public void testHandlerEndpointWithRefInterceptors() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointInterceptorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("handlerEndpointWithRefInterceptors");
testInterceptors(endpoint, context, false);
}
@Test
public void testTargetEndpointWithBeanInterceptors() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointInterceptorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("targetEndpointWithBeanInterceptors");
testInterceptors(endpoint, context, true);
}
@Test
public void testTargetEndpointWithRefInterceptors() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointInterceptorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("targetEndpointWithRefInterceptors");
testInterceptors(endpoint, context, false);
}
@Test
public void testSourceEndpointWithBeanInterceptors() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointInterceptorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("sourceEndpointWithBeanInterceptors");
testInterceptors(endpoint, context, true);
}
@Test
public void testSourceEndpointWithRefInterceptors() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointInterceptorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("sourceEndpointWithRefInterceptors");
testInterceptors(endpoint, context, false);
}
private static void testInterceptors(MessageEndpoint endpoint, ClassPathXmlApplicationContext context, boolean innerBeans) {
TestPreSendInterceptor preInterceptor = null;
TestAroundSendEndpointInterceptor aroundInterceptor = null;
if (innerBeans) {
preInterceptor = (TestPreSendInterceptor) ((AbstractEndpoint) endpoint).getInterceptors().get(0);
aroundInterceptor = (TestAroundSendEndpointInterceptor) ((AbstractEndpoint) endpoint).getInterceptors().get(1);
}
else {
preInterceptor = (TestPreSendInterceptor) context.getBean("preInterceptor");
aroundInterceptor = (TestAroundSendEndpointInterceptor) context.getBean("aroundInterceptor");
}
assertEquals(0, preInterceptor.getCount());
assertEquals(0, aroundInterceptor.getCount());
if (endpoint instanceof SourceEndpoint) {
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
channel.send(new StringMessage("foo"));
endpoint.send(new GenericMessage<EndpointPoller>(new EndpointPoller()));
}
else {
endpoint.send(new StringMessage("test"));
}
assertEquals(1, preInterceptor.getCount());
assertEquals(2, aroundInterceptor.getCount());
context.stop();
}
}

View File

@@ -1,110 +0,0 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.endpoint.EndpointPoller;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class MessageEndpointBeanPostProcessorTests {
@Test
public void testNoProxyCreatedForHandlerEndpointWithEmptyAdviceChain() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"messageEndpointBeanPostProcessorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("handlerEndpointWithoutAdvice");
assertFalse(AopUtils.isAopProxy(endpoint));
}
@Test
public void testHandlerEndpointWithAdviceChain() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"messageEndpointBeanPostProcessorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("handlerEndpointWithAdvice");
assertTrue(AopUtils.isAopProxy(endpoint));
TestBeforeAdvice beforeAdvice = (TestBeforeAdvice) context.getBean("simpleAdvice");
TestEndpointInterceptor interceptor = (TestEndpointInterceptor) context.getBean("interceptor");
assertEquals(0, beforeAdvice.getCount());
assertEquals(0, interceptor.getCount());
endpoint.send(new StringMessage("test"));
assertEquals(1, beforeAdvice.getCount());
assertEquals(2, interceptor.getCount());
context.stop();
}
@Test
public void testNoProxyCreatedForTargetEndpointWithEmptyAdviceChain() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"messageEndpointBeanPostProcessorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("targetEndpointWithoutAdvice");
assertFalse(AopUtils.isAopProxy(endpoint));
}
@Test
public void testTargetEndpointWithAdviceChain() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"messageEndpointBeanPostProcessorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("targetEndpointWithAdvice");
assertTrue(AopUtils.isAopProxy(endpoint));
TestBeforeAdvice beforeAdvice = (TestBeforeAdvice) context.getBean("simpleAdvice");
TestEndpointInterceptor interceptor = (TestEndpointInterceptor) context.getBean("interceptor");
assertEquals(0, beforeAdvice.getCount());
assertEquals(0, interceptor.getCount());
endpoint.send(new StringMessage("test"));
assertEquals(1, beforeAdvice.getCount());
assertEquals(2, interceptor.getCount());
context.stop();
}
@Test
public void testNoProxyCreatedForSourceEndpointWithEmptyAdviceChain() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"messageEndpointBeanPostProcessorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("sourceEndpointWithoutAdvice");
assertFalse(AopUtils.isAopProxy(endpoint));
}
@Test
public void testSourceEndpointWithAdviceChain() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"messageEndpointBeanPostProcessorTests.xml", this.getClass());
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("sourceEndpointWithAdvice");
assertTrue(AopUtils.isAopProxy(endpoint));
TestBeforeAdvice beforeAdvice = (TestBeforeAdvice) context.getBean("simpleAdvice");
TestEndpointInterceptor interceptor = (TestEndpointInterceptor) context.getBean("interceptor");
assertEquals(0, beforeAdvice.getCount());
assertEquals(0, interceptor.getCount());
endpoint.send(new GenericMessage<EndpointPoller>(new EndpointPoller()));
assertEquals(1, beforeAdvice.getCount());
assertEquals(2, interceptor.getCount());
context.stop();
}
}

View File

@@ -16,15 +16,16 @@
package org.springframework.integration.config;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.integration.endpoint.interceptor.EndpointInterceptorAdapter;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageTarget;
/**
* @author Mark Fisher
*/
public class TestBeforeAdvice implements MethodBeforeAdvice {
public class TestAroundSendEndpointInterceptor extends EndpointInterceptorAdapter {
private AtomicInteger counter = new AtomicInteger();
@@ -33,8 +34,12 @@ public class TestBeforeAdvice implements MethodBeforeAdvice {
return this.counter.get();
}
public void before(Method method, Object[] args, Object target) throws Throwable {
@Override
public boolean aroundSend(Message<?> message, MessageTarget endpoint) {
this.counter.incrementAndGet();
boolean result = endpoint.send(message);
this.counter.incrementAndGet();
return result;
}
}

View File

@@ -18,14 +18,13 @@ package org.springframework.integration.config;
import java.util.concurrent.atomic.AtomicInteger;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.integration.endpoint.interceptor.EndpointInterceptorAdapter;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class TestEndpointInterceptor extends EndpointInterceptorAdapter {
public class TestPreSendInterceptor extends EndpointInterceptorAdapter {
private AtomicInteger counter = new AtomicInteger();
@@ -34,11 +33,10 @@ public class TestEndpointInterceptor extends EndpointInterceptorAdapter {
return this.counter.get();
}
public boolean aroundInvoke(MethodInvocation invocation) throws Throwable {
@Override
public boolean preSend(Message<?> message) {
this.counter.incrementAndGet();
Boolean result = (Boolean) invocation.proceed();
this.counter.incrementAndGet();
return result;
return true;
}
}

View File

@@ -29,7 +29,6 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.aopalliance.aop.Advice;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
@@ -53,8 +52,8 @@ import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.ChannelRegistryAware;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.EndpointInterceptor;
import org.springframework.integration.endpoint.HandlerEndpoint;
import org.springframework.integration.endpoint.interceptor.ConcurrencyInterceptor;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
@@ -184,11 +183,9 @@ public class MessagingAnnotationPostProcessorTests {
postProcessor.postProcessAfterInitialization(testBean, "testBean");
HandlerEndpoint endpoint = (HandlerEndpoint) messageBus.lookupEndpoint("testBean.MessageHandler.endpoint");
assertEquals(1, endpoint.getInterceptors().size());
Advice interceptor = endpoint.getInterceptors().get(0);
EndpointInterceptor interceptor = endpoint.getInterceptors().get(0);
DirectFieldAccessor accessor = new DirectFieldAccessor(interceptor);
ConcurrencyInterceptor concurrencyInterceptor = (ConcurrencyInterceptor)
accessor.getPropertyValue("interceptor");
accessor = new DirectFieldAccessor(concurrencyInterceptor);
accessor = new DirectFieldAccessor(interceptor);
ConcurrentTaskExecutor cte = (ConcurrentTaskExecutor) accessor.getPropertyValue("executor");
ThreadPoolExecutor executor = (ThreadPoolExecutor) cte.getConcurrentExecutor();
assertEquals(17, executor.getCorePoolSize());

View File

@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
<message-bus auto-startup="false"/>
<channel id="testChannel"/>
<channel id="replyChannel"/>
<service-activator id="handlerEndpointWithBeanInterceptors"
input-channel="testChannel"
ref="testHandler"
output-channel="replyChannel">
<schedule period="100"/>
<interceptors>
<beans:bean class="org.springframework.integration.config.TestPreSendInterceptor"/>
<beans:bean class="org.springframework.integration.config.TestAroundSendEndpointInterceptor"/>
</interceptors>
</service-activator>
<service-activator id="handlerEndpointWithRefInterceptors"
input-channel="testChannel"
ref="testHandler"
output-channel="replyChannel">
<schedule period="100"/>
<interceptors>
<ref bean="preInterceptor"/>
<ref bean="aroundInterceptor"/>
</interceptors>
</service-activator>
<channel-adapter id="targetEndpointWithBeanInterceptors"
channel="testChannel"
target="testTarget">
<schedule period="100"/>
<interceptors>
<beans:bean class="org.springframework.integration.config.TestPreSendInterceptor"/>
<beans:bean class="org.springframework.integration.config.TestAroundSendEndpointInterceptor"/>
</interceptors>
</channel-adapter>
<channel-adapter id="targetEndpointWithRefInterceptors"
channel="testChannel"
target="testTarget">
<schedule period="100"/>
<interceptors>
<ref bean="preInterceptor"/>
<ref bean="aroundInterceptor"/>
</interceptors>
</channel-adapter>
<channel-adapter id="sourceEndpointWithBeanInterceptors"
source="testSource"
channel="testChannel">
<schedule period="100"/>
<interceptors>
<beans:bean class="org.springframework.integration.config.TestPreSendInterceptor"/>
<beans:bean class="org.springframework.integration.config.TestAroundSendEndpointInterceptor"/>
</interceptors>
</channel-adapter>
<channel-adapter id="sourceEndpointWithRefInterceptors"
source="testSource"
channel="testChannel">
<schedule period="100"/>
<interceptors>
<ref bean="preInterceptor"/>
<ref bean="aroundInterceptor"/>
</interceptors>
</channel-adapter>
<beans:bean id="testHandler" class="org.springframework.integration.config.TestHandler"/>
<beans:bean id="testSource" class="org.springframework.integration.config.TestSource"/>
<beans:bean id="testTarget" class="org.springframework.integration.config.TestTarget"/>
<beans:bean id="preInterceptor" class="org.springframework.integration.config.TestPreSendInterceptor"/>
<beans:bean id="aroundInterceptor" class="org.springframework.integration.config.TestAroundSendEndpointInterceptor"/>
</beans:beans>

View File

@@ -1,76 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
<message-bus auto-startup="false"/>
<channel id="testChannel"/>
<channel id="replyChannel"/>
<handler-endpoint id="handlerEndpointWithoutAdvice"
input-channel="testChannel"
ref="testHandler"
output-channel="replyChannel">
<schedule period="100"/>
</handler-endpoint>
<handler-endpoint id="handlerEndpointWithAdvice"
input-channel="testChannel"
ref="testHandler"
output-channel="replyChannel">
<schedule period="100"/>
<interceptors>
<ref bean="simpleAdvice"/>
<ref bean="interceptor"/>
</interceptors>
</handler-endpoint>
<channel-adapter id="targetEndpointWithoutAdvice"
channel="testChannel"
target="testTarget">
<schedule period="100"/>
</channel-adapter>
<channel-adapter id="targetEndpointWithAdvice"
channel="testChannel"
target="testTarget">
<schedule period="100"/>
<interceptors>
<ref bean="simpleAdvice"/>
<ref bean="interceptor"/>
</interceptors>
</channel-adapter>
<channel-adapter id="sourceEndpointWithoutAdvice"
source="testSource"
channel="replyChannel">
<schedule period="100"/>
</channel-adapter>
<channel-adapter id="sourceEndpointWithAdvice"
source="testSource"
channel="testChannel">
<schedule period="100"/>
<interceptors>
<ref bean="simpleAdvice"/>
<ref bean="interceptor"/>
</interceptors>
</channel-adapter>
<beans:bean id="testHandler" class="org.springframework.integration.config.TestHandler"/>
<beans:bean id="testSource" class="org.springframework.integration.config.TestSource"/>
<beans:bean id="testTarget" class="org.springframework.integration.config.TestTarget"/>
<beans:bean id="simpleAdvice" class="org.springframework.integration.config.TestBeforeAdvice"/>
<beans:bean id="interceptor" class="org.springframework.integration.config.TestEndpointInterceptor"/>
</beans:beans>