INT-1903 added tests and some pposlishing to IdGeneratorConfigurer
This commit is contained in:
@@ -41,26 +41,29 @@ public final class IdGeneratorConfigurer implements ApplicationListener<Applicat
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private volatile String generatorContextId;
|
||||
private static volatile String generatorContextId;
|
||||
|
||||
|
||||
public void onApplicationEvent(ApplicationContextEvent event) {
|
||||
ApplicationContext contex = event.getApplicationContext();
|
||||
if (event instanceof ContextRefreshedEvent){
|
||||
if (!StringUtils.hasText(generatorContextId)){
|
||||
ApplicationContext contex = event.getApplicationContext();
|
||||
if (!StringUtils.hasText(IdGeneratorConfigurer.generatorContextId)){
|
||||
|
||||
if (this.setIdGenerator(contex)){
|
||||
this.generatorContextId = contex.getId();
|
||||
IdGeneratorConfigurer.generatorContextId = contex.getId();
|
||||
}
|
||||
}
|
||||
else if (contex.getBeanNamesForType(IdGenerator.class).length > 0) {
|
||||
throw new BeanDefinitionStoreException(
|
||||
"'MessageHeaders.idGenerator' has already been set and can not be set again");
|
||||
}
|
||||
}
|
||||
else if (event instanceof ContextClosedEvent){
|
||||
ApplicationContext contex = event.getApplicationContext();
|
||||
if (contex.getId().equals(generatorContextId)){
|
||||
if (contex.getId().equals(IdGeneratorConfigurer.generatorContextId)){
|
||||
this.unsetIdGenerator();
|
||||
this.generatorContextId = null;
|
||||
IdGeneratorConfigurer.generatorContextId = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private boolean setIdGenerator(ApplicationContext context) {
|
||||
@@ -71,16 +74,11 @@ public final class IdGeneratorConfigurer implements ApplicationListener<Applicat
|
||||
}
|
||||
Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator");
|
||||
ReflectionUtils.makeAccessible(idGeneratorField);
|
||||
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Message IDs will be generated using custom IdGenerator [" + idGeneratorBean.getClass() + "]");
|
||||
}
|
||||
ReflectionUtils.setField(idGeneratorField, null, idGeneratorBean);
|
||||
}
|
||||
catch (BeanDefinitionStoreException e) {
|
||||
// let this one propagate
|
||||
throw e;
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException e) {
|
||||
// We will use the default.
|
||||
if (logger.isDebugEnabled()) {
|
||||
|
||||
@@ -25,8 +25,9 @@ import java.util.UUID;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.MessageHeaders;
|
||||
import org.springframework.integration.MessageHeaders.IdGenerator;
|
||||
@@ -43,7 +44,7 @@ public class MessageIdGenerationTests {
|
||||
|
||||
@Test
|
||||
public void testCustomIdGenerationWithParentRegistrar(){
|
||||
ApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-a.xml", this.getClass());
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass());
|
||||
ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context.xml"}, this.getClass(), parent);
|
||||
|
||||
IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
|
||||
@@ -54,11 +55,31 @@ public class MessageIdGenerationTests {
|
||||
child.close();
|
||||
new GenericMessage<Integer>(0);
|
||||
verify(idGenerator, times(1)).generateId();
|
||||
parent.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomIdGenerationWithParentChileIndependentCreation(){
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass());
|
||||
GenericXmlApplicationContext child = new GenericXmlApplicationContext();
|
||||
child.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context.xml");
|
||||
child.setParent(parent);
|
||||
child.refresh();
|
||||
|
||||
IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
|
||||
MessageChannel inputChannel = child.getBean("input", MessageChannel.class);
|
||||
inputChannel.send(new GenericMessage<Integer>(0));
|
||||
verify(idGenerator, times(4)).generateId();
|
||||
reset(idGenerator);
|
||||
child.close();
|
||||
new GenericMessage<Integer>(0);
|
||||
verify(idGenerator, times(1)).generateId();
|
||||
parent.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomIdGenerationWithParentRegistrarClosed(){
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-a.xml", this.getClass());
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass());
|
||||
ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context.xml"}, this.getClass(), parent);
|
||||
|
||||
IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
|
||||
@@ -69,12 +90,13 @@ public class MessageIdGenerationTests {
|
||||
parent.close();
|
||||
new GenericMessage<Integer>(0);
|
||||
verify(idGenerator, times(0)).generateId();
|
||||
parent.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomIdGenerationWithChildRegistrar(){
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass());
|
||||
ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-a.xml"}, this.getClass(), parent);
|
||||
ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-withGenerator.xml"}, this.getClass(), parent);
|
||||
|
||||
IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
|
||||
MessageChannel inputChannel = child.getBean("input", MessageChannel.class);
|
||||
@@ -84,12 +106,13 @@ public class MessageIdGenerationTests {
|
||||
parent.close();
|
||||
new GenericMessage<Integer>(0);
|
||||
verify(idGenerator, times(1)).generateId();
|
||||
child.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomIdGenerationWithChildRegistrarClosed(){
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass());
|
||||
ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-a.xml"}, this.getClass(), parent);
|
||||
ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"MessageIdGenerationTests-context-withGenerator.xml"}, this.getClass(), parent);
|
||||
|
||||
IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
|
||||
MessageChannel inputChannel = child.getBean("input", MessageChannel.class);
|
||||
@@ -99,9 +122,18 @@ public class MessageIdGenerationTests {
|
||||
child.close();
|
||||
new GenericMessage<Integer>(0);
|
||||
verify(idGenerator, times(0)).generateId();
|
||||
parent.close();
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=BeanDefinitionStoreException.class)
|
||||
public void testCustomIdGenerationWithParentChileIndependentCreationTwoChildrenTwoRegistrars(){
|
||||
ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass());
|
||||
|
||||
GenericXmlApplicationContext childA = new GenericXmlApplicationContext();
|
||||
childA.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml");
|
||||
childA.setParent(parent);
|
||||
childA.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
|
||||
Reference in New Issue
Block a user