/[projects]/dao/DaoMqPump2/DaoMqPump2/Transport.cs
ViewVC logotype

Contents of /dao/DaoMqPump2/DaoMqPump2/Transport.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2087 - (show annotations) (download)
Wed Nov 27 09:47:46 2013 UTC (10 years, 5 months ago) by torben
File size: 21044 byte(s)
Correct log message
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5
6 using System.Diagnostics;
7
8 using IBM.WMQ;
9 using MySql.Data.MySqlClient;
10 using System.Globalization;
11
12 namespace DaoMqPump2
13 {
14 public class Transport
15 {
16
17 enum LogfileType {
18 LogTransactions,
19 LogEvents,
20 LogDiscarded
21 }
22
23 public static string SQL2MQ = "sql2mq";
24 public static string MQ2SQL = "mq2sql";
25
26 //private bool enabled;
27
28 TransportController controller;
29
30 StatusData statusData = new StatusData();
31
32 public string name { get; private set; }
33 public string direction { get; private set; }
34 public string queueName { get; private set; }
35 public string mq2sqlInsertQuery { get; private set; }
36 public string sql2mqReadQuery { get; private set; }
37 public string sql2mqUpdateQuery { get; private set; }
38
39 //public bool lastrunOk { get; private set; }
40 //public string lastErrorMessage { get; private set; }
41
42 //public string lastOkTime { get; private set; }
43 //public string lastErrorTime { get; private set; }
44 //public string lastTransferTime { get; private set; }
45
46 //public int counter { get; private set; }
47
48 public StatusData TransportStatusData
49 {
50 get
51 {
52 return this.statusData;
53 }
54 }
55
56
57 public bool Enabled
58 {
59 get {
60 return statusData.transportEnabled;
61 }
62 set
63 {
64 statusData.transportEnabled = value;
65 if (value == true)
66 {
67 this.addLogEntry("Transport enabled");
68 }
69 else
70 {
71 this.addLogEntry("Transport disabled");
72 }
73 }
74 }
75
76
77 private LinkedList<string> logEntries = new LinkedList<string>();
78
79
80 public Transport(TransportController controller, string name, string direction, string queueName, string mq2sqlInsertQuery, string sql2mqReadQuery, string sql2mqUpdateQuery, bool enabled)
81 {
82 this.controller = controller;
83 this.name = name;
84 this.direction = direction;
85 this.queueName = queueName;
86 this.mq2sqlInsertQuery = mq2sqlInsertQuery;
87 this.sql2mqReadQuery = sql2mqReadQuery;
88 this.sql2mqUpdateQuery = sql2mqUpdateQuery;
89
90 statusData.transportEnabled = enabled;
91
92
93 statusData.lastrunOk = true;
94 statusData.counter = 0;
95 statusData.lastErrorMessage = statusData.lastOkTime = statusData.lastErrorTime = "";
96
97 addLogEntry( "Starting ... " );
98 }
99
100 ~Transport()
101 {
102 addLogEntry("Stopping ... ");
103 }
104
105
106 public void transportMessages()
107 {
108 if (statusData.transportEnabled == false)
109 return;
110
111 Console.WriteLine(name + " -> transportMessages() ");
112 statusData.lastrunOk = true;
113
114 int startCounter = statusData.counter;
115
116 if (direction == SQL2MQ)
117 {
118 transportSql2Mq();
119 }
120 else
121 {
122 transportMq2Sql();
123 }
124
125 if (statusData.lastrunOk == true)
126 {
127 statusData.lastOkTime = getNowString();
128
129 if (statusData.counter != startCounter)
130 {
131 //Vi har transporteret beskeder - så gemmer vi lige transfer tidspunktet
132 statusData.lastTransferTime = getNowString();
133 }
134 }
135 else
136 {
137 addLogEntry(statusData.lastErrorMessage);
138 statusData.lastErrorTime = getNowString();
139 }
140 }
141
142 private void transportSql2Mq()
143 {
144 MQQueueManager mqMgr = null;
145 MQQueue out_queue = null;
146
147 string filename = getLogFilename(LogfileType.LogTransactions);
148 using (StreamWriter translog = new StreamWriter(filename, true) )
149 try
150 {
151 //MQ Options
152 Hashtable connProps = getConnectionProperties();
153 int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
154
155 //MySQL Options
156 string mysqlString = buildMysqlConnString();
157
158 //MQ objects i v6 implementerer ikke IDisposable og kan derfor ikke bruges med "using" statement
159 mqMgr = new MQQueueManager(controller.mqQueueManager, connProps);//stage 1 connect to mq
160 out_queue = mqMgr.AccessQueue(queueName, openOptions);
161 using (MySqlConnection sqlReadConnection = new MySqlConnection(mysqlString)) //stage 2 connect to mysql
162 using (MySqlConnection sqlWriteConnection = new MySqlConnection(mysqlString))
163 {
164 sqlReadConnection.Open();
165 sqlWriteConnection.Open();
166
167 //stage 3 move messages
168 string readSql = "CALL " + sql2mqReadQuery + "()";
169 MySqlCommand readCmd = new MySqlCommand(readSql, sqlReadConnection);
170 MySqlDataReader dataReader = readCmd.ExecuteReader();
171 while (dataReader.Read())
172 {
173 int id = dataReader.GetInt32(0);
174 string msgString = dataReader.GetString(1);
175
176 MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
177 // same as MQPMO_DEFAULT
178
179 MQMessage msg = new MQMessage();
180 msg.Format = MQC.MQFMT_STRING;
181 msg.CharacterSet = 1252;
182 msg.WriteString(msgString);
183
184 out_queue.Put(msg, pmo);
185
186 //now that the message has been put on queue mark it as such
187
188 string updateSql = "CALL " + sql2mqUpdateQuery + "(" + id + ")";
189 MySqlCommand updateCmd = new MySqlCommand(updateSql, sqlWriteConnection);
190 int numrows = updateCmd.ExecuteNonQuery();
191
192 translog.WriteLine(getNowString() + " " + msgString);
193
194 if (numrows != 1)
195 {
196 break;
197 }
198 statusData.counter++;
199 }
200
201 }
202 }
203 catch (Exception e)
204 {
205 statusData.lastrunOk = false;
206 statusData.lastErrorMessage = name + ".transportMq2Sql error: " + e.GetType().FullName + " " + e.Message;
207 Console.WriteLine(statusData.lastErrorMessage);
208 Console.WriteLine(e.StackTrace);
209 EventLog.WriteEntry("DaoMqPump2", statusData.lastErrorMessage, EventLogEntryType.Warning);
210 }
211 finally
212 {
213
214 if (out_queue != null && out_queue.IsOpen)
215 {
216 try
217 {
218 out_queue.Close();
219 }
220 catch (Exception e)
221 {
222 Console.WriteLine("Error cleaning up qmgr " + e.Message);
223 }
224 }
225
226 if (mqMgr != null && mqMgr.IsOpen)
227 {
228 try
229 {
230 mqMgr.Close();
231 }
232 catch (Exception e)
233 {
234 Console.WriteLine("Error cleaning up qmgr " + e.Message);
235 }
236 }
237
238 }
239 }
240
241 private void transportMq2Sql()
242 {
243 MQQueueManager mqMgr = null;
244 MQQueue in_queue = null;
245 string filename = getLogFilename(LogfileType.LogTransactions);
246 using (StreamWriter translog = new StreamWriter(filename, true))
247 try
248 {
249 //MQ options
250 Hashtable connProps = getConnectionProperties();
251 int openOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING;
252
253 //MySQL options
254 string mysqlString = buildMysqlConnString();
255
256 //MQ objects i v6 implementerer ikke IDisposable og kan derfor ikke bruges med "using" statement
257 mqMgr = new MQQueueManager(controller.mqQueueManager, connProps);//stage 1 connect to mq
258 in_queue = mqMgr.AccessQueue(queueName, openOptions);
259 using (MySqlConnection sqlConnection = new MySqlConnection(mysqlString)) //stage 2 connect to mysql
260 {
261
262 sqlConnection.Open();
263
264
265 //stage 3 move messages
266 bool isContinue = true;
267 while (isContinue)
268 {
269
270 MQMessage mqMsg = new MQMessage();
271 MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions();
272
273 mqGetMsgOpts.Options = MQC.MQGMO_SYNCPOINT; //kræver en commit førend at den er fjernet fra køen
274
275 try
276 {
277 in_queue.Get(mqMsg, mqGetMsgOpts);
278 if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0)
279 {
280 string msgString = mqMsg.ReadString(mqMsg.MessageLength);
281 //System.Console.WriteLine(msgString);
282
283
284 // Hvis transaktionen starter med et ? er det ikke en gyldig transaktion
285 // validér ligeledes at headeren er gyldig
286 if ( msgString.StartsWith("?") || validateSalt2Header(msgString) == false )
287 {
288 string discarded_filename = getLogFilename(LogfileType.LogDiscarded);
289 using (StreamWriter discardedlog = new StreamWriter(discarded_filename, true))
290 {
291 discardedlog.WriteLine(getNowString() + " " + msgString);
292 }
293 mqMgr.Commit();//fjern den afviste transaktion fra køen
294 statusData.discardedCounter++;
295 continue; //gå frem til at tage næste transaktion fra køen
296 }
297
298
299 string sql = "CALL " + mq2sqlInsertQuery + "( '" + MySqlHelper.EscapeString(msgString) + "' )"; //opbygger en CALL somestoredprocedure('msgString'); sql streng
300
301 MySqlCommand sqlcmd = new MySqlCommand(sql, sqlConnection);
302 int numrows = sqlcmd.ExecuteNonQuery();
303
304 if (numrows == 1)
305 {
306 translog.WriteLine(getNowString() + " " + msgString);
307 mqMgr.Commit();
308 statusData.counter++;
309 }
310 else
311 {
312 mqMgr.Backout();
313 isContinue = false;
314 }
315
316 }
317 else
318 {
319 System.Console.WriteLine("Non-text message");
320 }
321 }
322 catch (MQException mqe)
323 {
324 isContinue = false;
325
326 // report reason, if any
327 if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE)
328 {
329 // special report for normal end
330 System.Console.WriteLine("no more messages");
331 }
332 else
333 {
334 // general report for other reasons
335 System.Console.WriteLine("MQQueue::Get ended with " + mqe.Message); ;
336 statusData.lastrunOk = false;
337 }
338
339 }
340
341
342 }
343
344 }
345
346 }
347 catch (Exception e)
348 {
349 //Det vil være mest korrekt at Rollback/backout MQ Transaktionen her - for at være sikker på at Message'n fjernes fra køen
350 try
351 {
352 if (mqMgr != null)
353 {
354 mqMgr.Backout();
355 }
356 }
357 catch (Exception e2)
358 {
359 this.addLogEntry("Error backing out from MQ Transaction: " + e2.Message);
360 }
361
362 statusData.lastrunOk = false;
363
364 statusData.lastErrorMessage = name + ".transportMq2Sql error: " + e.GetType().FullName + " " + e.Message;
365 Console.WriteLine(statusData.lastErrorMessage);
366 Console.WriteLine(e.StackTrace);
367 EventLog.WriteEntry("DaoMqPump2", statusData.lastErrorMessage, EventLogEntryType.Warning);
368 }
369 finally
370 {
371
372 if (in_queue != null && in_queue.IsOpen)
373 {
374 try
375 {
376 in_queue.Close();
377 }
378 catch (Exception e)
379 {
380 Console.WriteLine("Error cleaning up qmgr " + e.Message);
381 }
382 }
383
384 if (mqMgr != null && mqMgr.IsOpen)
385 {
386 try
387 {
388 mqMgr.Close();
389 } catch (Exception e) {
390 Console.WriteLine("Error cleaning up qmgr " + e.Message);
391 }
392 }
393
394 }
395 }
396
397 private string buildMysqlConnString()
398 {
399 string connectionString = "";
400
401 connectionString += "SERVER=" + controller.mysqlHost + ";";
402 //connectionString += "DATABASE=" + controller.mysqlHost + ";";
403 connectionString += "UID=" + controller.mysqlUser + ";";
404 connectionString += "PASSWORD=" + controller.mysqlPassword + ";";
405 connectionString += "Max Pool Size=20;";
406 //connectionString += "ConnectionReset=true;";//ConnectionReset kræver muligvis at Default Database er angivet - det virker ihvertfald ikke uden
407
408 return connectionString;
409 }
410
411 private Hashtable getConnectionProperties()
412 {
413 Hashtable connProperties = new Hashtable();
414 connProperties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
415 connProperties.Add(MQC.HOST_NAME_PROPERTY, controller.mqHost);
416 connProperties.Add(MQC.CHANNEL_PROPERTY, controller.mqChannel);
417 return connProperties;
418 }
419
420
421
422 private string getLogFilename(LogfileType type)
423 {
424
425 DateTime now = DateTime.Now;
426 string filename = controller.logDirectory + "\\";
427
428 //Find uge nr
429 DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
430 Calendar myCal = CultureInfo.InvariantCulture.Calendar;//System.Globalization
431 int week = myCal.GetWeekOfYear(now, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
432
433 switch (type)
434 {
435 case LogfileType.LogEvents:
436 filename += "eventlog_";
437 break;
438
439 case LogfileType.LogTransactions:
440 filename += "transactionlog_";
441 break;
442 case LogfileType.LogDiscarded:
443 filename += "discardedlog_";
444 break;
445 }
446
447
448 filename += name + "_" + now.Year.ToString("D4") + "_W" + week.ToString("D2") + ".log";
449
450 return filename;
451 }
452
453 public string getNowString()
454 {
455 DateTime now = DateTime.Now;
456
457 return now.ToString("s");
458 }
459
460 private bool validateSalt2Header(string salt2String)
461 {
462 if (salt2String.Length < 66)
463 {
464 addLogEntry("Transaction too short - discarding");
465 return false;
466 }
467
468
469 int result;
470 long result_long;
471
472 string afsender = salt2String.Substring(0, 5);
473 string modtager = salt2String.Substring(5, 5);
474 string afsenderTegnSaet = salt2String.Substring(10, 6);
475 string standardNavn = salt2String.Substring(16, 6);
476 string standardVersion = salt2String.Substring(22, 3);
477 string afsenderSekvensnr = salt2String.Substring(25, 6);
478 string afsenderTidsstempel = salt2String.Substring(31, 14);
479 string afsenderBakkeIdent = salt2String.Substring(45, 5);
480 string modtagerBakkeIdent = salt2String.Substring(50, 5);
481 string transaktionForkortelse = salt2String.Substring(55, 4);
482 string transaktionsLaengde = salt2String.Substring(59, 5);
483 string prioritet = salt2String.Substring(64, 1);
484
485
486
487 if (int.TryParse(standardVersion.Trim(), out result) == false) // standardVersion _skal_ være en int
488 {
489 addLogEntry("standardVersion not an integer, discarding");
490 return false;
491 }
492
493 if (int.TryParse(afsenderSekvensnr.Trim(), out result) == false) // afsenderSekvensnr _skal_ være en int
494 {
495 addLogEntry("afsenderSekvensnr not an integer, discarding");
496 return false;
497 }
498
499 if (long.TryParse(afsenderTidsstempel.Trim(), out result_long) == false) // afsenderTidsstempel _skal_ være en long
500 {
501 addLogEntry("afsenderTidsstempel not a long integer, discarding");
502 return false;
503 }
504
505 if (int.TryParse(transaktionsLaengde.Trim(), out result) == false) // transaktionsLaengde _skal_ være en int
506 {
507 addLogEntry("transaktionsLaengde not an integer, discarding");
508 return false;
509 }
510
511 if ( int.TryParse(prioritet.Trim(), out result) == false ) // prioritet _skal_ være en int
512 {
513 addLogEntry("prioritet not an integer, discarding");
514 return false;
515 }
516
517 return true;
518 }
519
520 private void addLogEntry(string msg)
521 {
522 msg = getNowString() + " " + msg;
523 lock (logEntries)
524 {
525 logEntries.AddFirst(msg);
526
527 if (logEntries.Count > 20)
528 {
529 logEntries.RemoveLast();
530 }
531 }
532
533 string filename = getLogFilename(LogfileType.LogEvents);
534 using (StreamWriter eventlog = new StreamWriter(filename, true))
535 {
536 eventlog.WriteLine(msg);
537 }
538 }
539
540 public string[] getLog()
541 {
542 lock(logEntries)
543 {
544 List<string> tmpEntries = new List<string>();
545 foreach (string s in logEntries)
546 {
547 tmpEntries.Add(s);
548 }
549 return tmpEntries.ToArray();
550 }
551 }
552
553 }
554 }

  ViewVC Help
Powered by ViewVC 1.1.20