Search in sources :

Example 1 with AbstractEndpoint

use of org.springframework.integration.endpoint.AbstractEndpoint in project spring-integration by spring-projects.

the class MessagingGatewaySupport method registerReplyMessageCorrelatorIfNecessary.

protected void registerReplyMessageCorrelatorIfNecessary() {
    MessageChannel replyChannel = getReplyChannel();
    if (replyChannel != null && this.replyMessageCorrelator == null) {
        boolean shouldStartCorrelator;
        synchronized (this.replyMessageCorrelatorMonitor) {
            if (this.replyMessageCorrelator != null) {
                return;
            }
            AbstractEndpoint correlator;
            BridgeHandler handler = new BridgeHandler();
            if (getBeanFactory() != null) {
                handler.setBeanFactory(getBeanFactory());
            }
            handler.afterPropertiesSet();
            if (replyChannel instanceof SubscribableChannel) {
                correlator = new EventDrivenConsumer((SubscribableChannel) replyChannel, handler);
            } else if (replyChannel instanceof PollableChannel) {
                PollingConsumer endpoint = new PollingConsumer((PollableChannel) replyChannel, handler);
                endpoint.setBeanFactory(getBeanFactory());
                endpoint.setReceiveTimeout(this.replyTimeout);
                endpoint.afterPropertiesSet();
                correlator = endpoint;
            } else if (replyChannel instanceof ReactiveStreamsSubscribableChannel) {
                ReactiveStreamsConsumer endpoint = new ReactiveStreamsConsumer(replyChannel, (Subscriber<Message<?>>) handler);
                endpoint.afterPropertiesSet();
                correlator = endpoint;
            } else {
                throw new MessagingException("Unsupported 'replyChannel' type [" + replyChannel.getClass() + "]." + "SubscribableChannel or PollableChannel type are supported.");
            }
            this.replyMessageCorrelator = correlator;
            shouldStartCorrelator = true;
        }
        if (shouldStartCorrelator && isRunning()) {
            if (isRunning()) {
                this.replyMessageCorrelator.start();
            }
        }
    }
}
Also used : AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) MessageChannel(org.springframework.messaging.MessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) BridgeHandler(org.springframework.integration.handler.BridgeHandler) MessagingException(org.springframework.messaging.MessagingException) PollableChannel(org.springframework.messaging.PollableChannel) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel)

Example 2 with AbstractEndpoint

use of org.springframework.integration.endpoint.AbstractEndpoint in project spring-integration by spring-projects.

the class AbstractMethodAnnotationPostProcessor method doCreateEndpoint.

protected AbstractEndpoint doCreateEndpoint(MessageHandler handler, MessageChannel inputChannel, List<Annotation> annotations) {
    AbstractEndpoint endpoint;
    if (inputChannel instanceof PollableChannel) {
        PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) inputChannel, handler);
        configurePollingEndpoint(pollingConsumer, annotations);
        endpoint = pollingConsumer;
    } else {
        Poller[] pollers = MessagingAnnotationUtils.resolveAttribute(annotations, "poller", Poller[].class);
        Assert.state(ObjectUtils.isEmpty(pollers), "A '@Poller' should not be specified for Annotation-based " + "endpoint, since '" + inputChannel + "' is a SubscribableChannel (not pollable).");
        if (inputChannel instanceof Publisher) {
            endpoint = new ReactiveStreamsConsumer(inputChannel, handler);
        } else {
            endpoint = new EventDrivenConsumer((SubscribableChannel) inputChannel, handler);
        }
    }
    return endpoint;
}
Also used : AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) PollableChannel(org.springframework.messaging.PollableChannel) Publisher(org.reactivestreams.Publisher) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Poller(org.springframework.integration.annotation.Poller)

Example 3 with AbstractEndpoint

use of org.springframework.integration.endpoint.AbstractEndpoint 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 4 with AbstractEndpoint

use of org.springframework.integration.endpoint.AbstractEndpoint in project spring-integration by spring-projects.

the class MessagingAnnotationPostProcessorTests method serviceActivatorAnnotation.

@Test
public void serviceActivatorAnnotation() {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    DirectChannel inputChannel = new DirectChannel();
    context.registerChannel("inputChannel", inputChannel);
    context.refresh();
    MessagingAnnotationPostProcessor postProcessor = new MessagingAnnotationPostProcessor();
    postProcessor.setBeanFactory(context.getBeanFactory());
    postProcessor.afterPropertiesSet();
    ServiceActivatorAnnotatedBean bean = new ServiceActivatorAnnotatedBean();
    postProcessor.postProcessAfterInitialization(bean, "testBean");
    assertTrue(context.containsBean("testBean.test.serviceActivator"));
    Object endpoint = context.getBean("testBean.test.serviceActivator");
    assertTrue(endpoint instanceof AbstractEndpoint);
}
Also used : AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) DirectChannel(org.springframework.integration.channel.DirectChannel) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 5 with AbstractEndpoint

use of org.springframework.integration.endpoint.AbstractEndpoint in project spring-integration by spring-projects.

the class ChainParserTests method checkSmartLifecycleConfig.

// INT-2605
@Test
public void checkSmartLifecycleConfig() {
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("ChainParserSmartLifecycleAttributesTest.xml", this.getClass());
    AbstractEndpoint chainEndpoint = ctx.getBean("chain", AbstractEndpoint.class);
    assertEquals(false, chainEndpoint.isAutoStartup());
    assertEquals(256, chainEndpoint.getPhase());
    MessageHandlerChain handlerChain = ctx.getBean("chain.handler", MessageHandlerChain.class);
    assertEquals(3000L, TestUtils.getPropertyValue(handlerChain, "messagingTemplate.sendTimeout"));
    assertEquals(false, TestUtils.getPropertyValue(handlerChain, "running"));
    // INT-3108
    MessageHandler serviceActivator = ctx.getBean("chain$child.sa-within-chain.handler", MessageHandler.class);
    assertTrue(TestUtils.getPropertyValue(serviceActivator, "requiresReply", Boolean.class));
    ctx.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) MessageHandler(org.springframework.messaging.MessageHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MessageHandlerChain(org.springframework.integration.handler.MessageHandlerChain) Test(org.junit.Test)

Aggregations

AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)34 Test (org.junit.Test)28 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)19 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)7 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)6 MessageHandler (org.springframework.messaging.MessageHandler)5 Marshaller (org.springframework.oxm.Marshaller)4 Unmarshaller (org.springframework.oxm.Unmarshaller)4 WebServiceMessageFactory (org.springframework.ws.WebServiceMessageFactory)4 PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)3 WebServiceMessageCallback (org.springframework.ws.client.core.WebServiceMessageCallback)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 DirectChannel (org.springframework.integration.channel.DirectChannel)2 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)2 ReactiveStreamsConsumer (org.springframework.integration.endpoint.ReactiveStreamsConsumer)2 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)2 PollableChannel (org.springframework.messaging.PollableChannel)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 SourceExtractor (org.springframework.ws.client.core.SourceExtractor)2