论坛首页 Java企业应用论坛

java操作MQ

浏览 2794 次
锁定老帖子 主题:java操作MQ
精华帖 (0) :: 良好帖 (0) :: 新手帖 (11) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-12-30  


package mq;

import java.io.IOException;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

public class MQMain {

	public static void main(String[] args) throws Exception {
		sendMsg();
		getMsg();
	}

	static void getMsg() throws MQException, IOException {
		MQEnvironment.hostname =  "172.25.1.69";
		MQEnvironment.port = 1414;
		MQEnvironment.CCSID = 1381;
		MQEnvironment.channel = "ch1";
		MQQueueManager qMgr = new MQQueueManager("QM_APPLE");

		int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT	| MQC.MQOO_INQUIRE;

		MQQueue queue = qMgr.accessQueue("Q1_Local", openOptions, null, null, null);
		
		MQMessage msg = new MQMessage();// 要读的队列的消息
		MQGetMessageOptions gmo = new MQGetMessageOptions( );
		gmo.options = MQC.MQGMO_SYNCPOINT;
		
		int currDepth = queue.getCurrentDepth();
		
		System.out.println(currDepth);
		
		if (currDepth > 0) {
			queue.get(msg, gmo);
			qMgr.commit();
			qMgr.close();
			System.out.println(msg.readStringOfCharLength(msg.getDataLength()));
		}
	}

	static void sendMsg() throws MQException, IOException {
		MQEnvironment.hostname =  "172.25.1.69";
		MQEnvironment.port = 1414;
		MQEnvironment.CCSID = 1381;
		MQEnvironment.channel = "ch1";
		MQQueueManager qMgr = new MQQueueManager("QM_APPLE");

		int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;

		MQQueue queue = qMgr.accessQueue("Q1_Local", openOptions, null, null, null);
		MQMessage msg = new MQMessage();// 要写入队列的消息
		msg.writeString("111111111111111111111111"); //将消息写入消息对象中
		MQPutMessageOptions pmo = new MQPutMessageOptions();
		pmo.options = MQC.MQPMO_SYNCPOINT;
		msg.expiry = -1; // 设置消息用不过期
		queue.put(msg, pmo);// 将消息放入队列
		qMgr.commit();
		System.out.println("发送成功");
	}
}







论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics