gpt4 book ai didi

java - 使用单个 Spring Boot 应用程序收听多个 IBM MQ

转载 作者:行者123 更新时间:2023-12-01 13:11:45 29 4
gpt4 key购买 nike

我需要收听多个队列(存在于同一个队列管理器中)。我有用于监听单个队列的有效 Spring Boot 应用程序代码。但是有什么方法可以从单个 Spring Boot 应用程序连接到多个队列吗?

如果我可以在运行时将监听器从一个队列切换到另一个队列,还有吗?

我有从单个队列中读取的代码,如下所示:

public class ConnectionConfiguration {

private static final Logger logger = LogManager.getLogger(ConnectionConfiguration.class);

@Value("${LDR_LOCAL_MQ_READ_FACTORYNAME}")
String connectionFactory;

@Value("${LDR_LOCAL_MQ_QUEUENAME}")
String localQueue;

@Value("${jmsConcurrency}")
String concurrency;

@Value("${servers.mq.host}")
private String host;

@Value("${servers.mq.port}")
private Integer port;

@Value("${servers.mq.queue-manager}")
private String queueManager;

@Value("${servers.mq.channel}")
private String channel;

@Value("${servers.mq.queue}")
private String queue;

@Value("${servers.mq.timeout}")
private long timeout;

@Bean
public ConnectionFactory connectionFactory() {

JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setResourceRef(true);
jndiObjectFactoryBean.setJndiName(connectionFactory);
try {
jndiObjectFactoryBean.afterPropertiesSet();
} catch (IllegalArgumentException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
} catch (NamingException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return (ConnectionFactory) jndiObjectFactoryBean.getObject();
}

@Bean
public MQQueueConnectionFactory mqQueueConnectionFactory() {
MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
try {
mqQueueConnectionFactory.setHostName(host);
mqQueueConnectionFactory.setQueueManager(queueManager);
mqQueueConnectionFactory.setPort(port);
mqQueueConnectionFactory.setChannel(channel);
mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
mqQueueConnectionFactory.setCCSID(1208);
} catch (Exception e) {
e.printStackTrace();
}
return mqQueueConnectionFactory;
}

@Bean
public SimpleMessageListenerContainer queueContainer(MQQueueConnectionFactory mqQueueConnectionFactory) {

SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(mqQueueConnectionFactory);
container.setDestinationName(queue);
container.setMessageListener(getListenerWrapper());
container.start();
return container;
}

@Bean
public MQListener getListenerWrapper() {
return new MQListener();
}

@Bean
public JmsTemplate getJmsTemplate() {
try {
return new JmsTemplate(mqQueueConnectionFactory());
} catch (Exception exp) {
logger.error(exp.getMessage(), exp);
}
return null;
}

}

最佳答案

只需为每个队列添加一个监听器容器 bean。

要更改队列,请调用 stop() ,然后 shutdown() ,更改目的地,然后 initialize() ,然后 start() .

关于java - 使用单个 Spring Boot 应用程序收听多个 IBM MQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49910969/

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