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
- <dependency>
- <groupId>javax.resource</groupId>
- <artifactId>connector</artifactId>
- <version>1.0</version>
- </dependency>
spring监听与IBM MQ JMS整合的更多相关文章
- spring中配置监听队列的MQ
一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置. <bean id="axx" ...
- ActiveMQ监听消息并进行转发,监听不同的mq服务器和不同的队列
工作中刚接触mq消息业务,其实也就是监听一下别的项目发送的消息然后进行对应的转发,但是监听的mq会有多个,而且转发的地址也可能有多个,这里就使用spring集成的方式!记录一下实现方式: 监听多个mq ...
- spring监听机制——观察者模式的应用
使用方法 spring监听模式需要三个组件: 1. 事件,需要继承ApplicationEvent,即观察者模式中的"主题",可以看做一个普通的bean类,用于保存在事件监听器的业 ...
- Spring 监听
Spring 中的事件监听的实现 这里我们不讨论事件监听的机制的原理,我们只讨论如何在项目中实现时间监听. spring的事件监听是基于观察者模式.设计开发中.如下类与接口是我们必须要使用的. App ...
- Spring监听,ApplicationListener
import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; import ...
- SpringBoot 对IBM MQ进行数据监听接收以及数据发送
一.需求介绍 后端使用Spring Boot2.0框架,要实现IBM MQ的实时数据JMS监听接收处理,并形成回执通过MQ队列发送. 二.引入依赖jar包 <dependency> < ...
- IBM MQ 与spring的整合
文件名:applicationContext-biz-mq.xml 新浪博客把里面的代码全部转换成HTML了,所以无法粘贴 可以查看CSDN里面的:http://blog.csdn.net/xiazo ...
- IBM Mq Spring JMS 的xml配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- IBM MQ 集成CXF 发送JMS 消息
0.POM依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
随机推荐
- web项目除了业务还需要关注的点
1:安全性,不允许访问外网,访问外网通过反向代理的方式. 2:安全性,和外网交互的时候,需要CA证书,基于SSL协议的证书 3:日志,生产上通常会关闭某些日志,所以,允许出现的日志就显得至关重要了. ...
- KCP 传输协议
作者:韦易笑链接:https://www.zhihu.com/question/36258781/answer/98944369来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 一份CTR的特征工程图
- MyBatis 中#与$的区别
今天在工作中有个点击排序的功能调试了许久,终寻因,总结之. 需求是这样的,页面有个table,有一列的上下箭头可点击并排序.对于这种需求,我的mybatis.xml的sql配置写成了如下: < ...
- Mac OS 修改hosts文件
这里用得是 VI 编辑器修改 打开终端(应用程序——实用工具),运行: sudo vi /etc/hosts,此时屏幕上会提示你输入密码 打开 hosts 文件之后按 i 键进入插入模式(可理解为编辑 ...
- python学习笔记一和PHP的一些对比
python和php一样是 解释性语言 php和php一样 定义变量不需要指定类型,C语言中的print函数 在python中也是适用的 python编码 适用缩进 4个空格,通常存储为UTF-8模 ...
- Nop权限的使用
1.首先后台Admin--->siteMap中添加: <siteMapNode SystemName="我是系统名" nopResource="显示的中文名称 ...
- OpenACC 书上的范例代码(Jacobi 迭代),part 1
▶ 使用Jacobi 迭代求泊松方程的数值解 ● 原始串行版本,运行时间 2272 ms #include <stdio.h> #include <stdlib.h> #inc ...
- zabbix监控windows用户登陆情况
https://yq.aliyun.com/articles/511381 添加登录失败监控项: 特别注意:把类型设置为:文本格式,否则会报类型错误. eventlog[Security,," ...
- 16. orcle中replace的用法及例子
replace 函数用法如下: replace('将要更改的字符串','被替换掉的字符串','替换字符串'); 例子: select replace ('1,2,3',',',';') from d ...