Java spring jmsListener侦听多个队列
在这篇文章中,Garry Russell解释了如何以编程方式创建多个Kafkalistener,以便从多个主题中聆听。。[此设置实际上正在为我成功运行] 现在,我希望有一个类似的设置也适用于JMSListener——在这里,我可以有一个类,其中有一个@JMSListener,并且我可以通过编程方式创建多个JMSListener实例,每个实例都注入了它自己的queueName 我找到了这个帖子 在这篇文章的最后,加里也发表了类似的评论 如果希望动态创建大量容器,那么只需通过编程方式创建容器,调用AfterPropertieSet(),然后启动() 我使用了我在上面第一篇文章(与KafkaListeners相关)中使用的设置,我的JMS侦听器的多个实例正在启动,但不使用任何消息 基本上我不明白我在哪里做这件事 然后以编程方式创建容器,调用AfterPropertieSet(),然后启动() 我对容器这个词感到困惑,我知道有JMSListener和 JmsListenerContainerFactory,在这个上下文中,容器是什么?我猜JMSListener 我已确认队列中有消息。另外,如果我没有以编程方式创建侦听器,并且只在其中提到一个带有硬编码队列的侦听器,那么它会很好地使用消息 当我以编程方式创建多个JMS侦听器时,基本上没有一个侦听器在使用消息Java spring jmsListener侦听多个队列,java,spring-boot,ibm-mq,spring-jms,Java,Spring Boot,Ibm Mq,Spring Jms,在这篇文章中,Garry Russell解释了如何以编程方式创建多个Kafkalistener,以便从多个主题中聆听。。[此设置实际上正在为我成功运行] 现在,我希望有一个类似的设置也适用于JMSListener——在这里,我可以有一个类,其中有一个@JMSListener,并且我可以通过编程方式创建多个JMSListener实例,每个实例都注入了它自己的queueName 我找到了这个帖子 在这篇文章的最后,加里也发表了类似的评论 如果希望动态创建大量容器,那么只需通过编程方式创建容器,调
@SpringBootApplication
@EnableJms
public class MqProdConsumerApplication {
private static Logger logger = LogManager.getLogger(MqProdConsumerApplication.class.getName());
private static Consumers consumersStatic;
@Autowired
Consumers consumers;
@PostConstruct
public void init() {
consumersStatic = this.consumers;
}
@Bean
public Gson gson() {
return new Gson();
}
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(MqProdConsumerApplication.class, args);
List<QueueInformation> queueInformationList = consumersStatic.getQueueInformationList();
Assert.notEmpty(queueInformationList, "queueInformationList cannot be empty");
logger.debug("queueInformationList ************" + queueInformationList.toString());
for (QueueInformation queueInformation : queueInformationList) {
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.setParent(context);
child.register(MQConfig.class);
Properties props = new Properties();
props.setProperty("mqQueueName", queueInformation.getMqQueueName());
//
PropertiesPropertySource pps = new PropertiesPropertySource("listenerProps", props);
child.getEnvironment().getPropertySources().addLast(pps);
child.refresh();
}
}
}
这是我的申请表
ibm.mq.queueManager: ABCTOD01
ibm.mq.channel: QMD00.SERVER
ibm.mq.connName: mqdv1.devfg.ABC.com
ibm.mq.user: pmd0app1
ibm.mq.password:
consumers:
queueInformationList:
-
mqQueueName: QMD00.D.SRF.PERSON.LITE.PHONE.LOAD
-
mqQueueName: QMD00.D.SRF.PERSON.PHONE.LOAD
好的,我找到了另一个帖子,加里回答了我要找的问题 基本上,这是我的有效解决方案。 伟大的工作@GaryRussell-我现在是一个粉丝:)
@配置
@使能JMS
公共类AppConfig实现JMSListenerConfiger{
@凌驾
公共无效配置JMSListender(JMSListendendPointRegistrator){
List queueInformationList=ConsumerStatic.getQueueInformationList();
int i=0;
对于(队列信息队列信息:
队列信息列表){
SimpleJMListenerEndpoint=新的SimpleJMListenerEndpoint();
setId(“myJmsEndpoint-”+i++);
setDestination(queueInformation.getMqQueueName());
endpoint.setMessageListener(消息->{
logger.debug(“************************************************************已收到消息:“+消息”);
});
注册点(端点);
debug(“为队列注册端点”+queueInformation.getMqQueueName());
}
}
另请参见详细记录的@robin。我更新了您的答案,加入了类注释,以澄清此方法属于何处
public class MQListener {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${mqQueueName}")
private String mqQueueName;
@PostConstruct
public void afteConstruct() {
logger.debug("************* initialized MQ Listener successfully, will read from =" + mqQueueName);
}
@JmsListener(destination = "${mqQueueName}", containerFactory = "myFactory")
public void receiveMessage(String receivedMessage) throws JAXBException, ExecutionException, InterruptedException {
logger.debug("***********************************************receivedMessage:" + receivedMessage);
}
}
ibm.mq.queueManager: ABCTOD01
ibm.mq.channel: QMD00.SERVER
ibm.mq.connName: mqdv1.devfg.ABC.com
ibm.mq.user: pmd0app1
ibm.mq.password:
consumers:
queueInformationList:
-
mqQueueName: QMD00.D.SRF.PERSON.LITE.PHONE.LOAD
-
mqQueueName: QMD00.D.SRF.PERSON.PHONE.LOAD
@Configuration
@EnableJms
public class AppConfig implements JmsListenerConfigurer {
@Override
public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
List<QueueInformation> queueInformationList = consumersStatic.getQueueInformationList();
int i = 0;
for (QueueInformation queueInformation :
queueInformationList) {
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
endpoint.setId("myJmsEndpoint-" + i++);
endpoint.setDestination(queueInformation.getMqQueueName());
endpoint.setMessageListener(message -> {
logger.debug("***********************************************receivedMessage:" + message);
});
registrar.registerEndpoint(endpoint);
logger.debug("registered the endpoint for queue" + queueInformation.getMqQueueName());
}
}