Search in sources :

Example 1 with AbstractMessageProducingHandler

use of org.springframework.integration.handler.AbstractMessageProducingHandler in project spring-integration by spring-projects.

the class AbstractMethodAnnotationPostProcessor method postProcess.

@Override
public Object postProcess(Object bean, String beanName, Method method, List<Annotation> annotations) {
    if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
        try {
            resolveTargetBeanFromMethodWithBeanAnnotation(method);
        } catch (NoSuchBeanDefinitionException e) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Skipping endpoint creation; " + e.getMessage() + "; perhaps due to some '@Conditional' annotation.");
            }
            return null;
        }
    }
    List<Advice> adviceChain = extractAdviceChain(beanName, annotations);
    MessageHandler handler = createHandler(bean, method, annotations);
    if (!CollectionUtils.isEmpty(adviceChain) && handler instanceof AbstractReplyProducingMessageHandler) {
        ((AbstractReplyProducingMessageHandler) handler).setAdviceChain(adviceChain);
    }
    if (handler instanceof Orderable) {
        Order orderAnnotation = AnnotationUtils.findAnnotation(method, Order.class);
        if (orderAnnotation != null) {
            ((Orderable) handler).setOrder(orderAnnotation.value());
        }
    }
    if (handler instanceof AbstractMessageProducingHandler || handler instanceof AbstractMessageRouter) {
        String sendTimeout = MessagingAnnotationUtils.resolveAttribute(annotations, "sendTimeout", String.class);
        if (sendTimeout != null) {
            Long value = Long.valueOf(this.beanFactory.resolveEmbeddedValue(sendTimeout));
            if (handler instanceof AbstractMessageProducingHandler) {
                ((AbstractMessageProducingHandler) handler).setSendTimeout(value);
            } else {
                ((AbstractMessageRouter) handler).setSendTimeout(value);
            }
        }
    }
    boolean handlerExists = false;
    if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
        Object handlerBean = this.resolveTargetBeanFromMethodWithBeanAnnotation(method);
        handlerExists = handlerBean != null && handler == handlerBean;
    }
    if (!handlerExists) {
        String handlerBeanName = generateHandlerBeanName(beanName, method);
        if (handler instanceof ReplyProducingMessageHandlerWrapper && StringUtils.hasText(MessagingAnnotationUtils.endpointIdValue(method))) {
            handlerBeanName = handlerBeanName + ".wrapper";
        }
        this.beanFactory.registerSingleton(handlerBeanName, handler);
        handler = (MessageHandler) this.beanFactory.initializeBean(handler, handlerBeanName);
    }
    if (AnnotatedElementUtils.isAnnotated(method, IdempotentReceiver.class.getName()) && !AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
        String[] interceptors = AnnotationUtils.getAnnotation(method, IdempotentReceiver.class).value();
        for (String interceptor : interceptors) {
            DefaultBeanFactoryPointcutAdvisor advisor = new DefaultBeanFactoryPointcutAdvisor();
            advisor.setAdviceBeanName(interceptor);
            NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
            pointcut.setMappedName("handleMessage");
            advisor.setPointcut(pointcut);
            advisor.setBeanFactory(this.beanFactory);
            if (handler instanceof Advised) {
                ((Advised) handler).addAdvisor(advisor);
            } else {
                ProxyFactory proxyFactory = new ProxyFactory(handler);
                proxyFactory.addAdvisor(advisor);
                handler = (MessageHandler) proxyFactory.getProxy(this.beanFactory.getBeanClassLoader());
            }
        }
    }
    if (!CollectionUtils.isEmpty(adviceChain)) {
        for (Advice advice : adviceChain) {
            if (advice instanceof HandleMessageAdvice) {
                NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(advice);
                handlerAdvice.addMethodName("handleMessage");
                if (handler instanceof Advised) {
                    ((Advised) handler).addAdvisor(handlerAdvice);
                } else {
                    ProxyFactory proxyFactory = new ProxyFactory(handler);
                    proxyFactory.addAdvisor(handlerAdvice);
                    handler = (MessageHandler) proxyFactory.getProxy(this.beanFactory.getBeanClassLoader());
                }
            }
        }
    }
    AbstractEndpoint endpoint = createEndpoint(handler, method, annotations);
    if (endpoint != null) {
        return endpoint;
    }
    return handler;
}
Also used : Order(org.springframework.core.annotation.Order) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice) MessageHandler(org.springframework.messaging.MessageHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AbstractMessageRouter(org.springframework.integration.router.AbstractMessageRouter) Orderable(org.springframework.integration.context.Orderable) Bean(org.springframework.context.annotation.Bean) DefaultBeanFactoryPointcutAdvisor(org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor) Advised(org.springframework.aop.framework.Advised) NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice) Advice(org.aopalliance.aop.Advice) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) IdempotentReceiver(org.springframework.integration.annotation.IdempotentReceiver) ReplyProducingMessageHandlerWrapper(org.springframework.integration.handler.ReplyProducingMessageHandlerWrapper) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut)

Example 2 with AbstractMessageProducingHandler

use of org.springframework.integration.handler.AbstractMessageProducingHandler 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 3 with AbstractMessageProducingHandler

use of org.springframework.integration.handler.AbstractMessageProducingHandler in project spring-integration by spring-projects.

the class AbstractStandardMessageHandlerFactoryBean method createHandler.

@Override
protected MessageHandler createHandler() {
    MessageHandler handler;
    if (this.targetObject == null) {
        Assert.isTrue(!StringUtils.hasText(this.targetMethodName), "The target method is only allowed when a target object (ref or inner bean) is also provided.");
    }
    if (this.targetObject != null) {
        Assert.state(this.expression == null, "The 'targetObject' and 'expression' properties are mutually exclusive.");
        AbstractMessageProducingHandler actualHandler = this.extractTypeIfPossible(this.targetObject, AbstractMessageProducingHandler.class);
        boolean targetIsDirectReplyProducingHandler = actualHandler != null && // give subclasses a say
        canBeUsedDirect(actualHandler) && methodIsHandleMessageOrEmpty(this.targetMethodName);
        if (this.targetObject instanceof MessageProcessor<?>) {
            handler = this.createMessageProcessingHandler((MessageProcessor<?>) this.targetObject);
        } else if (targetIsDirectReplyProducingHandler) {
            if (logger.isDebugEnabled()) {
                logger.debug("Wiring handler (" + this.targetObject + ") directly into endpoint");
            }
            checkReuse(actualHandler);
            postProcessReplyProducer(actualHandler);
            handler = (MessageHandler) this.targetObject;
        } else {
            handler = this.createMethodInvokingHandler(this.targetObject, this.targetMethodName);
        }
    } else if (this.expression != null) {
        handler = this.createExpressionEvaluatingHandler(this.expression);
    } else {
        handler = this.createDefaultHandler();
    }
    return handler;
}
Also used : AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) MessageProcessor(org.springframework.integration.handler.MessageProcessor) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler)

Example 4 with AbstractMessageProducingHandler

use of org.springframework.integration.handler.AbstractMessageProducingHandler in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method registerOutputChannelIfCan.

private B registerOutputChannelIfCan(MessageChannel outputChannel) {
    if (!(outputChannel instanceof FixedSubscriberChannelPrototype)) {
        this.integrationComponents.put(outputChannel, null);
        if (this.currentComponent != null) {
            String channelName = null;
            if (outputChannel instanceof MessageChannelReference) {
                channelName = ((MessageChannelReference) outputChannel).getName();
            }
            Object currentComponent = this.currentComponent;
            if (AopUtils.isAopProxy(currentComponent)) {
                currentComponent = extractProxyTarget(currentComponent);
            }
            if (currentComponent instanceof MessageProducer) {
                MessageProducer messageProducer = (MessageProducer) currentComponent;
                checkReuse(messageProducer);
                if (channelName != null) {
                    if (messageProducer instanceof AbstractMessageProducingHandler) {
                        ((AbstractMessageProducingHandler) messageProducer).setOutputChannelName(channelName);
                    } else {
                        throw new BeanCreationException("The 'currentComponent' (" + currentComponent + ") must extend 'AbstractMessageProducingHandler' " + "for message channel resolution by name.\n" + "Your handler should extend 'AbstractMessageProducingHandler', " + "its subclass 'AbstractReplyProducingMessageHandler', or you should " + "reference a 'MessageChannel' bean instead of its name.");
                    }
                } else {
                    messageProducer.setOutputChannel(outputChannel);
                }
            } else if (currentComponent instanceof SourcePollingChannelAdapterSpec) {
                SourcePollingChannelAdapterFactoryBean pollingChannelAdapterFactoryBean = ((SourcePollingChannelAdapterSpec) currentComponent).get().getT1();
                if (channelName != null) {
                    pollingChannelAdapterFactoryBean.setOutputChannelName(channelName);
                } else {
                    pollingChannelAdapterFactoryBean.setOutputChannel(outputChannel);
                }
            } else {
                throw new BeanCreationException("The 'currentComponent' (" + currentComponent + ") is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. " + "This is the end of the integration flow.");
            }
            this.currentComponent = null;
        }
    }
    return _this();
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MessageChannelReference(org.springframework.integration.dsl.support.MessageChannelReference) FixedSubscriberChannelPrototype(org.springframework.integration.dsl.support.FixedSubscriberChannelPrototype) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) MessageProducer(org.springframework.integration.core.MessageProducer)

Example 5 with AbstractMessageProducingHandler

use of org.springframework.integration.handler.AbstractMessageProducingHandler in project spring-integration by spring-projects.

the class IntegrationMBeanExporter method afterSingletonsInstantiated.

@Override
public void afterSingletonsInstantiated() {
    Map<String, MessageHandlerMetrics> messageHandlers = this.applicationContext.getBeansOfType(MessageHandlerMetrics.class);
    for (Entry<String, MessageHandlerMetrics> entry : messageHandlers.entrySet()) {
        String beanName = entry.getKey();
        MessageHandlerMetrics bean = entry.getValue();
        if (this.handlerInAnonymousWrapper(bean) != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Skipping " + beanName + " because it wraps another handler");
            }
            continue;
        }
        // If the handler is proxied, we have to extract the target to expose as an MBean.
        // The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
        MessageHandlerMetrics monitor = (MessageHandlerMetrics) extractTarget(bean);
        this.handlers.add(monitor);
    }
    Map<String, MessageSourceMetrics> messageSources = this.applicationContext.getBeansOfType(MessageSourceMetrics.class);
    for (Entry<String, MessageSourceMetrics> entry : messageSources.entrySet()) {
        // If the source is proxied, we have to extract the target to expose as an MBean.
        // The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
        MessageSourceMetrics monitor = (MessageSourceMetrics) extractTarget(entry.getValue());
        this.sources.add(monitor);
    }
    Map<String, MessageChannelMetrics> messageChannels = this.applicationContext.getBeansOfType(MessageChannelMetrics.class);
    for (Entry<String, MessageChannelMetrics> entry : messageChannels.entrySet()) {
        // If the channel is proxied, we have to extract the target to expose as an MBean.
        // The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
        MessageChannelMetrics monitor = (MessageChannelMetrics) extractTarget(entry.getValue());
        this.channels.add(monitor);
    }
    Map<String, MessageProducer> messageProducers = this.applicationContext.getBeansOfType(MessageProducer.class);
    for (Entry<String, MessageProducer> entry : messageProducers.entrySet()) {
        MessageProducer messageProducer = entry.getValue();
        if (messageProducer instanceof Lifecycle) {
            Lifecycle target = (Lifecycle) extractTarget(messageProducer);
            if (!(target instanceof AbstractMessageProducingHandler)) {
                this.inboundLifecycleMessageProducers.add(target);
            }
        }
    }
    super.afterSingletonsInstantiated();
    try {
        registerChannels();
        registerHandlers();
        registerSources();
        registerEndpoints();
        if (this.applicationContext.containsBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME)) {
            Object messageHistoryConfigurer = this.applicationContext.getBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
            if (messageHistoryConfigurer instanceof MessageHistoryConfigurer) {
                registerBeanInstance(messageHistoryConfigurer, IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
            }
        }
        if (!this.applicationContext.containsBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)) {
            this.managementConfigurer = new IntegrationManagementConfigurer();
            this.managementConfigurer.setDefaultCountsEnabled(true);
            this.managementConfigurer.setDefaultStatsEnabled(true);
            this.managementConfigurer.setApplicationContext(this.applicationContext);
            this.managementConfigurer.setBeanName(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
            this.managementConfigurer.afterSingletonsInstantiated();
        } else {
            this.managementConfigurer = this.applicationContext.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME, IntegrationManagementConfigurer.class);
        }
    } catch (RuntimeException e) {
        unregisterBeans();
        throw e;
    }
}
Also used : IntegrationManagementConfigurer(org.springframework.integration.config.IntegrationManagementConfigurer) MessageChannelMetrics(org.springframework.integration.support.management.MessageChannelMetrics) MessageSourceMetrics(org.springframework.integration.support.management.MessageSourceMetrics) LifecycleTrackableMessageSourceMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageSourceMetrics) LifecycleMessageSourceMetrics(org.springframework.integration.support.management.LifecycleMessageSourceMetrics) MessageHistoryConfigurer(org.springframework.integration.history.MessageHistoryConfigurer) Lifecycle(org.springframework.context.Lifecycle) LifecycleTrackableMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics) LifecycleMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleMessageHandlerMetrics) MessageHandlerMetrics(org.springframework.integration.support.management.MessageHandlerMetrics) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) MessageProducer(org.springframework.integration.core.MessageProducer)

Aggregations

AbstractMessageProducingHandler (org.springframework.integration.handler.AbstractMessageProducingHandler)5 MessageProducer (org.springframework.integration.core.MessageProducer)3 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)3 Orderable (org.springframework.integration.context.Orderable)2 MessageHandler (org.springframework.messaging.MessageHandler)2 Advice (org.aopalliance.aop.Advice)1 Advised (org.springframework.aop.framework.Advised)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 DefaultBeanFactoryPointcutAdvisor (org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor)1 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)1 NameMatchMethodPointcutAdvisor (org.springframework.aop.support.NameMatchMethodPointcutAdvisor)1 BeansException (org.springframework.beans.BeansException)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)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 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ApplicationContextAware (org.springframework.context.ApplicationContextAware)1 ApplicationEventPublisherAware (org.springframework.context.ApplicationEventPublisherAware)1