From 0cb3c14603ace698a2d5b87207e24d4c950fb246 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 16 May 2011 16:57:13 -0400 Subject: [PATCH] INT-1903 polishing --- .../config/IdGeneratorConfigurer.java | 46 ++++++++---- ...dInvokingOutboundChannelAdapterParser.java | 37 ++++++---- .../handler/MethodInvokingMessageHandler.java | 5 ++ .../core/MessageIdGenerationTests-context.xml | 10 +-- .../core/MessageIdGenerationTests.java | 70 ++++++++++++++++++- 5 files changed, 132 insertions(+), 36 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/IdGeneratorConfigurer.java b/spring-integration-core/src/main/java/org/springframework/integration/config/IdGeneratorConfigurer.java index 1af8c02cbd..dda730fef3 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/IdGeneratorConfigurer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/IdGeneratorConfigurer.java @@ -22,33 +22,48 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.BeanDefinitionStoreException; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ApplicationContextEvent; +import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.integration.MessageHeaders; import org.springframework.integration.MessageHeaders.IdGenerator; import org.springframework.util.ReflectionUtils; +import org.springframework.util.StringUtils; /** * @author Oleg Zhurakousky * @since 2.0.4 */ -public final class IdGeneratorConfigurer implements ApplicationListener, DisposableBean { +public final class IdGeneratorConfigurer implements ApplicationListener{ private final Log logger = LogFactory.getLog(getClass()); + + private volatile String generatorContextId; - public void onApplicationEvent(ContextRefreshedEvent event) { - this.setIdGenerator(event.getApplicationContext()); + public void onApplicationEvent(ApplicationContextEvent event) { + if (event instanceof ContextRefreshedEvent){ + if (!StringUtils.hasText(generatorContextId)){ + ApplicationContext contex = event.getApplicationContext(); + if (this.setIdGenerator(contex)){ + this.generatorContextId = contex.getId(); + } + } + } + else if (event instanceof ContextClosedEvent){ + ApplicationContext contex = event.getApplicationContext(); + if (contex.getId().equals(generatorContextId)){ + this.unsetIdGenerator(); + this.generatorContextId = null; + } + } + } - public void destroy() throws Exception { - this.unsetIdGenerator(); - } - - private void setIdGenerator(ApplicationContext context) { + private boolean setIdGenerator(ApplicationContext context) { try { IdGenerator idGeneratorBean = context.getBean(IdGenerator.class); if (logger.isDebugEnabled()) { @@ -56,11 +71,11 @@ public final class IdGeneratorConfigurer implements ApplicationListener(object, methodName); } + public MethodInvokingMessageHandler(Object object) { + processor = new MethodInvokingMessageProcessor(object, ServiceActivator.class); + } + public void setComponentType(String componentType) { this.componentType = componentType; diff --git a/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests-context.xml index 7aafa5a923..5bc2179aab 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests-context.xml @@ -5,11 +5,11 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd"> - - - - - + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests.java index 9d7ae44212..5a4d9c851a 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageIdGenerationTests.java @@ -25,6 +25,7 @@ import java.util.UUID; import org.junit.Ignore; import org.junit.Test; +import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.MessageChannel; import org.springframework.integration.MessageHeaders; @@ -39,19 +40,69 @@ import org.springframework.util.StopWatch; */ public class MessageIdGenerationTests { + @Test - public void testCustomIdGeneration(){ - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); + public void testCustomIdGenerationWithParentRegistrar(){ + ApplicationContext ctx = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-a.xml", this.getClass()); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context.xml"}, this.getClass(), ctx); + IdGenerator idGenerator = context.getBean("idGenerator", IdGenerator.class); MessageChannel inputChannel = context.getBean("input", MessageChannel.class); inputChannel.send(new GenericMessage(0)); verify(idGenerator, times(4)).generateId(); reset(idGenerator); - context.destroy(); + context.close(); + new GenericMessage(0); + verify(idGenerator, times(1)).generateId(); + } + + @Test + public void testCustomIdGenerationWithParentRegistrarClosed(){ + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-a.xml", this.getClass()); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context.xml"}, this.getClass(), ctx); + + IdGenerator idGenerator = context.getBean("idGenerator", IdGenerator.class); + MessageChannel inputChannel = context.getBean("input", MessageChannel.class); + inputChannel.send(new GenericMessage(0)); + verify(idGenerator, times(4)).generateId(); + reset(idGenerator); + ctx.close(); new GenericMessage(0); verify(idGenerator, times(0)).generateId(); } + @Test + public void testCustomIdGenerationWithChildRegistrar(){ + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-a.xml"}, this.getClass(), ctx); + + IdGenerator idGenerator = context.getBean("idGenerator", IdGenerator.class); + MessageChannel inputChannel = context.getBean("input", MessageChannel.class); + inputChannel.send(new GenericMessage(0)); + verify(idGenerator, times(4)).generateId(); + reset(idGenerator); + ctx.close(); + new GenericMessage(0); + verify(idGenerator, times(1)).generateId(); + } + + @Test + public void testCustomIdGenerationWithChildRegistrarClosed(){ + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-a.xml"}, this.getClass(), ctx); + + IdGenerator idGenerator = context.getBean("idGenerator", IdGenerator.class); + MessageChannel inputChannel = context.getBean("input", MessageChannel.class); + inputChannel.send(new GenericMessage(0)); + verify(idGenerator, times(4)).generateId(); + reset(idGenerator); + context.close(); + new GenericMessage(0); + verify(idGenerator, times(0)).generateId(); + } + + + @Test @Ignore public void performanceTest(){ @@ -91,6 +142,19 @@ public class MessageIdGenerationTests { public static class SampleIdGenerator implements IdGenerator { + public SampleIdGenerator(){ + System.out.println("Generator"); + } + public UUID generateId() { + return UUID.nameUUIDFromBytes(((System.currentTimeMillis() - System.nanoTime()) + "").getBytes()); + } + + } + + public static class SampleIdGeneratorA implements IdGenerator { + public SampleIdGeneratorA(){ + System.out.println("Generator A"); + } public UUID generateId() { return UUID.nameUUIDFromBytes(((System.currentTimeMillis() - System.nanoTime()) + "").getBytes());