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 7da7505c54..39038d61ba 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 @@ -21,6 +21,7 @@ import java.lang.reflect.Field; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; @@ -29,9 +30,7 @@ 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.Assert; import org.springframework.util.ReflectionUtils; -import org.springframework.util.StringUtils; /** * @author Oleg Zhurakousky @@ -42,15 +41,14 @@ public final class IdGeneratorConfigurer implements ApplicationListener 0; if (contextHasIdGenerator) { - Assert.state(!StringUtils.hasText(IdGeneratorConfigurer.generatorContextId), - "'MessageHeaders.idGenerator' has already been set and can not be set again"); if (this.setIdGenerator(context)) { IdGeneratorConfigurer.generatorContextId = context.getId(); } @@ -62,7 +60,9 @@ public final class IdGeneratorConfigurer implements ApplicationListener - + 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 5bc2179aab..7526840f18 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 @@ -4,13 +4,7 @@ xmlns:int="http://www.springframework.org/schema/integration" 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 1e9506f5b5..646e7ae038 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.beans.factory.BeanDefinitionStoreException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; import org.springframework.integration.MessageChannel; @@ -123,9 +124,29 @@ public class MessageIdGenerationTests { verify(idGenerator, times(0)).generateId(); parent.close(); } - - @Test(expected=IllegalStateException.class) - public void testCustomIdGenerationWithParentChileIndependentCreationTwoChildrenTwoRegistrars(){ + // similar to the last test, but should not fail because child AC is closed before second child AC is started + @Test + public void testCustomIdGenerationWithParentChildIndependentCreationChildrenRegistrarsOneAtTheTime(){ + ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); + + GenericXmlApplicationContext childA = new GenericXmlApplicationContext(); + childA.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml"); + childA.setParent(parent); + childA.refresh(); + + childA.close(); + + GenericXmlApplicationContext childB = new GenericXmlApplicationContext(); + childB.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml"); + childB.setParent(parent); + childB.refresh(); + + parent.close(); + childB.close(); + } + // should fail because both parent and child define IdGenerator instances + @Test(expected=BeanDefinitionStoreException.class) + public void testCustomIdGenerationWithParentChildIndependentCreation(){ ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass()); GenericXmlApplicationContext childA = new GenericXmlApplicationContext(); @@ -133,6 +154,21 @@ public class MessageIdGenerationTests { childA.setParent(parent); childA.refresh(); } + // should fail because second child attempts to register another instance of IdGenerator + @Test(expected=BeanDefinitionStoreException.class) + public void testCustomIdGenerationWithParentChildIndependentCreationChildrenRegistrars(){ + ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); + + GenericXmlApplicationContext childA = new GenericXmlApplicationContext(); + childA.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml"); + childA.setParent(parent); + childA.refresh(); + + GenericXmlApplicationContext childB = new GenericXmlApplicationContext(); + childB.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml"); + childB.setParent(parent); + childB.refresh(); + } @Test @Ignore @@ -178,15 +214,4 @@ public class MessageIdGenerationTests { return UUID.nameUUIDFromBytes(((System.currentTimeMillis() - System.nanoTime()) + "").getBytes()); } - } - - public static class SampleIdGeneratorA implements IdGenerator { - - public UUID generateId() { - return UUID.nameUUIDFromBytes(((System.currentTimeMillis() - System.nanoTime()) + "").getBytes()); - } - - } - - -} + }}