/[projects]/dao/DaoMqPump2/MQFilter/FilterController.cs
ViewVC logotype

Contents of /dao/DaoMqPump2/MQFilter/FilterController.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2180 - (show annotations) (download)
Tue May 20 20:12:15 2014 UTC (9 years, 11 months ago) by torben
File size: 11405 byte(s)
Make silentPeriod configurable
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Text.RegularExpressions;
6
7 using IBM.WMQ;
8
9 using Microsoft.Win32;
10
11 using DaoCommon;
12
13
14 namespace MQFilter
15 {
16 class FilterController
17 {
18
19 public string mqHost { get; private set; }
20 public string mqChannel { get; private set; }
21 public string mqQueueManager { get; private set; }
22
23 public string logDirectory { get; private set; }
24
25 public string[] filterTranscations { get; private set; }
26
27 Logfile logFile;
28
29
30 public static readonly string queueNameIndbakke = "DAO.INDBAKKE";
31 public static readonly string queueNameDimaps = "DAO.SAMLET";
32 public static readonly string queueNameMysql = "DAO.ALL";
33 public static readonly string queueNameStore = "DAO.STORE";
34
35 TimeSpan silentPeriodBegin;
36 TimeSpan silentPeriodEnd;
37
38 protected FilterController()
39 {
40 initialize();
41 logFile = new Logfile(LogfileType.LogEvents, "filter", logDirectory);
42 logFile.addSingleLogEntry("Starting service");
43 }
44
45 private void initialize()
46 {
47 Console.WriteLine("FilterController: Loading config");
48 RegistryKey key = Registry.LocalMachine.CreateSubKey("Software\\DAO\\MQFilter");
49
50
51 //Læser globale MQ Parametre
52 mqHost = (string)key.GetValue("MQHost");
53 if (mqHost == null || mqHost.Length == 0)
54 {
55 key.SetValue("MQHost", "", RegistryValueKind.String);
56 throw new System.ArgumentException("MQHost cannot be null or empty");
57 }
58
59 mqChannel = (string)key.GetValue("MQChannel");
60 if (mqChannel == null || mqChannel.Length == 0)
61 {
62 key.SetValue("MQChannel", "", RegistryValueKind.String);
63 throw new System.ArgumentException("MQChannel cannot be null or empty");
64 }
65
66 mqQueueManager = (string)key.GetValue("MQQueueManager");
67 if (mqQueueManager == null || mqQueueManager.Length == 0)
68 {
69 key.SetValue("MQQueueManager", "", RegistryValueKind.String);
70 throw new System.ArgumentException("MQQueueManager cannot be null or empty");
71 }
72
73 ////////////
74
75 logDirectory = (string)key.GetValue("LogDirectory");
76 if (logDirectory == null || logDirectory.Length == 0)
77 {
78 key.SetValue("LogDirectory", "", RegistryValueKind.String);
79 throw new System.ArgumentException("LogDirectory cannot be null or empty");
80 }
81
82 if (Directory.Exists(logDirectory) == false)
83 {
84 Directory.CreateDirectory(logDirectory);
85 }
86
87 ////////////
88
89 string silentBeginStr = (string)key.GetValue("SilentPeriodBegin");
90 if (silentBeginStr == null || silentBeginStr.Length == 0)
91 {
92 key.SetValue("SilentPeriodBegin", "", RegistryValueKind.String);
93 throw new System.ArgumentException("SilentPeriodBegin cannot be null or empty");
94 }
95 silentPeriodBegin = TimeSpan.Parse(silentBeginStr);
96
97 string silentEndStr = (string)key.GetValue("SilentPeriodEnd");
98 if (silentEndStr == null || silentEndStr.Length == 0)
99 {
100 key.SetValue("SilentPeriodEnd", "", RegistryValueKind.String);
101 throw new System.ArgumentException("SilentPeriodEnd cannot be null or empty");
102 }
103 silentPeriodEnd = TimeSpan.Parse(silentEndStr);
104
105 ////////////
106
107 String tmpFilterTransactions = (string)key.GetValue("FilterTransactions");
108 if (tmpFilterTransactions == null || tmpFilterTransactions.Length == 0)
109 {
110 key.SetValue("FilterTransactions", "", RegistryValueKind.String);
111 throw new System.ArgumentException("FilterTransactions cannot be null or empty");
112 }
113 tmpFilterTransactions = tmpFilterTransactions.Replace(';', ',');
114 filterTranscations = Regex.Split(tmpFilterTransactions, ",");
115
116 for (int i = 0; i < filterTranscations.Length; i++)
117 {
118 filterTranscations[i] = filterTranscations[i].Trim().ToUpper();
119 }
120
121 ////////////
122
123 }
124
125
126
127
128 public void transportAllMessages()
129 {
130 try
131 {
132 transportMessagesWorker();
133 }
134 catch (Exception e)
135 {
136 logFile.addSingleLogEntry("Error during transportAllMessages: " + e.Message);
137 }
138 }
139
140 private void transportMessagesWorker()
141 {
142 int messageCount = 0;
143
144 MQQueueManager mqMgr = null;
145 MQQueue queueIndbakke = null;
146 MQQueue queueMysql = null;
147 MQQueue queueDimaps = null;
148 MQQueue queueStore = null;
149
150 using (Logfile translog = new Logfile(LogfileType.LogTransactions, "filter", logDirectory))
151 try
152 {
153 //MQ options
154 Hashtable connProps = MQHelper.getConnectionProperties(mqHost, mqChannel);
155 int openInputOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING;
156 int openOutputOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
157
158
159 //MQ objects i v6 implementerer ikke IDisposable og kan derfor ikke bruges med "using" statement
160 mqMgr = new MQQueueManager(mqQueueManager, connProps);//stage 1 connect to mq
161
162
163 queueIndbakke = MQHelper.openQueueHelper(queueNameIndbakke, mqMgr, openInputOptions);
164
165 queueMysql = MQHelper.openQueueHelper(queueNameMysql, mqMgr, openOutputOptions);
166 queueDimaps = MQHelper.openQueueHelper(queueNameDimaps, mqMgr, openOutputOptions);
167 queueStore = MQHelper.openQueueHelper(queueNameStore, mqMgr, openOutputOptions);
168
169
170 bool isContinue = true;
171 while (isContinue)
172 {
173
174 MQMessage mqMsg = new MQMessage();
175 MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions();
176
177
178 try
179 {
180 queueIndbakke.Get(mqMsg, mqGetMsgOpts);
181 if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0)
182 {
183 string salt2String = mqMsg.ReadString(mqMsg.MessageLength);
184 //System.Console.WriteLine(msgString);
185
186
187 // validér at headeren er gyldig
188 if ( Salt2Helper.validateSalt2Header(salt2String) == false)
189 {
190
191
192 using (Logfile discardedlog = new Logfile(LogfileType.LogDiscarded, "filter", logDirectory))
193 {
194 discardedlog.writeLogEntry(salt2String);
195 }
196 continue; //gå frem til at tage næste transaktion fra køen
197 }
198
199 translog.writeLogEntry(salt2String);
200
201 MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
202 // same as MQPMO_DEFAULT
203
204 MQMessage msg = new MQMessage();
205 msg.Format = MQC.MQFMT_STRING;
206 msg.CharacterSet = 1252;
207 msg.WriteString(salt2String);
208
209 Salt2Header header = Salt2Helper.parseHeader(salt2String);
210 queueMysql.Put(msg, pmo);
211 if (saveForLater(header))
212 {
213 queueStore.Put(msg, pmo);
214 }
215 else
216 {
217 queueDimaps.Put(msg, pmo);
218 }
219
220
221
222 messageCount++;// increment per run message counter
223 if (messageCount >= 10000) // if we have moved 10000 messages in this run - let it go
224 {
225 isContinue = false;
226 }
227
228
229
230
231 }
232 else
233 {
234 System.Console.WriteLine("Non-text message");
235 }
236 }
237 catch (MQException mqe)
238 {
239 isContinue = false;
240
241 // report reason, if any
242 if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE)
243 {
244 // special report for normal end
245 System.Console.WriteLine("no more messages");
246 }
247 else
248 {
249 // general report for other reasons
250 System.Console.WriteLine("MQQueue::Get ended with " + mqe.Message); ;
251
252 }
253
254 }
255
256 }
257
258
259
260 }
261 finally
262 {
263 MQHelper.closeQueueSafe(queueIndbakke);
264 MQHelper.closeQueueSafe(queueMysql);
265 MQHelper.closeQueueSafe(queueDimaps);
266 MQHelper.closeQueueSafe(queueStore);
267
268 MQHelper.closeQueueManagerSafe(mqMgr);
269 }
270 }
271
272 private Boolean saveForLater(Salt2Header header)
273 {
274
275 TimeSpan now = DateTime.Now.TimeOfDay;
276
277 if (now >= silentPeriodBegin && now < silentPeriodEnd)
278 {
279
280 if (contains(header.transaktionForkortelse, this.filterTranscations) ) //Så længe vi skal være .net3.0 kompatible er LINQ problematisk (LINQ kræver 3.5)
281 {
282 return true;
283 }
284 else
285 {
286 return false;
287 }
288 }
289 else //normal operation - send straight trough
290 {
291 return false;
292 }
293
294 }
295
296 private bool contains(string needle, string[] haystack) // s
297 {
298 foreach(string hay in haystack)
299 {
300 if (needle.Equals(hay))
301 {
302 return true;
303 }
304 }
305
306 return false;
307 }
308
309
310
311
312 /* Singleton */
313 private static FilterController instance = null;
314 public static FilterController getInstance()
315 {
316 if (instance == null)
317 instance = new FilterController();
318
319 return instance;
320 }
321
322 }
323 }

  ViewVC Help
Powered by ViewVC 1.1.20