JAVA 连接IBM MQ的topic

1:java 连接ibm mq的topic首先需要在mq上建立topic,命令如下
     DEFINE TOPIC(TOPIC_xx_2) TOPICSTR(NBA/LAKER) DESCR('This is a TOPIC') PUB(ASPARENT) SUB(ENABLED)  WILDCARD(PASSTHRU)
至于队列管理器的创建见日志中的MQ队列管理建立日志
2:java连接程序



import com.ibm.mq.MQEnvironment;
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.pcf.CMQC;

/**
 *
 *@company XX
 *@author: XX
 *@since: 2011-9-13
 *@version:1.0
 */
public class MQTopicTest {
    public static void main(String[] args){
        MQTopicTest temp = new MQTopicTest();
        temp.send();
        temp.receive();
    }
    public void send(){
        try{
            MQEnvironment.hostname = "172.17.102.2";
            MQEnvironment.channel = "CHANNEL_xx_2";
            MQEnvironment.port = 8080;
            MQEnvironment.CCSID = 1381;
            MQEnvironment.properties.put(CMQC.TRANSPORT_PROPERTY,
                    CMQC.TRANSPORT_MQSERIES);
            MQQueueManager queueManager= new MQQueueManager("QM_xx_2");
            MQTopic topic = queueManager.accessTopic("TOPIC_xx_2", "TOPIC_xx_2", CMQC.MQTOPIC_OPEN_AS_PUBLICATION, CMQC.MQOO_OUTPUT);
            MQPutMessageOptions option = new MQPutMessageOptions();
            option.options = CMQC.MQPMO_FAIL_IF_QUIESCING | CMQC.MQPMO_RETAIN;;
            MQMessage msg = new MQMessage();
            msg.expiry = 5*60*1000;
            msg.writeObject("test");
            msg.setStringProperty("test", "1");
            topic.put(msg,option);
            queueManager.commit();
        }catch(Exception e){
            System.out.println(e);
        }
    }
    public void receive(){
        try{
            MQEnvironment.hostname = "172.17.102.2";
            MQEnvironment.channel = "CHANNEL_xx_2";
            MQEnvironment.port = 8080;
            MQEnvironment.CCSID = 1381;
            MQEnvironment.properties.put(CMQC.TRANSPORT_PROPERTY,
                    CMQC.TRANSPORT_MQSERIES);
            MQQueueManager queueManager= new MQQueueManager("QM_xx_2");
            MQTopic topic = queueManager.accessTopic("TOPIC_xx_2", "TOPIC_xx_2", CMQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, CMQC.MQSO_CREATE);
            MQMessage msg = new MQMessage();
            MQGetMessageOptions option = new MQGetMessageOptions();
            topic.get(msg, option);       
            queueManager.commit();
            System.out.println(msg.getStringProperty("test"));
        }catch(Exception e){
            System.out.println(e);
        }
    }
}

你可能感兴趣的:(java)