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中引用依赖


            com.ibm.mq
            jmqi
            ${ibm.mq.version}
        
        
            com.ibm.mq
            headers
            ${ibm.mq.version}
        
        
            com.ibm
            mq
            ${ibm.mq.version}
        
        
            com.ibm.mq
            jms
            ${ibm.mq.version}
        
        
            com.ibm.mq
            dhbcore
            ${ibm.mq.version}
        
        
            com.ibm
            mqjms
            ${ibm.mq.version}
        
        
            com.ibm.mq
            connector
            ${ibm.mq.version}
        
        
            org.springframework
            spring-jms
            ${org.springframework.version}
        
2.配置applicationContext-ibmmq.xml文件



	MQ
	
	
	
	
		
		
		
		
		
	  
	
	
        
        
            5
        
    
	
	   
     
         
         
    
      
     
         
         
    
    
     
      
        
        
        
    
    
      
      
        
         
         
     
   	 
      
          
        
    
    
      
        
        
     
    
    
      
          
          
          
         
    
	


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();
	}

}


你可能感兴趣的:(java,MQ)