spring xml 的配置:

文件名:applicationContext-biz-mq.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:jms="http://www.springframework.org/schema/jms"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd ">
  11. <description>MQ</description>
  12. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  13. <property name="locations">
  14. <list>
  15. <value>classpath:/mqconnect.properties</value>
  16. </list>
  17. </property>
  18. <property name="ignoreUnresolvablePlaceholders" value="true" />
  19. </bean>
  20. <!-- 业务实现类 -->
  21. <bean id="callRecordBiz" class="com.zmcc.servicemanager.biz.impl.CallRecordBizImpl" autowire="byName"/>
  22. <!-- <bean id="mqMessageSend" class="com.zmcc.servicemanager.mq.MQMessageSend">
  23. jie
  24. <property name="queueManagerName" value="${queue.manager.send}" />
  25. <property name="host" value="${queue.manager.host.send}" />
  26. <property name="destinationName" value="${queue.name.send}" />
  27. </bean>
  28. <bean id="mqMessageReceiveListener" class="com.zmcc.servicemanager.mq.MQMessageReceiveListener" init-method="start">
  29. 接收方队列管理器名
  30. <property name="queueManagerName" value="${queue.manager.get}" />
  31. <property name="host" value="${queue.manager.host.get}" />
  32. <property name="destinationName" value="${queue.name.get}" />
  33. </bean> -->
  34. <!-- WebSphere MQ Connection Factory -->
  35. <bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQConnectionFactory">
  36. <property name="hostName" value="${queue.manager.host.get}"/>
  37. <property name="queueManager" value="${queue.manager.get}"/>
  38. <property name="channel" value="${app.mq.channel}"/>
  39. <property name="port" value="${app.mq.port}"/>
  40. <property name="transportType" value="${app.mq.transportType}"/>
  41. </bean>
  42. <!-- JMS Queue Connection Factory -->
  43. <bean id="transactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
  44. <property name="connectionFactory" ref="mqConnectionFactory"/>
  45. </bean>
  46. <!-- JMS Destination Resolver -->
  47. <!-- <bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.DynamicDestinationResolver" /> -->
  48. <bean id="messageConverter" class="com.zmcc.servicemanager.mq.spring.MessageConverter" />
  49. <!-- 定义接收者 -->
  50. <bean  id="messageListener" class="com.zmcc.servicemanager.mq.spring.MessageListener" >
  51. <property name="callRecordBiz" ref="callRecordBiz" />
  52. </bean>
  53. <!-- JMS  listener -->
  54. <!-- <bean id="messageListenerAdapter" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
  55. <property name="defaultListenerMethod" value="receviedMessage"/>
  56. <property name="messageConverter" ref="messageConverter"/>
  57. <property name="delegate" ref="messageListener"/>
  58. </bean> -->
  59. <!--
  60. JMS listener
  61. listener:org.springframework.jms.listener.adapter.MessageListenerAdapter
  62. message-converter="messageConverter",
  63. -->
  64. <jms:listener-container connection-factory="mqConnectionFactory" transaction-manager="transactionManager"
  65. message-converter="messageConverter">
  66. <jms:listener destination="${queue.name.get}" ref="messageListener" method="receviedMessage" />
  67. </jms:listener-container>
  68. </beans>

mqconnect.properties配置文件:

  1. #通道名
  2. app.mq.channel=SYSTEM.DEF.SVRCONN
  3. #传输类型
  4. app.mq.transportType=1
  5. #端口号
  6. app.mq.port=1414
  7. #队列管理器名称-发送方
  8. queue.manager.send=WMQ1QM
  9. #主机地址-发送方
  10. queue.manager.host.send=10.70.175.81
  11. #队列名称-发送方
  12. queue.name.send=WMQ1OutputQ
  13. #队列管理器名称--接收方
  14. queue.manager.get=WMQ2QM
  15. #主机地址--接收方
  16. queue.manager.host.get=10.70.175.82
  17. #队列名称--接收方
  18. queue.name.get=WMQ2InputQ

MessageTest.Java测试文件,用来启动接收监听的配置文件:

  1. import org.springframework.context.support.ClassPathXmlApplicationContext;
  2. public class MessageTest {
  3. /**
  4. * @param args
  5. */
  6. public static void main(String[] args) {
  7. new ClassPathXmlApplicationContext("classpath*:/applicationContext-biz-mq.xml");
  8. }
  9. }

MessageListener.java监听文件:

  1. import com.zmcc.servicemanager.biz.CallRecordBiz;
  2. import com.zmcc.servicemanager.domain.CallRecord;
  3. /**
  4. * 消息监听器
  5. * @author ZouXia
  6. *
  7. */
  8. public class MessageListener{
  9. private CallRecordBiz callRecordBiz;
  10. /**
  11. * 接收消息
  12. * @param callRecord
  13. */
  14. public void receviedMessage(CallRecord callRecord) {
  15. System.out.println(callRecord.getRequestContent());
  16. //      callRecordBiz.saveEntity(callRecord);
  17. }
  18. public CallRecordBiz getCallRecordBiz() {
  19. return callRecordBiz;
  20. }
  21. public void setCallRecordBiz(CallRecordBiz callRecordBiz) {
  22. this.callRecordBiz = callRecordBiz;
  23. }
  24. }

MessageConverter.java用来转换的类:

  1. import javax.jms.JMSException;
  2. import javax.jms.Message;
  3. import javax.jms.Session;
  4. import javax.jms.TextMessage;
  5. import org.springframework.jms.support.converter.MessageConversionException;
  6. import com.zmcc.servicemanager.domain.CallRecord;
  7. /**
  8. * 消息转换器
  9. * @author ZouXia
  10. *
  11. */
  12. public class MessageConverter implements org.springframework.jms.support.converter.MessageConverter {
  13. /**
  14. * 发送消息的转换
  15. */
  16. public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
  17. return null;
  18. }
  19. /**
  20. * 接收消息的转换
  21. */
  22. public Object fromMessage(Message message) throws JMSException,MessageConversionException {
  23. // TODO
  24. TextMessage mapMessage = (TextMessage) message;
  25. CallRecord callRecord = new CallRecord();
  26. callRecord.setEndTime(new Date());
  27. callRecord.setRequestContent(mapMessage.getText());
  28. return callRecord;
  29. }
  30. }

CallRecord.java实体类

  1. public class CallRecord implements Serializable{
  2. private static final long serialVersionUID = 1L;
  3. private String requestContent;
  4. private Date endTime;
  5. ……
  6. }

JmsProducer.java发送消息:

  1. import javax.jms.Connection;
  2. import javax.jms.Destination;
  3. import javax.jms.JMSException;
  4. import javax.jms.MessageProducer;
  5. import javax.jms.Session;
  6. import javax.jms.TextMessage;
  7. import com.ibm.mq.jms.JMSC;
  8. import com.ibm.mq.jms.MQQueueConnectionFactory;
  9. /**
  10. * 点对点模式
  11. * @author ZouXia
  12. *
  13. */
  14. public class JmsProducer {
  15. /**
  16. * Main method
  17. *
  18. * @param args
  19. */
  20. public static void main(String[] args) {
  21. // Variables
  22. Connection connection = null;
  23. Session session = null;
  24. Destination destination = null;
  25. MessageProducer producer = null;
  26. try {
  27. // Create a connection factory
  28. // objects
  29. MQQueueConnectionFactory factory = new MQQueueConnectionFactory();
  30. factory.setQueueManager("WMQ1QM");
  31. factory.setHostName("10.70.175.81");
  32. factory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
  33. factory.setPort(1414);
  34. factory.setChannel("SYSTEM.DEF.SVRCONN");
  35. // Create JMS objects
  36. connection = factory.createConnection();
  37. session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  38. destination = session.createQueue("WMQ1OutputQ");
  39. producer = session.createProducer(destination);
  40. long uniqueNumber = System.currentTimeMillis() % 1000;
  41. TextMessage message = session
  42. .createTextMessage("JmsProducer: Your lucky number today is " + uniqueNumber);
  43. // Start the connection
  44. connection.start();
  45. // And, send the message
  46. producer.send(message);
  47. System.out.println("Sent message:\n" + message);
  48. } catch (JMSException jmsex) {
  49. jmsex.fillInStackTrace();
  50. } finally {
  51. try {
  52. producer.close();
  53. session.close();
  54. connection.close();
  55. } catch (JMSException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. }

所对应的发送方WMQ1QM的资源管理器:

所对应的通道:

接收方WMQ2QM的队列:

通道:

资源管理器即IBM MQ explorer是基于eclipse的进行的,在eclipse里面添加plug插件即可,其更新地址为:

http://public.dhe.ibm.com/software/integration/wmq/explorer/v75/updates/

如果出现以下问题:

  1. Exception in thread "main" java.lang.NoClassDefFoundError: javax/resource/ResourceException
  2. at com.ibm.mq.MQEnvironment.<clinit>(MQEnvironment.java:490)
  3. at com.ibm.mq.jms.services.ConfigEnvironment.<clinit>(ConfigEnvironment.java:190)
  4. at java.lang.Class.forName0(Native Method)
  5. at java.lang.Class.forName(Class.java:247)
  6. at com.ibm.mq.jms.MQConnectionFactory$1.run(MQConnectionFactory.java:658)
  7. at java.security.AccessController.doPrivileged(Native Method)
  8. at com.ibm.mq.jms.MQConnectionFactory.<clinit>(MQConnectionFactory.java:651)
  9. at com.zmcc.servicemanager.mq.jms.JmsProducer.main(JmsProducer.java:36)
  10. Caused by: java.lang.ClassNotFoundException: javax.resource.ResourceException
  11. at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
  12. at java.security.AccessController.doPrivileged(Native Method)
  13. at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
  14. at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
  15. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
  16. at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
  17. at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
  18. ... 8 more
pom.xml里面要引入connector.jar
  1. <dependency>
  2. <groupId>javax.resource</groupId>
  3. <artifactId>connector</artifactId>
  4. <version>1.0</version>
  5. </dependency>

spring监听与IBM MQ JMS整合的更多相关文章

  1. spring中配置监听队列的MQ

    一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置.  <bean id="axx" ...

  2. ActiveMQ监听消息并进行转发,监听不同的mq服务器和不同的队列

    工作中刚接触mq消息业务,其实也就是监听一下别的项目发送的消息然后进行对应的转发,但是监听的mq会有多个,而且转发的地址也可能有多个,这里就使用spring集成的方式!记录一下实现方式: 监听多个mq ...

  3. spring监听机制——观察者模式的应用

    使用方法 spring监听模式需要三个组件: 1. 事件,需要继承ApplicationEvent,即观察者模式中的"主题",可以看做一个普通的bean类,用于保存在事件监听器的业 ...

  4. Spring 监听

    Spring 中的事件监听的实现 这里我们不讨论事件监听的机制的原理,我们只讨论如何在项目中实现时间监听. spring的事件监听是基于观察者模式.设计开发中.如下类与接口是我们必须要使用的. App ...

  5. Spring监听,ApplicationListener

    import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; import ...

  6. SpringBoot 对IBM MQ进行数据监听接收以及数据发送

    一.需求介绍 后端使用Spring Boot2.0框架,要实现IBM MQ的实时数据JMS监听接收处理,并形成回执通过MQ队列发送. 二.引入依赖jar包 <dependency> < ...

  7. IBM MQ 与spring的整合

    文件名:applicationContext-biz-mq.xml 新浪博客把里面的代码全部转换成HTML了,所以无法粘贴 可以查看CSDN里面的:http://blog.csdn.net/xiazo ...

  8. IBM Mq Spring JMS 的xml配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  9. IBM MQ 集成CXF 发送JMS 消息

    0.POM依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

随机推荐

  1. web项目除了业务还需要关注的点

    1:安全性,不允许访问外网,访问外网通过反向代理的方式. 2:安全性,和外网交互的时候,需要CA证书,基于SSL协议的证书 3:日志,生产上通常会关闭某些日志,所以,允许出现的日志就显得至关重要了. ...

  2. KCP 传输协议

    作者:韦易笑链接:https://www.zhihu.com/question/36258781/answer/98944369来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  3. 一份CTR的特征工程图

  4. MyBatis 中#与$的区别

    今天在工作中有个点击排序的功能调试了许久,终寻因,总结之.  需求是这样的,页面有个table,有一列的上下箭头可点击并排序.对于这种需求,我的mybatis.xml的sql配置写成了如下: < ...

  5. Mac OS 修改hosts文件

    这里用得是 VI 编辑器修改 打开终端(应用程序——实用工具),运行: sudo vi /etc/hosts,此时屏幕上会提示你输入密码 打开 hosts 文件之后按 i 键进入插入模式(可理解为编辑 ...

  6. python学习笔记一和PHP的一些对比

    python和php一样是 解释性语言 php和php一样 定义变量不需要指定类型,C语言中的print函数 在python中也是适用的 python编码 适用缩进  4个空格,通常存储为UTF-8模 ...

  7. Nop权限的使用

    1.首先后台Admin--->siteMap中添加: <siteMapNode SystemName="我是系统名" nopResource="显示的中文名称 ...

  8. OpenACC 书上的范例代码(Jacobi 迭代),part 1

    ▶ 使用Jacobi 迭代求泊松方程的数值解 ● 原始串行版本,运行时间 2272 ms #include <stdio.h> #include <stdlib.h> #inc ...

  9. zabbix监控windows用户登陆情况

    https://yq.aliyun.com/articles/511381 添加登录失败监控项: 特别注意:把类型设置为:文本格式,否则会报类型错误. eventlog[Security,," ...

  10. 16. orcle中replace的用法及例子

    replace 函数用法如下: replace('将要更改的字符串','被替换掉的字符串','替换字符串'); 例子: select  replace ('1,2,3',',',';') from d ...