Search in sources :

Example 1 with IntegrationObjectSupport

use of org.springframework.integration.context.IntegrationObjectSupport in project spring-integration by spring-projects.

the class AbstractSimpleMessageHandlerFactoryBean method createHandlerInternal.

protected final H createHandlerInternal() {
    synchronized (this.initializationMonitor) {
        if (this.initialized) {
            // There was a problem when this method was called already
            return null;
        }
        this.handler = createHandler();
        if (this.handler instanceof ApplicationContextAware && this.applicationContext != null) {
            ((ApplicationContextAware) this.handler).setApplicationContext(this.applicationContext);
        }
        if (this.handler instanceof BeanFactoryAware && getBeanFactory() != null) {
            ((BeanFactoryAware) this.handler).setBeanFactory(getBeanFactory());
        }
        if (this.handler instanceof BeanNameAware && this.beanName != null) {
            ((BeanNameAware) this.handler).setBeanName(this.beanName);
        }
        if (this.handler instanceof ApplicationEventPublisherAware && this.applicationEventPublisher != null) {
            ((ApplicationEventPublisherAware) this.handler).setApplicationEventPublisher(this.applicationEventPublisher);
        }
        if (this.handler instanceof MessageProducer && this.outputChannel != null) {
            ((MessageProducer) this.handler).setOutputChannel(this.outputChannel);
        }
        Object actualHandler = extractTarget(this.handler);
        if (actualHandler == null) {
            actualHandler = this.handler;
        }
        if (actualHandler instanceof IntegrationObjectSupport) {
            if (this.componentName != null) {
                ((IntegrationObjectSupport) actualHandler).setComponentName(this.componentName);
            }
            if (this.channelResolver != null) {
                ((IntegrationObjectSupport) actualHandler).setChannelResolver(this.channelResolver);
            }
        }
        if (!CollectionUtils.isEmpty(this.adviceChain)) {
            if (actualHandler instanceof AbstractReplyProducingMessageHandler) {
                ((AbstractReplyProducingMessageHandler) actualHandler).setAdviceChain(this.adviceChain);
            } else if (this.logger.isDebugEnabled()) {
                String name = this.componentName;
                if (name == null && actualHandler instanceof NamedComponent) {
                    name = ((NamedComponent) actualHandler).getComponentName();
                }
                this.logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler" + (name == null ? "" : (", " + name)) + ".");
            }
        }
        if (this.async != null) {
            if (actualHandler instanceof AbstractMessageProducingHandler) {
                ((AbstractMessageProducingHandler) actualHandler).setAsync(this.async);
            }
        }
        if (this.handler instanceof Orderable && this.order != null) {
            ((Orderable) this.handler).setOrder(this.order);
        }
        this.initialized = true;
    }
    if (this.handler instanceof InitializingBean) {
        try {
            ((InitializingBean) this.handler).afterPropertiesSet();
        } catch (Exception e) {
            throw new BeanInitializationException("failed to initialize MessageHandler", e);
        }
    }
    return this.handler;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) ApplicationContextAware(org.springframework.context.ApplicationContextAware) IntegrationObjectSupport(org.springframework.integration.context.IntegrationObjectSupport) Orderable(org.springframework.integration.context.Orderable) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) NamedComponent(org.springframework.integration.support.context.NamedComponent) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeansException(org.springframework.beans.BeansException) BeanNameAware(org.springframework.beans.factory.BeanNameAware) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageProducer(org.springframework.integration.core.MessageProducer) InitializingBean(org.springframework.beans.factory.InitializingBean)

Example 2 with IntegrationObjectSupport

use of org.springframework.integration.context.IntegrationObjectSupport in project spring-integration by spring-projects.

the class ConsumerEndpointFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    if (this.beanName == null) {
        logger.error("The MessageHandler [" + this.handler + "] will be created without a 'componentName'. " + "Consider specifying the 'beanName' property on this ConsumerEndpointFactoryBean.");
    } else {
        try {
            if (!this.beanName.startsWith("org.springframework")) {
                MessageHandler targetHandler = this.handler;
                if (AopUtils.isAopProxy(targetHandler)) {
                    Object target = ((Advised) targetHandler).getTargetSource().getTarget();
                    if (target instanceof MessageHandler) {
                        targetHandler = (MessageHandler) target;
                    }
                }
                if (targetHandler instanceof IntegrationObjectSupport) {
                    ((IntegrationObjectSupport) targetHandler).setComponentName(this.beanName);
                }
            }
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not set component name for handler " + this.handler + " for " + this.beanName + " :" + e.getMessage());
            }
        }
    }
    if (!CollectionUtils.isEmpty(this.adviceChain)) {
        /*
			 *  ARPMHs advise the handleRequestMessage method internally and already have the advice chain injected.
			 *  So we only advise handlers that are not reply-producing.
			 *  Or if one (or more) of advices is IdempotentReceiverInterceptor.
			 *  If the handler is already advised,
			 *  add the configured advices to its chain, otherwise create a proxy.
			 */
        Class<?> targetClass = AopUtils.getTargetClass(this.handler);
        boolean replyMessageHandler = AbstractReplyProducingMessageHandler.class.isAssignableFrom(targetClass);
        for (Advice advice : this.adviceChain) {
            if (!replyMessageHandler || advice instanceof HandleMessageAdvice) {
                NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(advice);
                handlerAdvice.addMethodName("handleMessage");
                if (this.handler instanceof Advised) {
                    ((Advised) this.handler).addAdvisor(handlerAdvice);
                } else {
                    ProxyFactory proxyFactory = new ProxyFactory(this.handler);
                    proxyFactory.addAdvisor(handlerAdvice);
                    this.handler = (MessageHandler) proxyFactory.getProxy(this.beanClassLoader);
                }
            }
        }
    }
    if (this.channelResolver == null) {
        this.channelResolver = new BeanFactoryMessageChannelDestinationResolver(this.beanFactory);
    }
    initializeEndpoint();
}
Also used : HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) IntegrationObjectSupport(org.springframework.integration.context.IntegrationObjectSupport) Advised(org.springframework.aop.framework.Advised) ProxyFactory(org.springframework.aop.framework.ProxyFactory) BeanFactoryMessageChannelDestinationResolver(org.springframework.messaging.core.BeanFactoryMessageChannelDestinationResolver) NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor) Advice(org.aopalliance.aop.Advice) HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice)

Aggregations

IntegrationObjectSupport (org.springframework.integration.context.IntegrationObjectSupport)2 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)2 Advice (org.aopalliance.aop.Advice)1 Advised (org.springframework.aop.framework.Advised)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 NameMatchMethodPointcutAdvisor (org.springframework.aop.support.NameMatchMethodPointcutAdvisor)1 BeansException (org.springframework.beans.BeansException)1 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 BeanNameAware (org.springframework.beans.factory.BeanNameAware)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 ApplicationContextAware (org.springframework.context.ApplicationContextAware)1 ApplicationEventPublisherAware (org.springframework.context.ApplicationEventPublisherAware)1 Orderable (org.springframework.integration.context.Orderable)1 MessageProducer (org.springframework.integration.core.MessageProducer)1 AbstractMessageProducingHandler (org.springframework.integration.handler.AbstractMessageProducingHandler)1 HandleMessageAdvice (org.springframework.integration.handler.advice.HandleMessageAdvice)1 NamedComponent (org.springframework.integration.support.context.NamedComponent)1 MessageHandler (org.springframework.messaging.MessageHandler)1 BeanFactoryMessageChannelDestinationResolver (org.springframework.messaging.core.BeanFactoryMessageChannelDestinationResolver)1