Example usage for org.springframework.integration.config IdGeneratorConfigurer theIdGenerator

List of usage examples for org.springframework.integration.config IdGeneratorConfigurer theIdGenerator

Introduction

In this page you can find the example usage for org.springframework.integration.config IdGeneratorConfigurer theIdGenerator.

Prototype

IdGenerator theIdGenerator

To view the source code for org.springframework.integration.config IdGeneratorConfigurer theIdGenerator.

Click Source Link

Usage

From source file:org.springframework.integration.config.IdGeneratorConfigurer.java

private boolean setIdGenerator(ApplicationContext context) {
    try {/*from   ww  w.  ja v a 2  s.  c  o m*/
        IdGenerator idGeneratorBean = context.getBean(IdGenerator.class);
        if (logger.isDebugEnabled()) {
            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 (logger.isWarnEnabled()) {
                        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 (logger.isInfoEnabled()) {
            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 && logger.isWarnEnabled()) {
            logger.warn("Found too many 'IdGenerator' beans (" + idBeans + ") "
                    + "Will use the existing UUID strategy.");
        } else if (logger.isDebugEnabled()) {
            logger.debug("Unable to locate MessageHeaders.IdGenerator. Will use the existing UUID strategy.");
        }
        return false;
    } catch (IllegalStateException e) {
        // thrown from ReflectionUtils
        if (logger.isWarnEnabled()) {
            logger.warn("Unexpected exception occurred while accessing idGenerator of MessageHeaders."
                    + " Will use the existing UUID strategy.", e);
        }
        return false;
    }
    return true;
}

From source file:org.springframework.integration.config.IdGeneratorConfigurer.java

private void unsetIdGenerator() {
    try {//  w  w  w.  j a va 2  s . com
        Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator");
        ReflectionUtils.makeAccessible(idGeneratorField);
        idGeneratorField.set(null, null);
        IdGeneratorConfigurer.theIdGenerator = null;
    } catch (Exception e) {
        if (logger.isWarnEnabled()) {
            logger.warn("Unexpected exception occurred while accessing idGenerator of MessageHeaders.", e);
        }
    }
}