• Java连接MQ的实例, 测试类


    1. package cjf.mq.mqclient;  
    2.   
    3.   
    4. import com.ibm.mq.MQC;  
    5. import com.ibm.mq.MQEnvironment;  
    6. import com.ibm.mq.MQException;  
    7. import com.ibm.mq.MQGetMessageOptions;  
    8. import com.ibm.mq.MQMessage;  
    9. import com.ibm.mq.MQPutMessageOptions;  
    10. import com.ibm.mq.MQQueue;  
    11. import com.ibm.mq.MQQueueManager;  
    12. import com.ibm.mq.MQGetMessageOptions;  
    13.   
    14. public class MQClient {  
    15.   
    16.     static MQQueueManager qMgr;  
    17.     static int CCSID = 1381;//WINGBK,1208:UTF-8  
    18.     static String queueString = "MQ_QUEUE";  
    19.   
    20.     public static void connect() throws MQException {  
    21.         MQEnvironment.hostname = "";  
    22.         MQEnvironment.channel = "java.channel";  
    23.         MQEnvironment.port = 1321;  
    24.         MQEnvironment.CCSID = CCSID;  
    25.   
    26.         qMgr = new MQQueueManager("MQ_TEST");  
    27.         qMgr.disconnect();  
    28.     }  
    29.   
    30.     public static void sendMsg(String msgStr) {  
    31.         int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT  
    32.                 | MQC.MQOO_INQUIRE;  
    33.         MQQueue queue = null;  
    34.         try {  
    35.             // 建立Q1通道的连接  
    36.             queue = qMgr  
    37.                     .accessQueue(queueString, openOptions, null, null, null);  
    38.             MQMessage msg = new MQMessage();// 要写入队列的消息  
    39.             msg.format = MQC.MQFMT_STRING;  
    40.             msg.characterSet = CCSID;  
    41.             msg.encoding = CCSID;  
    42.             // msg.writeObject(msgStr); //将消息写入消息对象中  
    43.             msg.writeString(msgStr);  
    44.             MQPutMessageOptions pmo = new MQPutMessageOptions();  
    45.             msg.expiry = -1; // 设置消息用不过期  
    46.             queue.put(msg, pmo);// 将消息放入队列  
    47.         } catch (Exception e) {  
    48.             // TODO Auto-generated catch block  
    49.             e.printStackTrace();  
    50.         } finally {  
    51.             if (queue != null) {  
    52.                 try {  
    53.                     queue.close();  
    54.                 } catch (MQException e) {  
    55.                     // TODO Auto-generated catch block  
    56.                     e.printStackTrace();  
    57.                 }  
    58.             }  
    59.         }  
    60.     }  
    61.   
    62.     public static void receiveMsg() {  
    63.         int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT  
    64.                 | MQC.MQOO_INQUIRE;  
    65.         MQQueue queue = null;  
    66.         try {  
    67.             queue = qMgr  
    68.                     .accessQueue(queueString, openOptions, null, null, null);  
    69.             System.out.println("该队列当前的深度为:" + queue.getCurrentDepth());  
    70.             System.out.println("===========================");  
    71.             int depth = queue.getCurrentDepth();  
    72.             // 将队列的里的消息读出来  
    73.             while (depth-- > 0) {  
    74.                 MQMessage msg = new MQMessage();// 要读的队列的消息  
    75.                 MQGetMessageOptions gmo = new MQGetMessageOptions();  
    76.                 queue.get(msg, gmo);  
    77.                   
    78.                 System.out.println("消息的大小为:" + msg.getDataLength());  
    79.                 System.out.println("消息的内容: "  
    80.                         + msg.readLine());  
    81.                 System.out.println("---------------------------");  
    82.             }  
    83.         } catch (Exception e) {  
    84.             // TODO Auto-generated catch block  
    85.             e.printStackTrace();  
    86.         } finally {  
    87.             if (queue != null) {  
    88.                 try {  
    89.                     queue.close();  
    90.                 } catch (MQException e) {  
    91.                     // TODO Auto-generated catch block  
    92.                     e.printStackTrace();  
    93.                 }  
    94.             }  
    95.         }  
    96.     }  
    97.   
    98.     public static void main(String[] args) throws MQException {  
    99.         connect();  
    100.         sendMsg("fuck MQ");  
    101.         receiveMsg();  
    102.     }  
    103. }  
  • 相关阅读:
    游戏中的View开发
    下载更新包,并且在任务栏提示进度.
    调用android非unbind的服务中的方法(不使用bindService启动的服务)
    判断android系统中Service是否在运行的方法
    如何从Activity中调用Service中的方法
    android官方文档---应用程序基础---服务的理解(app Component---services)
    怎么通过文件名的String,找到对应的资源ID
    疯狂Java之JDBC 笔记
    android应用将json数据打包在本地,进行读取的代码
    STL 堆的使用
  • 原文地址:https://www.cnblogs.com/haitaofeiyang/p/7531086.html
Copyright © 2020-2023  润新知