goods goods study, day day Up !

C# 连接 IBM MQ

安装 IBM WebSphere MQ:http://www-01.ibm.com/software/integration/wmq/explorer/downloads/

 

 正确安装要注意几个地方,集群,通道发送方和通道接收方。

    要想正确发送消息,必须具备以下条件:

    1、传输队列。(本地队列,用法选传输)

    2、消息通道(注意填写链接名称是对方IP,传输队列都填上)

    3、远程队列(注意填写远程队列和远程队列管理器名称)

    然后往远程队列里面填写数据,就会发送到远程的队列了!!

    C#连接IBMMQ需要引用以下几个DLL,该DLLIBMMQ安装目录bin文件夹下!amqmdnet.dllimqc23vn.dllmqic32.dllamqmdnet.dll

    代码如下:

   

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using IBM.WMQ.PCF;
  5 using IBM.WMQ;
  6 
  7 
  8 
  9 namespace test
 10 {
 11 
 12     internal class IBMMQClass
 13     {
 14 
 15         public void CreateQueueManage(string qmName)
 16         {
 17 
 18             throw new Exception("不会创建!");
 19 
 20         }
 21 
 22         /// <summary>
 23 
 24         /// 创建本地队列
 25 
 26         /// </summary>
 27 
 28         /// <param name="qmName">队列管理器名称</param>
 29 
 30         /// <param name="queueName">本地队列名称</param>
 31 
 32         public void CreateQueue(string qmName, string queueName)
 33         {
 34 
 35             PCFMessageAgent agent = new PCFMessageAgent(qmName);
 36 
 37             PCFMessage request = new PCFMessage(CMQCFC.MQCMD_CREATE_Q);
 38 
 39             request.AddParameter(MQC.MQCA_Q_NAME, queueName);
 40 
 41             request.AddParameter(MQC.MQIA_Q_TYPE, MQC.MQQT_LOCAL);
 42 
 43             PCFMessage[] response = agent.Send(request);
 44 
 45             agent.Disconnect();
 46 
 47         }
 48 
 49         /// <summary>
 50 
 51         /// 创建远程队列
 52 
 53         /// </summary>
 54 
 55         /// <param name="qmName">队列管理器</param>
 56 
 57         /// <param name="queueName">队列名称</param>
 58 
 59         /// <param name="ycqmName">远程队列管理器名称</param>
 60 
 61         /// <param name="ycqueueName">远程队列名称</param>
 62 
 63         /// <param name="csqueueName">传输队列名称</param>
 64 
 65         public void CreateYuanchengQueue(string qmName, string queueName, string ycqmName, string ycqueueName, string csqueueName)
 66         {
 67 
 68             PCFMessageAgent agent = new PCFMessageAgent(qmName);
 69 
 70             PCFMessage request = new PCFMessage(CMQCFC.MQCMD_CREATE_Q);
 71 
 72             request.AddParameter(MQC.MQCA_Q_NAME, queueName);
 73 
 74             request.AddParameter(MQC.MQIA_Q_TYPE, MQC.MQQT_REMOTE);
 75 
 76             request.AddParameter(MQC.MQCA_REMOTE_Q_MGR_NAME, ycqmName);
 77 
 78             request.AddParameter(MQC.MQCA_REMOTE_Q_NAME, ycqueueName);
 79 
 80             request.AddParameter(MQC.MQCA_XMIT_Q_NAME, csqueueName);
 81 
 82             PCFMessage[] response = agent.Send(request);
 83 
 84             agent.Disconnect();
 85 
 86         }
 87 
 88         /// <summary>
 89 
 90         /// 创建通道
 91 
 92         /// </summary>
 93 
 94         /// <param name="isSend">True为发送方,false为接收方</param>
 95 
 96         public void CreateChannel(bool isSend)
 97         {
 98 
 99             PCFMessageAgent agent = new PCFMessageAgent("QM_zhang");
100 
101             PCFMessage request = new PCFMessage(CMQCFC.MQCMD_CREATE_CHANNEL);
102 
103             request.AddParameter(CMQCFC.MQCACH_CHANNEL_NAME, "mama");
104 
105             if (!isSend)
106             {
107 
108                 request.AddParameter(CMQCFC.MQIACH_CHANNEL_TYPE, MQC.MQCHT_RECEIVER);
109 
110             }
111 
112             else
113             {
114 
115                 request.AddParameter(CMQCFC.MQIACH_CHANNEL_TYPE, MQC.MQCHT_SENDER);
116 
117                 request.AddParameter(CMQCFC.MQCACH_CONNECTION_NAME, "192.168.3.232");
118 
119                 request.AddParameter(CMQCFC.MQCACH_XMIT_Q_NAME, "bb");
120 
121             }
122 
123             PCFMessage[] response = agent.Send(request);
124 
125             agent.Disconnect();
126 
127         }
128 
129         /// <summary>
130 
131         /// 删除队列
132 
133         /// </summary>
134 
135         /// <param name="qmName">队列管理器名称</param>
136 
137         /// <param name="queueName">队列名称</param>
138 
139         public void DeleteQueue(string qmName, string queueName)
140         {
141 
142             PCFMessageAgent agent = new PCFMessageAgent(qmName);
143 
144             PCFMessage request = new PCFMessage(CMQCFC.MQCMD_DELETE_Q);
145 
146             request.AddParameter(MQC.MQCA_Q_NAME, queueName);
147 
148             PCFMessage[] response = agent.Send(request);
149 
150             agent.Disconnect();
151 
152         }
153 
154         /// <summary>
155 
156         /// 发送消息
157 
158         /// </summary>
159 
160         /// <param name="qmName">队列管理器</param>
161 
162         /// <param name="queueName">队列名称</param>
163 
164         /// <param name="body">消息内容</param>
165 
166         public void PutMessage(string qmName, string queueName, string body)
167         {
168 
169             MQQueueManager qMgr = new MQQueueManager(qmName);
170 
171             MQQueue queue = qMgr.AccessQueue(queueName, MQC.MQOO_OUTPUT);
172 
173             try
174             {
175 
176                 MQMessage message = new MQMessage();
177 
178                 message.WriteString(body);
179 
180                 message.Format = MQC.MQFMT_STRING;
181 
182                 queue.Put(message);
183 
184             }
185 
186             catch { }
187 
188             finally
189             {
190 
191                 queue.Close();
192 
193             }
194 
195         }
196 
197         /// <summary>
198 
199         /// 获取消息
200 
201         /// </summary>
202 
203         /// <param name="qmName">队列管理器名称</param>
204 
205         /// <param name="queueName">队列名称</param>
206 
207         /// <returns>消息内容</returns>
208 
209         public string GetMessage(string qmName, string queueName)
210         {
211 
212             MQQueueManager qMgr = new MQQueueManager(qmName);
213 
214             MQQueue queue = qMgr.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING);
215 
216             try
217             {
218 
219                 MQMessage message = new MQMessage();
220 
221                 queue.Get(message);
222 
223                 return message.ReadString(message.MessageLength);
224 
225             }
226 
227             catch
228 
229             { }
230 
231             finally
232             {
233 
234                 queue.Close();
235 
236             }
237 
238         }
239 
240         /// <summary>
241 
242         /// 获取消息
243 
244         /// </summary>
245 
246         /// <param name="qmName">队列管理器名称</param>
247 
248         /// <param name="queueName">队列名称</param>
249 
250         /// <param name="timeInterval">等待时间(毫秒)</param>
251 
252         /// <param name="qMgr">队列管理器实体类</param>
253 
254         /// <returns>消息内容</returns>
255 
256         public string GetMessage(string qmName, string queueName, int timeInterval, out MQQueueManager qMgr)
257         {
258 
259             MQQueueManager qMgr = new MQQueueManager(qmName);
260 
261             MQQueue queue = qMgr.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING);
262 
263             MQGetMessageOptions gmo = new MQGetMessageOptions();
264 
265             gmo.Options = MQC.MQGMO_WAIT;
266 
267             gmo.WaitInterval = timeInterval;
268 
269             gmo.MatchOptions = MQC.MQMO_NONE;
270 
271             MQMessage message = new MQMessage();
272 
273             queue.Get(message, gmo);
274 
275             return message.ReadString(message.MessageLength);
276 
277         }
278 
279     }
280 
281 }

 

posted @ 2018-04-20 17:13  赤子丹心520  阅读(3517)  评论(0编辑  收藏  举报