use of org.springframework.util.IdGenerator in project spring-integration by spring-projects.
the class IdGeneratorConfigurer method setIdGenerator.
private boolean setIdGenerator(ApplicationContext context) {
try {
IdGenerator idGeneratorBean = context.getBean(IdGenerator.class);
if (this.logger.isDebugEnabled()) {
this.logger.debug("using custom MessageHeaders.IdGenerator [" + idGeneratorBean.getClass() + "]");
}
Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator");
ReflectionUtils.makeAccessible(idGeneratorField);
IdGenerator currentIdGenerator = (IdGenerator) ReflectionUtils.getField(idGeneratorField, null);
if (currentIdGenerator != null) {
if (currentIdGenerator.equals(idGeneratorBean)) {
// same instance is already set, nothing needs to be done
return false;
} else {
if (IdGeneratorConfigurer.theIdGenerator.getClass() == idGeneratorBean.getClass()) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Another instance of " + idGeneratorBean.getClass() + " has already been established; ignoring");
}
return true;
} else {
// different instance has been set, not legal
throw new BeanDefinitionStoreException("'MessageHeaders.idGenerator' has already been set and can not be set again");
}
}
}
if (this.logger.isInfoEnabled()) {
this.logger.info("Message IDs will be generated using custom IdGenerator [" + idGeneratorBean.getClass() + "]");
}
ReflectionUtils.setField(idGeneratorField, null, idGeneratorBean);
IdGeneratorConfigurer.theIdGenerator = idGeneratorBean;
} catch (NoSuchBeanDefinitionException e) {
// No custom IdGenerator. We will use the default.
int idBeans = context.getBeansOfType(IdGenerator.class).size();
if (idBeans > 1 && this.logger.isWarnEnabled()) {
this.logger.warn("Found too many 'IdGenerator' beans (" + idBeans + ") " + "Will use the existing UUID strategy.");
} else if (this.logger.isDebugEnabled()) {
this.logger.debug("Unable to locate MessageHeaders.IdGenerator. Will use the existing UUID strategy.");
}
return false;
} catch (IllegalStateException e) {
// thrown from ReflectionUtils
if (this.logger.isWarnEnabled()) {
this.logger.warn("Unexpected exception occurred while accessing idGenerator of MessageHeaders." + " Will use the existing UUID strategy.", e);
}
return false;
}
return true;
}
use of org.springframework.util.IdGenerator in project spring-framework by spring-projects.
the class IdTimestampMessageHeaderInitializer method initHeaders.
@Override
public void initHeaders(MessageHeaderAccessor headerAccessor) {
IdGenerator idGenerator = getIdGenerator();
if (idGenerator != null) {
headerAccessor.setIdGenerator(idGenerator);
}
headerAccessor.setEnableTimestamp(isEnableTimestamp());
}
use of org.springframework.util.IdGenerator in project spring-framework by spring-projects.
the class MessageBuilderTests method testBuildMessageWithoutIdAndTimestamp.
@Test
public void testBuildMessageWithoutIdAndTimestamp() {
MessageHeaderAccessor headerAccessor = new MessageHeaderAccessor();
headerAccessor.setIdGenerator(new IdGenerator() {
@Override
public UUID generateId() {
return MessageHeaders.ID_VALUE_NONE;
}
});
Message<?> message = MessageBuilder.createMessage("foo", headerAccessor.getMessageHeaders());
assertNull(message.getHeaders().getId());
assertNull(message.getHeaders().getTimestamp());
}
use of org.springframework.util.IdGenerator in project spring-integration by spring-projects.
the class MessageIdGenerationTests method testCustomIdGenerationWithParentRegistrarClosed.
@Test
public void testCustomIdGenerationWithParentRegistrarClosed() throws Exception {
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);
MessageChannel inputChannel = child.getBean("input", MessageChannel.class);
inputChannel.send(new GenericMessage<Integer>(0));
verify(idGenerator, atLeastOnce()).generateId();
parent.close();
child.close();
this.assertDestroy();
}
use of org.springframework.util.IdGenerator in project spring-integration by spring-projects.
the class MessageIdGenerationTests method testCustomIdGenerationWithParentChildIndependentCreation.
@Test
public void testCustomIdGenerationWithParentChildIndependentCreation() throws Exception {
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, atLeastOnce()).generateId();
child.close();
parent.close();
this.assertDestroy();
}
Aggregations