gpt4 book ai didi

spring-integration - 在初始化方法中使用 channel 时,Spring集成 'Dispatcher has no subscribers'

转载 作者:行者123 更新时间:2023-12-04 18:12:18 24 4
gpt4 key购买 nike

尝试在Spring bean的init-method中将消息发布到 channel 时出现“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();
}
}

如果在第一次调用后初始化了currencyListBO而不是init方法,则一切正常。
public CurrencyListBO getCurrencyList() {
if(currencyListBO == null) {
init();
}
return currencyListBO;
}

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

最佳答案

在实例化您的bean之后但在关联其余上下文之前(在这种情况下,在将RMI适配器预订到 channel 之前)将调用init/@ PostConstruct方法。

您需要等待,直到上下文完全刷新为止。

做到这一点的一种方法是实现

ApplicationListener<ContextRefreshedEvent>

然后把你的代码放进去
public void onApplicationEvent(ContextRefreshedEvent event)

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

关于spring-integration - 在初始化方法中使用 channel 时,Spring集成 'Dispatcher has no subscribers',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11136000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com