Spring集成:无法将消息发送到频道

时间:2017-03-07 04:20:05

标签: java spring spring-integration activemq spring-jms

我正在使用spring-frameworkspring-integration最新版本,只是使用spring集成制作POC应用程序,但无法运行它。

我有春天整合这样的东西。

    <bean id="initUrlQ" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="initUrl.Q" />
    </bean> 
   <int:channel id="initChannelProducerId" />

    <jms:outbound-channel-adapter id="initOutboundId" channel="initChannelProducerId" connection-factory="padtoys.jms.cachedConnectionFactory"
        destination="initUrlQ"  />
    <int:channel id="initChannelConsumerId" />
    <jms:message-driven-channel-adapter id="initQListenerId" channel="initChannelConsumerId"
        connection-factory="padtoys.jms.cachedConnectionFactory" destination="initUrlQ"
        acknowledge="auto" concurrent-consumers="1" max-concurrent-consumers="1" />  

    <int:service-activator method="testSA" ref="testBean" input-channel="initChannelConsumerId" output-channel="nullChannel"/>

我的testBean java文件看起来像这样。

public class TestBean implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @PostConstruct
    public void afterInit(){
        System.err.println("after init..!");
        DirectChannel inChannel = applicationContext.getBean("initChannelProducerId", DirectChannel.class);
        System.err.println("channel::" + inChannel);
        inChannel.send(createMessage("This is test url!!", 0));
    }

    @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @ServiceActivator
    public String testSA(){
        System.err.println("inside service activator!!!");
        return "this is test";
    }

    private Message<String> createMessage(final String url, final int depth) {
        return MessageBuilder.withPayload(StringUtils.EMPTY)
                .setHeader(MESSAGE_HEADERS.URL, url)
                .setHeader(MESSAGE_HEADERS.DEPTH, depth).build();
    }
} 

我正在开始这样的应用程序。

@SuppressWarnings("resource") public static void main(String[] args) {
        LOG.info("Trigerring process!");
        new ClassPathXmlApplicationContext("/application-context.xml");
    }

请注意我已在spring上下文xml中声明testBeanpadtoys.jms.cachedConnectionFactory

问题:

当我启动应用程序时,低于错误

Caused by: org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'org.springframework.context.support.ClassPathXmlApplicationContext@5a1198c7.initChannelProducerId'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
        at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
        at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:423)
        at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:373)
        at com.dodax.pad.ext.toys.crawler.scanner.TestBean.afterInit(TestBean.java:25)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
        ... 30 more
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
        at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:138)
        at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
        at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)

注意:

我在这一行获得正确的频道名称, System.err.println("channel::" + inChannel);,但是当我尝试将消息发送到频道时问题就开始了。

1 个答案:

答案 0 :(得分:1)

您不能在send()方法中执行消息传递(@PostConstruct) - 它在应用程序上下文生命周期中为时尚早;上下文尚未准备好执行消息传递。

有关在启动期间执行消息传递的正确位置,请参阅this answer