嘿,这是我之前发布的关于将消息驱动的 channel 适配器连接到多个队列而不将每个队列都写出来的问题的后续。
How to hook up a list of message driven adapters without actually writing each one out?
我尝试遵循建议的方法:
现在我有了这个配置(子级),它从其环境中获取属性(如队列名称)并相应地创建消息驱动适配器。
@Autowired
private ApplicationContext context;
@Value("${id}")
private String id;
@Value("${primaryConnection}")
private String primaryConnection;
@Value("${queueName}")
private String queueName;
@Bean
public IntegrationFlow primaryInitiationListenerFlow() {
return IntegrationFlows.from(Jms
.messageDriverChannelAdapter(context.getBean(primaryConnection, ConnectionFactory.class))
.autoStartup(false)
.destination(queueName)
.configureListenerContainer(listenerContainerSpec())
.id(id + "PrimaryIn")
.get())
.channel("initiationChannel")
.get();
}
在应用程序启动时,我迭代我拥有的队列名称列表,并为每个队列名称实例化上述上下文,并以我的主上下文作为父级,并传入适当的环境。
public void onApplicationEvent(ContextRefreshedEvent event) {
for(String infoKey : clientConfig.getPairs().keySet()) {
PairInfo info = clientConfig.getPairs().get(infoKey);
Properties properties = info.getProperties();
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addLast(new PropertiesPropertySource("connectionProps", properties));
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.register(InboundFlow.class);
child.setId(properties.getProperty("id"));
child.setParent(context);
child.setEnvironment(environment);
child.refresh();
}
}
子上下文的所有这些实例现在都输入到不同的队列中,并将消息放入由父上下文监听的公共(public) channel (initiationChannel
)中。
@Bean
public IntegrationFlow initiationAndDataFlow() {
return IntegrationFlows
.from("initiationChannel")
.handle(//doStuff)
.get();
}
我已经在父级中定义了 initiationChannel
并按照 Gary 在示例中所做的那样进行了设置。
但是当我启动 spring 上下文时,我收到此错误,指出 initiationChannel
没有发布者:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: >Failed to instantiate [org.springframework.integration.dsl.IntegrationFlow]: >Factory method 'initiationAndDataFlow' threw exception; nested exception is >java.lang.NoClassDefFoundError: org.reactivestreams.Publisher
我需要首先加载父上下文(带有订阅者),然后将其分配给每个子上下文,因此当父上下文加载时,不会有人发布到 channel 。
我不知道我做错了什么。感谢您的帮助。
最佳答案
java.lang.NoClassDefFoundError: org.reactivestreams.Publisher
您的类路径中缺少reactive-streams-1.0.0.jar
。
您应该为您的项目使用 maven 或 gradle,以确保解决所有依赖项。
关于java - 动态入站适配器连接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33721603/