如何使用 spring 连续启动从 IBM MQ JMS 接收消息?
How to receive messages from IBM MQ JMS using spring boot continuously?
我有 spring 启动应用程序,它接收 JSON 请求并将其推送到 IBM MQ JMS 队列。可以有 n 个 JSON 个请求将被推送到队列中。我的目标是处理队列中的每个请求。我如何收听队列并使用 spring 引导一条一条地获取消息以进行处理?
您需要将侦听器实现为:
1:创建配置如下:
@Bean
public MQConnectionFactory mqConnectionFactory(){
MQConnectionFactory connectionFactory = new MQConnectionFactory();
connectionFactory.setHostName(); //mq host name
connectionFactory.setPort(); // mq port
connectionFactory.setQueueManager(); //mq queue manager
connectionFactory.setChannel(); //mq channel name
connectionFactory.setTransportType(1);
connectionFactory.setSSLCipherSuite(); //tls cipher suite name
return connectionFactory;
}
@Bean()
public DefaultMessageListenerContainer myMessageEventContainer() {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setAutoStartup(true);
container.setConnectionFactory(mqConnectionFactory);
container.setDestinationName(//queue name//);
container.setMessageListener(new MyEventListener());
return container;
}
2:实现消息监听器:
public class MyEventListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String stringMessage = textMessage.getText();
//do something with your message from queue
}
} catch (JMSException e) {
//catch error
}
}
}
在pom.xml中添加'mq-jms-spring-boot-starter'依赖如下:
在 application.yaml 中添加以下属性:
mq:
queue-manager: queueManager
conn-name: connName(port)
channel: channelName
ssl-cipher-suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 or whatever ur app supports
use-i-b-m-cipher-mappings: true (if using topic otherwise skip this property)
在您的侦听器方法中添加以下注解@JmsListener
@EnableJms
Public class Consumer{
@JmsListener(destination = "QueueNameToListenTo")
public void listener(Object message) {
logger.info("message received {}",message);
//do something
}
}
我有 spring 启动应用程序,它接收 JSON 请求并将其推送到 IBM MQ JMS 队列。可以有 n 个 JSON 个请求将被推送到队列中。我的目标是处理队列中的每个请求。我如何收听队列并使用 spring 引导一条一条地获取消息以进行处理?
您需要将侦听器实现为:
1:创建配置如下:
@Bean
public MQConnectionFactory mqConnectionFactory(){
MQConnectionFactory connectionFactory = new MQConnectionFactory();
connectionFactory.setHostName(); //mq host name
connectionFactory.setPort(); // mq port
connectionFactory.setQueueManager(); //mq queue manager
connectionFactory.setChannel(); //mq channel name
connectionFactory.setTransportType(1);
connectionFactory.setSSLCipherSuite(); //tls cipher suite name
return connectionFactory;
}
@Bean()
public DefaultMessageListenerContainer myMessageEventContainer() {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setAutoStartup(true);
container.setConnectionFactory(mqConnectionFactory);
container.setDestinationName(//queue name//);
container.setMessageListener(new MyEventListener());
return container;
}
2:实现消息监听器:
public class MyEventListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String stringMessage = textMessage.getText();
//do something with your message from queue
}
} catch (JMSException e) {
//catch error
}
}
}
在pom.xml中添加'mq-jms-spring-boot-starter'依赖如下:
在 application.yaml 中添加以下属性:
mq:
queue-manager: queueManager
conn-name: connName(port)
channel: channelName
ssl-cipher-suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 or whatever ur app supports
use-i-b-m-cipher-mappings: true (if using topic otherwise skip this property)
在您的侦听器方法中添加以下注解@JmsListener
@EnableJms
Public class Consumer{
@JmsListener(destination = "QueueNameToListenTo")
public void listener(Object message) {
logger.info("message received {}",message);
//do something
}
}