C#ベースのIBMメッセージキュー操作クライアント
22305 ワード
背景:
XXプロジェクトをするには取引のニュースをYYシステムに送る必要があります.
また、MQを選択した理由は、YYシステムのダウンタイムを防止し、受信メッセージを受信できないためです.
インプリメンテーション
1、IBM WebSphere MQクライアントのインストール
2、amqmdnet.dllを参照する(クライアントのインストールディレクトリの下で見つけることができる)
3、C#コードは以下の通り
XXプロジェクトをするには取引のニュースをYYシステムに送る必要があります.
また、MQを選択した理由は、YYシステムのダウンタイムを防止し、受信メッセージを受信できないためです.
インプリメンテーション
1、IBM WebSphere MQクライアントのインストール
2、amqmdnet.dllを参照する(クライアントのインストールディレクトリの下で見つけることができる)
3、C#コードは以下の通り
1 /*
2 * write by:wjf
3 * date:2015-05-20
4 * dec:MQ
5 *
6 */
7 using System;
8 using System.Diagnostics;
9 using System.IO;
10 using IBM.WMQ;
11
12 namespace RsaTest
13 {
14 public class IbmMQClient : IDisposable
15 {
16 // MQ IP ;
17 private string hostname = "182.180.80.241";
18 //private const string hostname = "182.180.80.243";
19 //MQ
20 //private const int port = 54221;
21 private readonly int port;
22 // ;
23 private const int openOptions = MQC.MQOO_OUTPUT; // MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
24
25 // ;
26 private readonly MQQueueManager qManager;
27 //
28 private readonly string qName;
29 // MQ ;
30 private readonly string channel;
31 // ;
32 //private string qManagerName;
33
34 public IbmMQClient(string qManagerName, string channel, string qName, string sPort, string hostname)
35 {
36 //this.qManagerName = qManagerName;
37 this.channel = channel;
38 this.qName = qName;
39 this.port = Convert.ToInt32(sPort);
40 this.hostname = hostname;
41 InitQueueEnv();
42 qManager = new MQQueueManager(qManagerName);
43 }
44
45 /// <summary>
46 ///
47 /// </summary>
48 private void InitQueueEnv()
49 {
50 // MQ , MQ ;
51 // MQ ;
52 MQEnvironment.Hostname = hostname;
53 // MQ ;
54 MQEnvironment.Channel = channel;
55 // MQ ;
56 MQEnvironment.Port = port;
57 //MQEnvironment.UserId = "mqm";
58 }
59
60 public void SendMessage(string message)
61 {
62 if (qManager == null)
63 {
64 throw new Exception(" 。");
65 }
66 // ;
67 MQQueue lq = qManager.AccessQueue(qName, openOptions);
68
69 try
70 {
71 // , ;
72 MQMessage mo = new MQMessage();
73 // ( )
74 mo.CharacterSet = 1208;
75 // ;
76 //mo.WriteUTF(message);
77 mo.WriteString(message);
78 //
79 mo.Format = MQC.MQFMT_STRING;
80 // ;
81 MQPutMessageOptions pmo = new MQPutMessageOptions();
82 // ;
83 lq.Put(mo, pmo);
84 }
85 finally
86 {
87 try
88 {
89 // ;
90 lq.Close();
91 }
92 catch (Exception e)
93 {
94 Debug.WriteLine(e);
95 }
96 }
97 }
98
99 public void SendFile(string filePath)
100 {
101 if (qManager == null)
102 {
103 throw new Exception(" 。");
104 }
105 // ;
106 MQQueue lq = qManager.AccessQueue(qName, openOptions);
107
108 try
109 {
110 // , ;
111 MQMessage mo = new MQMessage();
112 //
113 FileInfo fi = new FileInfo(filePath);
114 //
115 mo.WriteInt8(fi.Length);
116 //
117 mo.WriteUTF(fi.Name);
118 //
119 mo.Write(File.ReadAllBytes(filePath));
120 // ;
121 MQPutMessageOptions pmo = new MQPutMessageOptions();
122 // ;
123 lq.Put(mo, pmo);
124 }
125 finally
126 {
127 try
128 {
129 // ;
130 lq.Close();
131 }
132 catch (Exception e)
133 {
134 Debug.WriteLine(e);
135 }
136 }
137 }
138
139 ~IbmMQClient()
140 {
141 Close();
142 }
143
144 /// <summary>
145 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
146 /// </summary>
147 /// <filterpriority>2</filterpriority>
148 public void Dispose()
149 {
150 Close();
151 }
152
153 public void Close()
154 {
155 if (qManager != null)
156 {
157 try
158 {
159 // ;
160 qManager.Disconnect();
161 }
162 catch (Exception e)
163 {
164 Debug.WriteLine(e);
165 }
166 }
167 }
168 }
169 }