1. 程式人生 > >Spring 整合 ibm mq

Spring 整合 ibm mq

1.引入ibm mq 依賴

由於ibm mq依賴的jar包不在maven的中央倉庫,如果要在pom.xml中依賴,需要先將jar安裝進本地倉庫或者私服

開啟cmd,cd到mq_installdir/java/lib下,執行下列命令

mvn install:install-file -Dfile=com.ibm.mq.defaultconfig.jar -DgroupId=com.ibm.mq -DartifactId=defaultconfig -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=com.ibm.mq.commonservices.jar -DgroupId=com.ibm.mq -DartifactId=commonservices -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=com.ibm.mqjms.jar -DgroupId=com.ibm -DartifactId=mqjms -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=jms.jar -DgroupId=com.ibm.mq -DartifactId=jms -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=com.ibm.mq.headers.jar -DgroupId=com.ibm.mq -DartifactId=headers -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=com.ibm.mq.jar -DgroupId=com.ibm -DartifactId=mq -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=com.ibm.mq.jmqi.jar -DgroupId=com.ibm.mq -DartifactId=jmqi -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=com.ibm.mq.pcf.jar -DgroupId=com.ibm.mq -DartifactId=pcf -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=com.ibm.mq.postcard.jar -DgroupId=com.ibm.mq -DartifactId=postcard -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=com.ibm.mq.soap.jar -DgroupId=com.ibm.mq -DartifactId=soap -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=com.ibm.mq.tools.ras.jar -DgroupId=com.ibm.mq.tools -DartifactId=ras -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=jms.jar -DgroupId=com.ibm.mq -DartifactId=jms -Dversion=7.5.0.1 -Dpackaging=jar

mvn install:install-file -Dfile=dhbcore.jar -DgroupId=com.ibm.mq -DartifactId=dhbcore -Dversion=7.5.0.1 -Dpackaging=jar
以上命令是在本地倉庫安裝相應的jar包

安裝完成後在pom.xml中引用依賴

<dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>jmqi</artifactId>
            <version>${ibm.mq.version}</version>
        </dependency>
        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>headers</artifactId>
            <version>${ibm.mq.version}</version>
        </dependency>
        <dependency>
            <groupId>com.ibm</groupId>
            <artifactId>mq</artifactId>
            <version>${ibm.mq.version}</version>
        </dependency>
        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>jms</artifactId>
            <version>${ibm.mq.version}</version>
        </dependency>
        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>dhbcore</artifactId>
            <version>${ibm.mq.version}</version>
        </dependency>
        <dependency>
            <groupId>com.ibm</groupId>
            <artifactId>mqjms</artifactId>
            <version>${ibm.mq.version}</version>
        </dependency>
        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>connector</artifactId>
            <version>${ibm.mq.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
2.配置applicationContext-ibmmq.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	<description>MQ</description>
	<!-- 載入mq屬性配置檔案 -->
	<context:property-placeholder location="classpath*:/mqconnect.properties" />
	<!-- mq佇列連線工廠 -->
	<bean id="jmsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
		<property name="transportType" value="${app.mq.transportType}"/>
		<property name="queueManager" value="${queue.manager}"/>
		<property name="hostName" value="${queue.manager.host}"/>
		<property name="port" value="${app.mq.port}"/>
		<property name="channel" value="${app.mq.channel}"/>
	</bean>  
	<!-- spring快取管理mq佇列連線工廠 -->
	<bean id="jmsQueueConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="jmsConnectionFactory" />
        <property name="sessionCacheSize">
            <value>5</value>
        </property>
    </bean>
	
	 <!-- MQ sender queue setup -->  
    <bean id="senderQueue" class="com.ibm.mq.jms.MQQueue"> 
        <property name="baseQueueManagerName" value="${queue.manager}"/> 
        <property name="baseQueueName" value="${queue.name}"/> 
    </bean>
    <!-- MQ receiver queue setup -->  
    <bean id="receiverQueue" class="com.ibm.mq.jms.MQQueue"> 
        <property name="baseQueueManagerName" value="${queue.manager}"/> 
        <property name="baseQueueName" value="${queue.name}"/> 
    </bean>
    
    <!-- the JMSTemplate setup for Sender Queue--> 
    <bean id="SenderJMSTemplate" class="org.springframework.jms.core.JmsTemplate">  
        <property name="connectionFactory" ref="jmsQueueConnectionFactory"/>
        <property name="defaultDestination" ref="senderQueue"/>
        <property name="pubSubDomain" value="false"/>
    </bean>
    
    <!-- the JMSTemplate setup for Receiver Queue -->  
    <bean id="ReceiverJMSTemplate" class="org.springframework.jms.core.JmsTemplate">  
        <property name="connectionFactory" ref="jmsQueueConnectionFactory"/>
        <property name="defaultDestination" ref="receiverQueue"/> 
        <property name="pubSubDomain" value="false"/> 
    </bean> 
   	 <!-- 佇列傳送程式 -->
    <bean id="jmsSender" class="com.watson.jms.JMSSender">  
        <property name="jmsTemplate" ref="SenderJMSTemplate" />  
        <property name="replyTo" ref="senderQueue" />
    </bean>
    <!-- 佇列接收程式 -->
    <bean id="jmsReceiver" class="com.watson.jms.JMSReceiver">  
        <property name="jmsTemplate" ref="ReceiverJMSTemplate"/>
        <property name="receiveQueue" ref="receiverQueue"/>
    </bean> 
    
    <!-- spring 監聽佇列,一旦佇列中有訊息,馬上觸發監聽 -->
    <bean id="jmsContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
        <property name="connectionFactory" ref="jmsQueueConnectionFactory" />  
        <property name="destination" ref="receiverQueue" />  
        <property name="messageListener" ref="jmsReceiver" />  
        <property name="concurrentConsumers" value="1" /> 
    </bean>
	
</beans>

3.mq屬性配置檔案mqconnect.properties
#192.168.5.105
#queue.manager.host=192.168.5.105
#app.mq.port=2414
#queue.manager=IB9QMGR
#app.mq.channel=SERVER2015
#app.mq.transportType=1
#queue.name=ORDER.QUEUE

#localhost
queue.manager.host=127.0.0.1
app.mq.port=2414
queue.manager=IB9QMGR
app.mq.channel=SYSTEM.DEF.SVRCONN
app.mq.transportType=1
queue.name=jms.test
4.訊息傳送程式JMSSender.java
package com.watson.jms;


import java.util.Properties;

import javax.jms.Destination;

import org.springframework.jms.core.JmsTemplate;
import com.alibaba.fastjson.JSONObject;

public class JMSSender {

	private JmsTemplate jmsTemplate;

	private Properties msgheader;

	private Destination replyTo;

	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	/**
	 * @return Returns the msgheader.
	 */
	public Properties getMsgheader() {
		return msgheader;
	}

	/**
	 * @param msgheader The msgheader to set.
	 */
	public void setMsgheader(Properties msgheader) {
		this.msgheader = msgheader;
	}

	/**
	 * @return Returns the replyTo.
	 */
	public Destination getReplyTo() {
		return replyTo;
	}

	/**
	 * @param replyTo The replyTo to set.
	 */
	public void setReplyTo(Destination replyTo) {
		this.replyTo = replyTo;
	}

	public void sendMesage(Object object) {
		JSONObject json = new JSONObject();
		json.put("orderData", object);
		jmsTemplate.convertAndSend(replyTo, json.toJSONString());
	}
}

5.訊息接收程式JMSReceiver.java。請注意此類實現了MessageListener介面,覆蓋onMessage方法,這樣就可以在spring的監聽容器中配置本來為監聽程式,一旦監聽的佇列中有訊息,就會觸發本類中的onMessage方法。
package com.watson.jms;

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

import org.springframework.jms.core.JmsTemplate;

import com.ibm.mq.jms.MQQueue;


public class JMSReceiver implements MessageListener {
	
	private JmsTemplate jmsTemplate;
	
	private MQQueue receiveQueue;
	/**
	 * @return Returns the jmsTemplate.
	 */
	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}
	/**
	 * @param jmsTemplate The jmsTemplate to set.
	 */
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
	
	/**
	 * @return Returns the receiveQueue.
	 */
	public MQQueue getReceiveQueue() {
		return receiveQueue;
	}
	/**
	 * @param receiveQueue The receiveQueue to set.
	 */
	public void setReceiveQueue(MQQueue receiveQueue) {
		this.receiveQueue = receiveQueue;
	}
	
	public void onMessage(Message message) {
		try {
			System.out.println("reviced msg is:" +(new java.util.Date())+":"+ ((TextMessage)message).getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public void processMessage(){
		Message msg = jmsTemplate.receive(receiveQueue.getBaseQueueName());
		try{
			if (msg instanceof ObjectMessage) {
				ObjectMessage objMsg = (ObjectMessage) msg;
				try {
					Person person = (Person) objMsg.getObject();
					System.out.println("使用者名稱:" + person.getName() + "年齡:"
							+ person.getAge());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}			
		}catch(Exception e){
				e.printStackTrace();
		}
		
	}

}


6.單元測試程式
package com.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.watson.jms.JMSReceiver;
import com.watson.jms.JMSSender;
import com.watson.jms.Person;

public class JMSSenderTest {
	
	@Test
	public void jmsSender() {
		ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-ibmmq.xml");
		JMSSender sender = (JMSSender)app.getBean("jmsSender");
		Person person = new Person("watson",20);
		sender.sendMesage(person);
	}
	
	@Test
	public void jmsReceiver() {
		ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-ibmmq.xml");
		JMSReceiver receiver = (JMSReceiver)app.getBean("jmsReceiver");
		receiver.processMessage();
	}

}