Spring Integration 没有为端点定义轮询器

Posted

技术标签:

【中文标题】Spring Integration 没有为端点定义轮询器【英文标题】:Spring Integration No poller has been defined for endpoint 【发布时间】:2014-01-14 23:44:27 【问题描述】:

您好,我很难解决我的 xml 配置问题,

这是我的 Spring 集成配置 xml:

<context:annotation-config />
    <context:component-scan base-package="hk.com.test.spring.integration" />

    <int:channel id="orders" />
    <int:channel id="drinks" />

    <int:channel id="hotDrink">
        <int:queue capacity="5" />
    </int:channel>

    <int:channel id="coldDrink">
        <int:queue capacity="10" />
    </int:channel>

    <bean id="drinkRouter" class="hk.com.test.spring.integration.DrinkRouter" />
    <bean id="orderSplitter" class="hk.com.test.spring.integration.OrderSplitter" />
    <bean id="barista" class="hk.com.test.spring.integration.Barista" />

    <int:gateway id="cafe" service-interface="hk.com.test.spring.integration.Cafe" />

    <int:splitter input-channel="orders" ref="orderSplitter"
        method="split" output-channel="drinks" />

    <int:router input-channel="drinks" ref="drinkRouter" method="resolveItemChannel" />


    <int:service-activator input-channel="coldDrink"
        ref="barista" method="prepareColdDrink" />

    <int:service-activator input-channel="hotDrink"
        ref="barista" method="preparehotDrink" />

这是我的主要课程::

public class Main 

    public static void main(String args[]) 
        System.out.println("Hello");

        // load the Spring context
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "spring-config2.xml");
        Cafe cafe = (Cafe) context.getBean("cafe");
        for (int i = 1; i <= 100; i++) 
            Order order = new Order(i);
            order.addItem(DrinkType.LATTE, false);
            order.addItem(DrinkType.MOCHA, true);
            cafe.placeOrder(order);
        

    

我只是简单地使用一个主类运行它,我收到了这个异常::

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.config.ConsumerEndpointFactoryBean#2': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No poller has been defined for endpoint 'org.springframework.integration.config.ConsumerEndpointFactoryBean#2', and no default poller is available within the context.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:589)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at hk.com.novare.spring.integration.main.Main.main(Main.java:16)
Caused by: java.lang.IllegalArgumentException: No poller has been defined for endpoint 'org.springframework.integration.config.ConsumerEndpointFactoryBean#2', and no default poller is available within the context.
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.integration.config.ConsumerEndpointFactoryBean.initializeEndpoint(ConsumerEndpointFactoryBean.java:220)
    at org.springframework.integration.config.ConsumerEndpointFactoryBean.afterPropertiesSet(ConsumerEndpointFactoryBean.java:175)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    ... 12 more

我刚开始学习 Spring 集成,我也不知道如何修复我的配置..

【问题讨论】:

【参考方案1】:

用于定义全局默认池的注解替代方案:

@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata defaultPoller() 

    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(10));
    return pollerMetadata;

【讨论】:

【参考方案2】:

您有多个queue 频道。要从他们那里接收消息,您应该配置&lt;poller&gt;:全局一个,或者为订阅这些队列的每个组件配置:Poller Configuration,Poller sample

更新:

全局轮询器:

<int:poller default="true" fixed-delay="50"/>

队列通道是分离器、路由器和服务激活器,对吧?

不,频道就是频道,当然,它们不会对消息做任何事情。消息由Endpoints 从频道获取。在queue 的情况下,它应该是PollingConsumer,并且异常说明了这一点。因此,您必须:或添加全局轮询器,但在这种情况下,所有端点都将通过相同的规则轮询消息,或者为每个端点配置 &lt;poller&gt;。在您的情况下,它们是具有input-channel 的那些队列的组件。

【讨论】:

嗨,Artem,感谢您的回复,您能否指导我如何添加或配置全局轮询器...另一个问题,如果我错了,请纠正我,队列通道是拆分器、路由器和服务激活器正确的?如果是这样,每个频道都需要有自己的投票吗? 我明白了,所以所有队列通道都是那些有输入通道的。非常感谢 Artem 先生。我还有一个问题, 它给了我一个错误-> cvc-complex-type.3.2.2: Attribute 'fixed-delay' is not allowed出现在元素“int:poller”中。 Lools 喜欢你在 Spring Integration 上使用非常旧的版本:projects.spring.io/spring-integration ... 感谢 Artem 先生,我目前正在使用 2.2.3.RELEASE,哈哈,我更改了我的版本,然后在我让它工作后更新你 假设您的 IDE 抱怨该属性,或者您在运行时的 CLASSPATH 中有多个版本。【参考方案3】:

问题通过以下步骤解决-

    在 Eclipse 中右键单击您的项目。 然后是 Maven > 更新项目。

注意:确保您也有 xsd 和 jar。

<int-stream:stdin-channel-adapter id="producer" channel="messageChannel" >
    <int:poller id="defaultPoller" default="true" fixed-rate="200" />
</int-stream:stdin-channel-adapter>

【讨论】:

以上是关于Spring Integration 没有为端点定义轮询器的主要内容,如果未能解决你的问题,请参考以下文章

使用 Spring Security 保护 REST 端点

Spring Boot 应用程序端点返回 403

Spring Integration Dispatcher 没有频道订阅者

Spring Integration 没有输出通道或回复通道

Spring Integration JMS Inbound Gateway 回复通道没有订阅者

Spring-Integration:将 DirectChannel 更改为 ExecutorChannel 结果为 ClassCastException