天天看点

入门新手可以看看,关于Java连接WebsphereMQ(也就是MQSeries)的内容

        目前我正在实习,我的leader布置我研究WebsphereMQ。

        WebsphereMQ可以在IBM的网站下载,安装之后有两个要注意的点。

        第一个有可能出现的问题是:AMQ7077: 你未被授权来进行请求的操作。这个问题是没有用管理员身份运行程序造成的,要以管理员身份重新启动程序。

        第二个可能出现的问题是:AMQ7257:安装“Installation1”(C:\Program Files (x86)\IBM\WebSphere MQ) 的 MQ服务必须正在运行。这个问题是没有开启服务项造成的。点计算机-管理-服务-把IBM MQ的服务项启用。

        接下来说配置的问题,我通过虚拟机模拟了另外一台电脑进行信息的接收,主机作为信息的发送方(虚拟机和主机都安装了WebsphereMQ)。(在上一个帖子中我写的就是关于虚拟机ip的问题,可以对这篇文章进行补充)在主机的WebsphereMQ上要做以下的操作:

        1、建立实现发送作用的队列管理器send,(端口号默认的是1414,可以自己随便改一个,但要记住了,待会会用到)

        2、在send中建立本地队列XQ,用法设置为传输

        3、继续建立远程队列RQ,远程队列为LQ(这个待会在虚拟机上建立),远程队列管理器是receive(这个也将在虚拟机上创建),传输队列是XQ

        4、创建发送方的通道SToR,连接名称的格式是:xxx.xxx.xxx.xxx(xxxx) , 也就是:虚拟机的ip,然后一个括号,里头写虚拟机上的接收队列管理器receive的端口号;传输队列是XQ

        5、创建服务器连接通道,起名DC.SVRCONN,这个要记住了,待会要用到。

        主机配置完毕,接下来看虚拟机的操作:

        6、建立队列管理器receive,在步骤3有提到过

        7、建立本地队列LQ,在步骤3有提到过

        8、建立接收方通道 SToR (和步骤4中的发送方通道相对应)

        9、同5,创建服务器连接通道,名称必须一样

        这些配置工作做完之后就可以做测试了,在主机的RQ上放入测试信息,启动传输通道,然后在虚拟机上的LQ查看是否收到信息。

        接下来是用java实现方面的了,推荐大家看一些东西,我看的是《MQ编程模式中文版7》,自己百度一下是可以下载到这份PDF的。

        在主机上编程的时候会用到com.ibm.mq的jar包,这个jar包eclips是不带的,所以要导入。具体的文件可以在MQ的安装目录下的java/lib中找到。

        以下是我的代码

import java.io.IOException;
import java.util.Hashtable;

import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

public class MQ0{
	//定义队列管理器和队列的名称(发送方的参数)
	private static String qmName = "QM0";
	private static String qName = "RQ8";
	private static MQQueueManager qMgr;
	private static Hashtable properties = new Hashtable();

	
	public static void main(String args[]) {
		
		try {			
			//以下参数是接收机器的参数
			properties.put("hostname", "192.168.1.2");
			properties.put("port", new Integer(1414));
			properties.put("channel", "DC.SVRCONN");
			properties.put("CCSID", new Integer(1381));
			properties.put("transport","MQSeries");
			
		
			// 建立与队列管理器的连接。Create a connection to the queue manager
			
			//MQQueueManager qMgr = new MQQueueManager(qmName,properties);
			//不知道为什么多了一个properties就不行了
			MQQueueManager qMgr = new MQQueueManager(qmName);
			
			// 设置希望打开的选项???什么意思Set up the options on the queue we wish to open...
			int openOptions = 16;
			
			
			// Now specify the queue that we wish to open,
			// and the open options...
			//选择队列、打开选项
			MQQueue remoteQ = qMgr.accessQueue(qName, openOptions);
			
			//创建一条信息
			// Define a simple WebSphere MQ message, and write some text in UTF format..
			MQMessage putMessage = new MQMessage();
			putMessage.writeUTF("test123456");
			
			// specify the message options...
			MQPutMessageOptions pmo = new MQPutMessageOptions();
			

			// accept the defaults, same as MQPMO_DEFAULT
			// put the message on the queue
		    //将消息放入队列
			remoteQ.put(putMessage,pmo);
			System.out.println("Message has been input into the Remote Queue");
			
			// 关闭队列。Close the queue...
			remoteQ.close();
			
			// 断开连接。Disconnect from the queue manager
			qMgr.disconnect();
		}
		/*
		 * 异常处理代码			 * 
		 */
		catch (MQException ex) {
			// If an error has occurred in the above, try to identify what went wrong
			// Was it a WebSphere MQ error?
			System.out.println("A WebSphere MQ error occurred : Completion code " + ex.completionCode + " Reason code " + ex.reasonCode);
		}
		catch (IOException ex) {
			// Was it a Java buffer space error?
			System.out.println("An error occurred whilst writing to the message buffer: " + ex);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
           

        运行之后如果结果正常的话就会有“Message has been input into the Remote Queue”这样的提示。接着在虚拟机中查看LQ中是否存在消息。

       接下来是接受方的代码,可在虚拟机上接收。

import java.io.IOException;
import java.util.Hashtable;
import com.ibm.mq.*;

public class MQ8{
	//定义队列管理器和队列的名称(发送方的参数)
	private static String qmName = "recieve";
	private static String qName = "LQ";
	private static MQQueueManager qMgr;
	
	public static void main(String args[]) {
		
		try {				
			// 建立与队列管理器的连接。Create a connection to the queue manager
			
			MQQueueManager qMgr = new MQQueueManager(qmName);
			
			// 设置选项
			int openOptions = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING;;
			
			
			//选择队列、打开选项
			MQQueue remoteQ = qMgr.accessQueue(qName, openOptions);
			
			//创建一条空的信息用于接收
				MQMessage getMessage = new MQMessage();
		
				
			//MQPutMessageOptions pmo = new MQPutMessageOptions();
			
				MQGetMessageOptions gmo = new MQGetMessageOptions();
			     // Get messages under sync point control.
			     // 在同步点控制下获取消息.
			     gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;
			     // Wait if no messages on the Queue.
			     // 如果在队列上没有消息则等待.
			     gmo.options = gmo.options + MQC.MQGMO_WAIT;
			     // Fail if QeueManager Quiescing.
			     // 如果队列管理器停顿则失败.
			     gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;
			     // Sets the time limit for the wait.
			     // 设置等待的时间限制.
			     gmo.waitInterval = 3000;		
		
			     
		    //获取消息
			remoteQ.get(getMessage,gmo);
			System.out.println("Message has been got from the Remote Queue");
			String msgString = getMessage.readUTF();
		    System.out.println(" The Message from the Queue is :  " + msgString);
		    

			// 关闭队列。
			remoteQ.close();
			
			// 断开连接。
			qMgr.disconnect();
		}
		/*
		 * 异常处理代码			 * 
		 */
		catch (MQException ex) {
			System.out.println("A WebSphere MQ error occurred : Completion code " + ex.completionCode + " Reason code " + ex.reasonCode);
		}
		catch (IOException ex) {			
			System.out.println("An error occurred whilst writing to the message buffer: " + ex);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
}