我必须从仅使用@JmsListener
更改为动态设置侦听器,以便允许我的用户配置应用程序以选择要读取的队列。
我尝试遵循Spring JMS Documentation进行程序化端点注册,但是它没有涵盖一个方面:如何设置要用于我的侦听器的ListenerContainerFactory
。
我尝试了以下方法:
@Configuration
@EnableJms
public class JmsConfig implements JmsListenerConfigurer {
@Autowired
private JmsListenerEndpointRegistry registry;
@Overide
public void configureJmsListeners(JmsListenerEndpointRegistrar register) {
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
endpoint.setId("myJmsEndpoint");
endpoint.setDestination("TestQueue");
endpoint.setupListenerContainer(registry.getListenerContainer("myContainerFactory"))
endpoint.setMessageListener( message -> {
// handle
});
register.registerEndpoint(endpoint)
}
@Bean
public JmsListenerContainerFactory myContainerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
// Other Connection and Container factories
}
但我收到:
java.lang.IllegalArgumentException: Could not configure endpoint with the specified container 'null' Only JMS (org.springframework.jms.listener.AbstractMessageListenerContainer subclass) or JCA (org.springframework.jms.listener.endpoint.JmsMessageEndpointManager) are supported.
at org.springframework.jms.config.AbstractJmsListenerEndpoint$JcaEndpointConfigurer.configureEndpoint(AbstractJmsListenerEndpoint.java:188)
我猜测null
是因为在此阶段,Spring仍然没有创建bean(对吗?)。
什么是使这项工作正确的方法?
谢谢!