spring监听与IBM MQ JMS整合
Posted 心若不动,风又奈何;你若不伤,岁月无恙。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring监听与IBM MQ JMS整合相关的知识,希望对你有一定的参考价值。
spring xml 的配置:
文件名:applicationContext-biz-mq.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:jms="http://www.springframework.org/schema/jms"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd ">
- <description>MQ</description>
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:/mqconnect.properties</value>
- </list>
- </property>
- <property name="ignoreUnresolvablePlaceholders" value="true" />
- </bean>
- <!-- 业务实现类 -->
- <bean id="callRecordBiz" class="com.zmcc.servicemanager.biz.impl.CallRecordBizImpl" autowire="byName"/>
- <!-- <bean id="mqMessageSend" class="com.zmcc.servicemanager.mq.MQMessageSend">
- jie
- <property name="queueManagerName" value="${queue.manager.send}" />
- <property name="host" value="${queue.manager.host.send}" />
- <property name="destinationName" value="${queue.name.send}" />
- </bean>
- <bean id="mqMessageReceiveListener" class="com.zmcc.servicemanager.mq.MQMessageReceiveListener" init-method="start">
- 接收方队列管理器名
- <property name="queueManagerName" value="${queue.manager.get}" />
- <property name="host" value="${queue.manager.host.get}" />
- <property name="destinationName" value="${queue.name.get}" />
- </bean> -->
- <!-- WebSphere MQ Connection Factory -->
- <bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQConnectionFactory">
- <property name="hostName" value="${queue.manager.host.get}"/>
- <property name="queueManager" value="${queue.manager.get}"/>
- <property name="channel" value="${app.mq.channel}"/>
- <property name="port" value="${app.mq.port}"/>
- <property name="transportType" value="${app.mq.transportType}"/>
- </bean>
- <!-- JMS Queue Connection Factory -->
- <bean id="transactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
- <property name="connectionFactory" ref="mqConnectionFactory"/>
- </bean>
- <!-- JMS Destination Resolver -->
- <!-- <bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.DynamicDestinationResolver" /> -->
- <bean id="messageConverter" class="com.zmcc.servicemanager.mq.spring.MessageConverter" />
- <!-- 定义接收者 -->
- <bean id="messageListener" class="com.zmcc.servicemanager.mq.spring.MessageListener" >
- <property name="callRecordBiz" ref="callRecordBiz" />
- </bean>
- <!-- JMS listener -->
- <!-- <bean id="messageListenerAdapter" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
- <property name="defaultListenerMethod" value="receviedMessage"/>
- <property name="messageConverter" ref="messageConverter"/>
- <property name="delegate" ref="messageListener"/>
- </bean> -->
- <!--
- JMS listener
- listener:org.springframework.jms.listener.adapter.MessageListenerAdapter
- message-converter="messageConverter",
- -->
- <jms:listener-container connection-factory="mqConnectionFactory" transaction-manager="transactionManager"
- message-converter="messageConverter">
- <jms:listener destination="${queue.name.get}" ref="messageListener" method="receviedMessage" />
- </jms:listener-container>
- </beans>
mqconnect.properties配置文件:
- #通道名
- app.mq.channel=SYSTEM.DEF.SVRCONN
- #传输类型
- app.mq.transportType=1
- #端口号
- app.mq.port=1414
- #队列管理器名称-发送方
- queue.manager.send=WMQ1QM
- #主机地址-发送方
- queue.manager.host.send=10.70.175.81
- #队列名称-发送方
- queue.name.send=WMQ1OutputQ
- #队列管理器名称--接收方
- queue.manager.get=WMQ2QM
- #主机地址--接收方
- queue.manager.host.get=10.70.175.82
- #队列名称--接收方
- queue.name.get=WMQ2InputQ
MessageTest.Java测试文件,用来启动接收监听的配置文件:
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MessageTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- new ClassPathXmlApplicationContext("classpath*:/applicationContext-biz-mq.xml");
- }
- }
MessageListener.java监听文件:
- import com.zmcc.servicemanager.biz.CallRecordBiz;
- import com.zmcc.servicemanager.domain.CallRecord;
- /**
- * 消息监听器
- * @author ZouXia
- *
- */
- public class MessageListener{
- private CallRecordBiz callRecordBiz;
- /**
- * 接收消息
- * @param callRecord
- */
- public void receviedMessage(CallRecord callRecord) {
- System.out.println(callRecord.getRequestContent());
- // callRecordBiz.saveEntity(callRecord);
- }
- public CallRecordBiz getCallRecordBiz() {
- return callRecordBiz;
- }
- public void setCallRecordBiz(CallRecordBiz callRecordBiz) {
- this.callRecordBiz = callRecordBiz;
- }
- }
MessageConverter.java用来转换的类:
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.springframework.jms.support.converter.MessageConversionException;
- import com.zmcc.servicemanager.domain.CallRecord;
- /**
- * 消息转换器
- * @author ZouXia
- *
- */
- public class MessageConverter implements org.springframework.jms.support.converter.MessageConverter {
- /**
- * 发送消息的转换
- */
- public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
- return null;
- }
- /**
- * 接收消息的转换
- */
- public Object fromMessage(Message message) throws JMSException,MessageConversionException {
- // TODO
- TextMessage mapMessage = (TextMessage) message;
- CallRecord callRecord = new CallRecord();
- callRecord.setEndTime(new Date());
- callRecord.setRequestContent(mapMessage.getText());
- return callRecord;
- }
- }
CallRecord.java实体类
- public class CallRecord implements Serializable{
- private static final long serialVersionUID = 1L;
- private String requestContent;
- private Date endTime;
- ……
- }
JmsProducer.java发送消息:
- import javax.jms.Connection;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.MessageProducer;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import com.ibm.mq.jms.JMSC;
- import com.ibm.mq.jms.MQQueueConnectionFactory;
- /**
- * 点对点模式
- * @author ZouXia
- *
- */
- public class JmsProducer {
- /**
- * Main method
- *
- * @param args
- */
- public static void main(String[] args) {
- // Variables
- Connection connection = null;
- Session session = null;
- Destination destination = null;
- MessageProducer producer = null;
- try {
- // Create a connection factory
- // objects
- MQQueueConnectionFactory factory = new MQQueueConnectionFactory();
- factory.setQueueManager("WMQ1QM");
- factory.setHostName("10.70.175.81");
- factory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
- factory.setPort(1414);
- factory.setChannel("SYSTEM.DEF.SVRCONN");
- // Create JMS objects
- connection = factory.createConnection();
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- destination = session.createQueue("WMQ1OutputQ");
- producer = session.createProducer(destination);
- long uniqueNumber = System.currentTimeMillis() % 1000;
- TextMessage message = session
- .createTextMessage("JmsProducer: Your lucky number today is " + uniqueNumber);
- // Start the connection
- connection.start();
- // And, send the message
- producer.send(message);
- System.out.println("Sent message:\n" + message);
- } catch (JMSException jmsex) {
- jmsex.fillInStackTrace();
- } finally {
- try {
- producer.close();
- session.close();
- connection.close();
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- }
- }
所对应的发送方WMQ1QM的资源管理器:
所对应的通道:
接收方WMQ2QM的队列:
通道:
资源管理器即IBM MQ explorer是基于eclipse的进行的,在eclipse里面添加plug插件即可,其更新地址为:
http://public.dhe.ibm.com/software/integration/wmq/explorer/v75/updates/
如果出现以下问题:
- Exception in thread "main" java.lang.NoClassDefFoundError: javax/resource/ResourceException
- at com.ibm.mq.MQEnvironment.<clinit>(MQEnvironment.java:490)
- at com.ibm.mq.jms.services.ConfigEnvironment.<clinit>(ConfigEnvironment.java:190)
- at java.lang.Class.forName0(Native Method)
- at java.lang.Class.forName(Class.java:247)
- at com.ibm.mq.jms.MQConnectionFactory$1.run(MQConnectionFactory.java:658)
- at java.security.AccessController.doPrivileged(Native Method)
- at com.ibm.mq.jms.MQConnectionFactory.<clinit>(MQConnectionFactory.java:651)
- at com.zmcc.servicemanager.mq.jms.JmsProducer.main(JmsProducer.java:36)
- Caused by: java.lang.ClassNotFoundException: javax.resource.ResourceException
- at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
- at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
- at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
- ... 8 more
pom.xml里面要引入connector.jar
- <dependency>
- <groupId>javax.resource</groupId>
- <artifactId>connector</artifactId>
- <version>1.0</version>
- </dependency>
以上是关于spring监听与IBM MQ JMS整合的主要内容,如果未能解决你的问题,请参考以下文章
spring 集成 IBM MQ 启动报错, Uncategorized exception occured during JMS processing;