服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 开发工具 > 查看文档

java 中使用websphere mq

    java 中使用websphere

    websphere mq  : 用于传输信息 具有跨平台的功能。
1 安装websphere mq 并启动
2 websphere mq 建立 queue manager (如:mqsi_sample_qm)
3 建立queue 类型选择 local类型 的 (如lq  )
3 建立channels 类型选择server connection (如bridgechannel)


    java 代码如下:


package test.mq;

import com.ibm.mq.*;

/*
 * 成功的访问mq 的java 类
 */
public class firstmqtest {
//    public static void main(string[] args[]){
//        firstmqtest first = new firstmqtest();
//        first.test();
//    }
    public static void main(string args[]){
        firstmqtest first = new firstmqtest();
        first.test();
        
    }
    public void test(){
        string qmanager = "mqsi_sample_qm"; //queuemanager name
          string qname = "lq";//queue name
          try {
                    //configure connection parameters
                    mqenvironment.hostname="172.16.17.123";//mq server name or ip
                    //mqenvironment.port=1414;//listenr port
                    mqenvironment.channel="bridgechannel";//server-connection channel
                    mqenvironment.ccsid =1381;
                    // create a connection to the queuemanager
                    system.out.println("connecting to queue manager: "+qmanager);
                    mqqueuemanager qmgr = new mqqueuemanager(qmanager);
                    // set up the options on the queue we wish to open 
                    int openoptions = mqc.mqoo_input_as_q_def | mqc.mqoo_output;
                    // now specify the queue that we wish to open and the open options
                    system.out.println("accessing queue: "+qname);
                    mqqueue queue = qmgr.accessqueue(qname, openoptions);
                    // define a simple websphere mq message ... 
                    mqmessage msg = new mqmessage();
                    // ... and write some text in utf8 format
                    msg.writeutf("hello, world!");
                    // specify the default put message options
                    mqputmessageoptions pmo = new mqputmessageoptions();
                    // put the message to the queue
                    system.out.println("sending a message...");
                    
                    /*
                     * 在此测试一下 mq 的传输次列
                     * 
                     */
                     for(int j=0;j<5;j++){
                         string str ="test11111111111";
                         str = str+j;
                         msg.writeutf(str);
                         queue.put(msg, pmo);
                     }
                    queue.put(msg, pmo);
                    // now get the message back again. first define a websphere mq message 
                    // to receive the data 
                    mqmessage rcvmessage = new mqmessage();
                    
                    // specify default get message options 
                    mqgetmessageoptions gmo = new mqgetmessageoptions();
                    // get the message off the queue.
                    system.out.println("...and getting the message back again");
                    queue.get(rcvmessage, gmo);
                    // and display the message text...
                    string msgtext = rcvmessage.readutf();
                    system.out.println("the message is: " + msgtext);
                    // close the queue
                    system.out.println("closing the queue");
                    queue.close();
                    // disconnect from the queuemanager
                    system.out.println("disconnecting from the queue manager");
                    qmgr.disconnect();
                    system.out.println("done!");
                }
                catch (mqexception ex) {
                    system.out.println("a websphere mq error occured : completion code "
                            + ex.completioncode + " reason code " + ex.reasoncode);
                }
                catch (java.io.ioexception ex) {
                    system.out.println("an ioexception occured whilst writing to the message buffer: "
                            + ex);
                }

    }
}

扫描关注微信公众号