IBM WebSphere MQ C#


使用VMware虛擬機搭建

A機:192.168.163.130 MQ8.0

B機:192.168.163.129 MQ7.0

第一次使用amqmdnet.dll,amqmdxcs.dll,imqb23vn.dll,mqic32.dll開發的WMQ通信小工具,必須要本地安裝一套webSphere MQ客戶端才能通信,如下圖的哪兩個玩意。為此使用非常不爽,而且連接8.0的時候要使用8.0的dll。

 

本次不需要安裝插件也能正常通信的小工具,如下圖:

上圖是為了測試兩台WMQ設備A到B通信過程,當然用一台WMQ(本機入隊、出隊)測試也是沒有問題的,如下圖:

廢話少說直接上核心代碼:

  1 using com.ibm.mq;
  2 using com.ibm.mq.headers.pcf;
  3 using com.ibm.msg.client.jms;
  4 using com.ibm.msg.client.wmq.common;
  5 using javax.jms;
  6 using System;
  7 using System.Collections;
  8 using System.Collections.Generic;
  9 using System.Linq;
 10 using System.Text;
 11 using System.Threading.Tasks;
 12 using System.Windows;
 13 using Queue = javax.jms.Queue;
 14 
 15 namespace WPFIbmMqClientTools
 16 {
 17     /// <summary>
 18     /// https://blog.csdn.net/MePlusPlus/article/details/52847580
 19     /// </summary>
 20     public class WMQHelper
 21     {
 22         public static string HostName;
 23         public static int Port;
 24         /// <summary>
 25         /// 隊列管理器名稱
 26         /// </summary>
 27         public static string QmName;
 28         /// <summary>
 29         /// 通道名稱
 30         /// </summary>
 31         public static string ChannelName;
 32 
 33         private static JMSContext mQQueueManagerJMS = null;
 34         private static Queue mQQueue = null;
 35         private static JMSProducer producer = null;
 36         private static JMSConsumer consumer = null;
 37         private static MQQueueManager mQQueueManager = null;
 38         private static MQQueue mQQueueSend = null;
 39         private static MQQueue mQQueueRecv = null;
 40         private static MQQueue mQQueueDepth = null;
 41         //private static MQQueue mQQueueDepth = null;
 42 
 43         public static string CreateWQMConnect()
 44         {
 45             string result = "隊列管理器連接失敗";
 46             if (mQQueueManager == null)
 47             {
 48                 try
 49                 {
 50                     //var ff = JmsFactoryFactory.getInstance(JmsConstants.__Fields.WMQ_PROVIDER);
 51                     //var cf = ff.createConnectionFactory();
 52                     //cf.setIntProperty(CommonConstants.__Fields.WMQ_CONNECTION_MODE, CommonConstants.__Fields.WMQ_CM_CLIENT);
 53                     //cf.setStringProperty(CommonConstants.__Fields.WMQ_HOST_NAME, HostName);
 54                     //cf.setIntProperty(CommonConstants.__Fields.WMQ_PORT, Port);
 55                     //cf.setStringProperty(CommonConstants.__Fields.WMQ_CHANNEL, ChannelName);
 56                     //cf.setStringProperty(CommonConstants.__Fields.WMQ_QUEUE_MANAGER, QmName);
 57                     ////cf.setStringProperty(CommonConstants.__Fields.WMQ_APPLICATIONNAME, "JMS WMQ EXAMPLE");
 58                     ////cf.setStringProperty(CommonConstants.USERID, "EXAMPLE_USER");
 59                     ////mQQueueManagerJMS = cf.createContext();
 60 
 61                     //配置MQ服務器連接參數
 62                     MQEnvironment.hostname = HostName;
 63                     MQEnvironment.port = Port;
 64                     MQEnvironment.channel = ChannelName;
 65                     //設置應用名稱,方便服務器MQ 查看應用連接
 66                     //MQEnvironment.properties.put(CommonConstants.USERID, "MQ Test By Java");
 67                     mQQueueManager = new MQQueueManager(QmName);
 68                     result = "隊列管理器連接成功";
 69                 }
 70                 catch (MQException ex)
 71                 {
 72                     MessageBox.Show(result + "  CompCode:" + ex.getCompCode() + "   Reason:" + ex.getReason() + "   ERROR:" + ex.getErrorCode());
 73                 }
 74                 catch (Exception ex)
 75                 {
 76                     MessageBox.Show(result + ex.Message);
 77                 }
 78             }
 79             else
 80             {
 81                 result = "隊列管理器連接成功";
 82             }
 83             return result;
 84         }
 85 
 86         public static bool PutMessage(string queueName, string body)
 87         {
 88             bool result = false;
 89             try
 90             {
 91                 //if (mQQueue == null)
 92                 //{
 93                 //    //mQQueue = mQQueueManager.createQueue("queue:///" + queueName + "");
 94                 //    mQQueue = mQQueueManagerJMS.createQueue(queueName);
 95                 //}
 96                 //if (producer == null)
 97                 //{
 98                 //    producer = mQQueueManager.createProducer();
 99                 //}
100                 ////producer.send(mQQueue, body);
101 
102 
103                 if (mQQueueSend == null)
104                 {
105                     //mQQueueSend = mQQueueManager.accessQueue(queueName, com.ibm.mq.constants.CMQC.MQOO_OUTPUT);
106                     mQQueueSend = mQQueueManager.accessQueue(queueName, MQC.MQOO_OUTPUT);
107                 }
108                 MQMessage message = new MQMessage();
109                 message.writeUTF(body);
110                 mQQueueSend.put(message);
111                 result = true;
112             }
113             catch (Exception ex)
114             {
115                 TraceHelper.Instance.Error("入隊消息異常", ex);
116             }
117             return result;
118         }
119 
120         /// <summary>
121         /// 
122         /// </summary>
123         /// <param name="queueName">隊列名稱</param>
124         /// <returns>-1 失敗 0未存在數據  1獲取到數據</returns>
125         public static int GetMessage(string queueName)
126         {
127             int result = 0;
128             try
129             {
130                 //if (mQQueue == null)
131                 //{
132                 //    //mQQueue = mQQueueManager.createQueue("queue:///" + queueName + "");
133                 //    mQQueue = mQQueueManagerJMS.createQueue(queueName);
134                 //}
135                 //if (consumer == null)
136                 //{
137                 //    consumer = mQQueueManagerJMS.createConsumer(mQQueue);
138                 //}
139                 //var message = consumer.receive();
140                 //var body = message?.getBody(typeof(String)) as string;
141 
142                 if (mQQueueRecv == null)
143                 {
144                     //mQQueueRecv = mQQueueManager.accessQueue(queueName, com.ibm.mq.constants.CMQC.MQOO_INPUT_AS_Q_DEF 
145                     //    | com.ibm.mq.constants.CMQC.MQOO_FAIL_IF_QUIESCING 
146                     //    | com.ibm.mq.constants.CMQC.MQOO_INQUIRE);
147                     mQQueueRecv = mQQueueManager.accessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INQUIRE);
148                 }
149                 var currentDepth = mQQueueRecv.getCurrentDepth();
150                 if (currentDepth > 0)
151                 {
152                     MQMessage message = new MQMessage();
153                     mQQueueRecv.get(message);
154                     result = 1;
155                     //if (string.IsNullOrEmpty(message.ReadString(message.MessageLength)) == false)
156                     //{
157                     //    result = 1;
158                     //}
159                 }
160             }
161             catch (Exception ex)
162             {
163                 result = -1;
164                 TraceHelper.Instance.Error("出隊消息異常", ex);
165             }
166             return result;
167         }
168 
169         public static int GetCurrentDepth(string queueName)
170         {
171             int result = -1;
172             try
173             {
174                 if (mQQueueDepth == null && mQQueueManager != null)
175                 {
176                     mQQueueDepth = mQQueueManager.accessQueue(queueName, MQC.MQOO_INQUIRE);
177                 }
178                 if (mQQueueDepth != null)
179                 {
180                     result = mQQueueDepth.getCurrentDepth();
181                 }
182             }
183             catch (Exception ex)
184             {
185                 TraceHelper.Instance.Error("獲取深度消息異常", ex);
186             }
187             return result;
188         }
189 
190     }
191 }
View Code

主要使用IbmMqClient

本次上傳的源碼文件包含com.ibm.mq.allclient-9.0.4.0.dll和amqmdnet.dll兩個版本。

在使用com.ibm.mq.allclient-9.0.4.0.dll(源https://github.com/mustaddon/IbmMqClient)開發過程中主要參考https://blog.csdn.net/MePlusPlus/article/details/52847580該博主代碼。

因為cnblogs附件上傳10M限制,所以源碼上傳到百度網盤進行下載

源碼下載地址:鏈接:https://pan.baidu.com/s/1hTPVDF5sp7nfsui6x0ROOQ   提取碼:mxjc 

 遠程連接我遇到三個種錯誤分享一下

常見問題Reason:2035-MCA

配置MCA(MUSR_MQADMIN)開啟遠程訪問權限,如下圖

    

常見問題Reason:2035-用戶過濾

 

  

常見問題權限過濾

  1. 首先右鍵點擊隊列管理器名稱(QM_AAA)。
  2. 然后在擴展中設置安全策略為不受支持。
  3. 最后把連接認證信息清空,點擊應用后在點擊確認就可以了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM