MQ java 基础编程(一)

时间:2025-02-09 16:04:25

package ;

 

import ;

import ;

import ;

 

import ;

import ;

import ;

import ;

import ;

import ;

 

/**

 * @author Administrator

 *

 * TODO To change the template for this generated type comment go to Window -

 * Preferences - Java - Code Style - Code Templates

 */

public class MQGet {

       private static String HOST_URL = "192.168.1.116";

 

       private static String MQ_CHANNEL = "";

 

       private static String MQ_MANAGER = "QMGR";

 

       private static String MQ_QUEUE = "";

 

       private static int MQ_PORT = 4001;

 

       public static void main(String[] args) {

              ();

       }

 

       public static void getMessage() {

              try {

                     /* 设置MQEnvironment 属性以便客户机连接 */

                     = HOST_URL;

                     = MQ_CHANNEL;

                     (MQC.TRANSPORT_PROPERTY,

                                   MQC.TRANSPORT_MQSERIES);

                     = 1381;

                     = MQ_PORT;

 

                     /* 连接到队列管理器 */

                     MQQueueManager qMgr = new MQQueueManager(MQ_MANAGER);

                     ("queue manager is connected!");

 

                     /*

                      * 设置打开选项以便打开用于输出的队列,如果队列管理器停止,我们也 已设置了选项去应对不成功情况

                      */

                     int openOptions = MQC.MQOO_INPUT_SHARED

                                   | MQC.MQOO_FAIL_IF_QUIESCING;

 

                     /* 打开队列 */

                     queue = (MQ_QUEUE, openOptions);

                     ("队列连接成功");

                     /* 设置放置消息选项 */

                     MQGetMessageOptions gmo = new MQGetMessageOptions();

 

                     /* 在同步点控制下获取消息 */

                     = + MQC.MQGMO_SYNCPOINT;

 

                     /* 如果在队列上没有消息则等待 */

                     = + MQC.MQGMO_WAIT;

 

                     /* 如果队列管理器停顿则失败 */

                     = + MQC.MQGMO_FAIL_IF_QUIESCING;

 

                     /* 设置等待的时间限制 */

                     = MQC.MQWI_UNLIMITED;

                     /* create the message buffer store */

                     MQMessage inMessage = new MQMessage();

                     /* set the selector */

                     = "clinet_B_receive".getBytes();

                     /* get the message */

                     (inMessage, gmo);

                     ("get message success");

                    

                     /* 读出消息对象 */

                     Hashtable messageObject = (Hashtable) ();

                     (messageObject);

                     /* 读出消息内容 */

                     byte[] content = (byte[]) ("content");

                     /* save file */

                     FileOutputStream output = new FileOutputStream(

                                   "f:/");

                     (content);

                     ();

 

                     (("FileName"));

                     /* 提交事务,相当于确认消息已经接收,服务器会删除该消息 */

                     ();

 

              } catch (MQException e) {

                     ();

              } catch (IOException e) {

                     // TODO Auto-generated catch block

                     ();

              } catch (ClassNotFoundException e) {

                     // TODO Auto-generated catch block

                     ();

              }

       }

}