Search in sources :

Example 6 with IdGenerator

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;
}
Also used : Field(java.lang.reflect.Field) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) IdGenerator(org.springframework.util.IdGenerator) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 7 with IdGenerator

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());
}
Also used : IdGenerator(org.springframework.util.IdGenerator)

Example 8 with IdGenerator

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());
}
Also used : IdGenerator(org.springframework.util.IdGenerator) UUID(java.util.UUID) Test(org.junit.Test)

Example 9 with IdGenerator

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();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) IdGenerator(org.springframework.util.IdGenerator) Test(org.junit.Test)

Example 10 with IdGenerator

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();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) IdGenerator(org.springframework.util.IdGenerator) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) Test(org.junit.Test)

Aggregations

IdGenerator (org.springframework.util.IdGenerator)10 Test (org.junit.Test)8 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 MessageChannel (org.springframework.messaging.MessageChannel)5 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 JdkIdGenerator (org.springframework.integration.support.IdGenerators.JdkIdGenerator)2 SimpleIncrementingIdGenerator (org.springframework.integration.support.IdGenerators.SimpleIncrementingIdGenerator)2 MessageHeaders (org.springframework.messaging.MessageHeaders)2 Field (java.lang.reflect.Field)1 UUID (java.util.UUID)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 GenericXmlApplicationContext (org.springframework.context.support.GenericXmlApplicationContext)1