在init-method中使用通道时,Spring集成'Dispatcher没有订户'

时间:2012-06-21 10:14:39

标签: spring-integration

在尝试将消息发布到spring bean的init-method中的通道时,获取'Dispatcher没有订阅者'错误。请看下面的例子:

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:rmi="http://www.springframework.org/schema/integration/rmi"
    xmlns:int="http://www.springframework.org/schema/integration"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration
        http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/rmi
        http://www.springframework.org/schema/integration/rmi/spring-integration-rmi.xsd">

    <bean id="currencyService" class="com.demo.CurrencyService" init-method="init"/>

    <int:channel id="currencyChannel" />
    <int:channel id="currencyReplyChannel">
        <int:queue/>
    </int:channel>
    <rmi:outbound-gateway id="currencyServiceGateway"
        request-channel="currencyChannel" remote-channel="currencyServiceChannel"
        reply-channel="currencyReplyChannel" host="localhost" port="2197" />
</beans>

Spring托管bean:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;

public class CurrencyService {

    @Autowired
    private MessageChannel currencyChannel;

    @Autowired
    private PollableChannel currencyReplyChannel;

    private CurrencyListBO currencyListBO;

    public CurrencyListBO getCurrencyList() {
        return currencyListBO;
    }

    public void init() {
        CurrencyIN request = new CurrencyIN();
        request.setChannelCode("RMW");
        request.setTransactionType(CurrencyIN.TransactionType.currencyLoaderService
                .toString());
        GenericMessage<IRequestBO> message = new GenericMessage<IRequestBO>(
                request);
        MessagingTemplate template = new MessagingTemplate();
        template.send(currencyChannel, message);
        Message<CurrencyListBO> reply = template.receive(currencyReplyChannel);
        currencyListBO = reply.getPayload();
    }
}

如果不是init-method,则在第一次调用之后初始化currencyListBO,一切正常。

public CurrencyListBO getCurrencyList() {
    if(currencyListBO == null) {
        init();
    }
    return currencyListBO;
}

请让我知道第一种方法的问题。

1 个答案:

答案 0 :(得分:7)

在实例化bean之后但在连接其余上下文之前调用init / @PostConstruct方法(在这种情况下,在RMI适配器订阅了通道之前)。

您需要等到上下文完全刷新。

实现这一目标的一种方法是实施

ApplicationListener<ContextRefreshedEvent>

并将您的代码放入

public void onApplicationEvent(ContextRefreshedEvent event)

在上下文完全连线后将调用该方法。