向WebSphere mq topic中put消息

package com.quest.test;

import java.io.IOException;

import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.MQTopic;
import com.ibm.mq.constants.CMQC;
import com.quest.mq.MQTool;

public class MQTest {

	/**
	 * @param args
	 * @throws MQException 
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws MQException, IOException, InterruptedException {
		
		String queueManagerName = "QM1";
		String hostname = "127.0.0.1";
		int port = 1421;
		String channel = "SYSTEM.DEF.SVRCONN";
		MQTool mqTool = new MQTool(queueManagerName, hostname, port, channel);
		MQQueueManager queueManager = mqTool.getQueueManager();
		//accessTopic方法的第一个参数为topic string,第二个参数为topic name  
		//程序会根据topic name的topic string 和你提供的topic string 组合得到消息发送到的topic string
		MQTopic topic = queueManager.accessTopic("", "a", CMQC.MQTOPIC_OPEN_AS_PUBLICATION, CMQC.MQOO_OUTPUT);
		System.out.println(topic.getName());
		
		MQMessage msg = new MQMessage();
		msg.writeString("ssssssssss");
		MQPutMessageOptions pmo = new MQPutMessageOptions();
		pmo.options = CMQC.MQPMO_ASYNC_RESPONSE;
		topic.put(msg,pmo);
		
		System.out.println(queueManager.getAsyncStatus().putSuccessCount);
		System.out.println(queueManager.getAsyncStatus().putFailureCount);
		queueManager.commit();
		topic.close();
		
		MQTopic topic2 = queueManager.accessTopic("price", "", CMQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, CMQC.MQSO_CREATE);
        
        MQGetMessageOptions option = new MQGetMessageOptions();
        topic2.get(msg, option); 

	}

}

  

猜你喜欢

转载自keepmoving2012.iteye.com/blog/1397428
今日推荐