Spring JMS与IBM MQ 整合,监听

[size=x-large]项目中因为要监听IBM MQ 队列的消息,想到用Spring JMS来做[/size]

[b]Spring JMS介绍[/b]
Spring对JMS(Java Message Service)的封装,简化了代码量,便于使用 :wink:

[b]下面直接贴代码来说明[/b]
[*]定义MQ连接工厂










[*]定义工厂适配器

class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">



[*]队列定义






[*]队列监听容器

class="org.springframework.jms.listener.DefaultMessageListenerContainer"
lazy-init="false">
ref="testUserCredentialsConnectionFactoryAdapter" />







[*]消息监听

package com.liyghting.mq.listener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service
public class TestMessageListener implements MessageListener {
private static final Logger logger = LoggerFactory.getLogger(TestMessageListener .class);
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage)message;
try {
String msg = textMessage.getText();
logger.info("消息[{}]",msg);
} catch (JMSException e) {
logger.error("消息监听异常", e);
}
}
}

你可能感兴趣的:(Java,spring,jms,java)