7 |
|
|
8 |
using IBM.WMQ; |
using IBM.WMQ; |
9 |
using MySql.Data.MySqlClient; |
using MySql.Data.MySqlClient; |
10 |
|
using System.Globalization; |
11 |
|
|
12 |
namespace DaoMqPump2 |
namespace DaoMqPump2 |
13 |
{ |
{ |
16 |
|
|
17 |
enum LogfileType { |
enum LogfileType { |
18 |
LogTransactions, |
LogTransactions, |
19 |
LogEvents |
LogEvents, |
20 |
|
LogDiscarded |
21 |
} |
} |
22 |
|
|
23 |
public static string SQL2MQ = "sql2mq"; |
public static string SQL2MQ = "sql2mq"; |
141 |
|
|
142 |
private void transportSql2Mq() |
private void transportSql2Mq() |
143 |
{ |
{ |
144 |
|
MQQueueManager mqMgr = null; |
145 |
|
MQQueue out_queue = null; |
146 |
|
|
147 |
string filename = getLogFilename(LogfileType.LogTransactions); |
string filename = getLogFilename(LogfileType.LogTransactions); |
148 |
using (StreamWriter translog = new StreamWriter(filename, true) ) |
using (StreamWriter translog = new StreamWriter(filename, true) ) |
149 |
try |
try |
154 |
|
|
155 |
//MySQL Options |
//MySQL Options |
156 |
string mysqlString = buildMysqlConnString(); |
string mysqlString = buildMysqlConnString(); |
|
|
|
157 |
|
|
158 |
using (MQQueueManager mqMgr = new MQQueueManager(controller.mqQueueManager, connProps))//stage 1 connect to mq |
//MQ objects i v6 implementerer ikke IDisposable og kan derfor ikke bruges med "using" statement |
159 |
using (MQQueue out_queue = mqMgr.AccessQueue(queueName, openOptions)) |
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 |
using (MySqlConnection sqlReadConnection = new MySqlConnection(mysqlString)) //stage 2 connect to mysql |
162 |
using (MySqlConnection sqlWriteConnection = new MySqlConnection(mysqlString)) |
using (MySqlConnection sqlWriteConnection = new MySqlConnection(mysqlString)) |
163 |
{ |
{ |
189 |
MySqlCommand updateCmd = new MySqlCommand(updateSql, sqlWriteConnection); |
MySqlCommand updateCmd = new MySqlCommand(updateSql, sqlWriteConnection); |
190 |
int numrows = updateCmd.ExecuteNonQuery(); |
int numrows = updateCmd.ExecuteNonQuery(); |
191 |
|
|
192 |
translog.WriteLine(getNowString() + " " + msgString + "\n"); |
translog.WriteLine(getNowString() + " " + msgString); |
193 |
|
|
194 |
if (numrows != 1) |
if (numrows != 1) |
195 |
{ |
{ |
203 |
catch (Exception e) |
catch (Exception e) |
204 |
{ |
{ |
205 |
statusData.lastrunOk = false; |
statusData.lastrunOk = false; |
206 |
statusData.lastErrorMessage = name + ".transportSql2Mq error: " + e.Message; |
statusData.lastErrorMessage = name + ".transportMq2Sql error: " + e.GetType().FullName + " " + e.Message; |
207 |
Console.WriteLine(statusData.lastErrorMessage); |
Console.WriteLine(statusData.lastErrorMessage); |
208 |
|
Console.WriteLine(e.StackTrace); |
209 |
EventLog.WriteEntry("DaoMqPump2", statusData.lastErrorMessage, EventLogEntryType.Warning); |
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() |
private void transportMq2Sql() |
242 |
{ |
{ |
243 |
|
MQQueueManager mqMgr = null; |
244 |
|
MQQueue in_queue = null; |
245 |
string filename = getLogFilename(LogfileType.LogTransactions); |
string filename = getLogFilename(LogfileType.LogTransactions); |
246 |
using (StreamWriter translog = new StreamWriter(filename, true)) |
using (StreamWriter translog = new StreamWriter(filename, true)) |
247 |
try |
try |
|
{ |
|
|
//MQ options |
|
|
Hashtable connProps = getConnectionProperties(); |
|
|
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING; |
|
|
|
|
|
//MySQL options |
|
|
string mysqlString = buildMysqlConnString(); |
|
|
|
|
|
using (MQQueueManager mqMgr = new MQQueueManager(controller.mqQueueManager, connProps))//stage 1 connect to mq |
|
|
using (MQQueue in_queue = mqMgr.AccessQueue(queueName, openOptions) ) |
|
|
using (MySqlConnection sqlConnection = new MySqlConnection(mysqlString)) //stage 2 connect to mysql |
|
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(); |
sqlConnection.Open(); |
263 |
|
|
264 |
|
|
265 |
//stage 3 move messages |
//stage 3 move messages |
266 |
bool isContinue = true; |
bool isContinue = true; |
267 |
while (isContinue) |
while (isContinue) |
268 |
{ |
{ |
269 |
|
|
270 |
MQMessage mqMsg = new MQMessage(); |
MQMessage mqMsg = new MQMessage(); |
271 |
MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions(); |
MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions(); |
272 |
|
|
273 |
mqGetMsgOpts.Options = MQC.MQGMO_SYNCPOINT; //kræver en commit førend at den er fjernet fra køen |
mqGetMsgOpts.Options = MQC.MQGMO_SYNCPOINT; //kræver en commit førend at den er fjernet fra køen |
274 |
|
|
275 |
try |
try |
|
{ |
|
|
in_queue.Get(mqMsg, mqGetMsgOpts); |
|
|
if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0) |
|
276 |
{ |
{ |
277 |
string msgString = mqMsg.ReadString(mqMsg.MessageLength); |
in_queue.Get(mqMsg, mqGetMsgOpts); |
278 |
System.Console.WriteLine(msgString); |
if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0) |
279 |
|
{ |
280 |
|
string msgString = mqMsg.ReadString(mqMsg.MessageLength); |
281 |
|
//System.Console.WriteLine(msgString); |
282 |
|
|
|
string sql = "CALL " + mq2sqlInsertQuery + "( '" + MySqlHelper.EscapeString(msgString) + "' )"; //opbygger en CALL somestoredprocedure('msgString'); sql streng |
|
283 |
|
|
284 |
MySqlCommand sqlcmd = new MySqlCommand(sql, sqlConnection); |
// Hvis transaktionen starter med et ? er det ikke en gyldig transaktion |
285 |
int numrows = sqlcmd.ExecuteNonQuery(); |
// 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 |
|
|
|
if (numrows == 1) |
|
|
{ |
|
|
translog.WriteLine(getNowString() + " " + msgString + "\n"); |
|
|
mqMgr.Commit(); |
|
|
statusData.counter++; |
|
316 |
} |
} |
317 |
else |
else |
318 |
{ |
{ |
319 |
mqMgr.Backout(); |
System.Console.WriteLine("Non-text message"); |
|
isContinue = false; |
|
320 |
} |
} |
|
|
|
321 |
} |
} |
322 |
else |
catch (MQException mqe) |
323 |
{ |
{ |
324 |
System.Console.WriteLine("Non-text message"); |
isContinue = false; |
325 |
} |
|
326 |
} |
// report reason, if any |
327 |
catch (MQException mqe) |
if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE) |
328 |
{ |
{ |
329 |
isContinue = false; |
// 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 |
|
|
|
// report reason, if any |
|
|
if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE) |
|
|
{ |
|
|
// special report for normal end |
|
|
System.Console.WriteLine("no more messages"); |
|
|
} |
|
|
else |
|
|
{ |
|
|
// general report for other reasons |
|
|
System.Console.WriteLine("MQQueue::Get ended with " + mqe.Message); ; |
|
|
statusData.lastrunOk = false; |
|
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 |
catch (Exception e) |
{ |
374 |
{ |
try |
375 |
statusData.lastrunOk = false; |
{ |
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 |
statusData.lastErrorMessage = name + ".transportMq2Sql error: " + e.Message; |
} |
|
Console.WriteLine(statusData.lastErrorMessage); |
|
|
EventLog.WriteEntry("DaoMqPump2", statusData.lastErrorMessage, EventLogEntryType.Warning); |
|
|
} |
|
395 |
} |
} |
396 |
|
|
397 |
private string buildMysqlConnString() |
private string buildMysqlConnString() |
402 |
//connectionString += "DATABASE=" + controller.mysqlHost + ";"; |
//connectionString += "DATABASE=" + controller.mysqlHost + ";"; |
403 |
connectionString += "UID=" + controller.mysqlUser + ";"; |
connectionString += "UID=" + controller.mysqlUser + ";"; |
404 |
connectionString += "PASSWORD=" + controller.mysqlPassword + ";"; |
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; |
return connectionString; |
409 |
} |
} |
413 |
Hashtable connProperties = new Hashtable(); |
Hashtable connProperties = new Hashtable(); |
414 |
connProperties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT); |
connProperties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT); |
415 |
connProperties.Add(MQC.HOST_NAME_PROPERTY, controller.mqHost); |
connProperties.Add(MQC.HOST_NAME_PROPERTY, controller.mqHost); |
416 |
connProperties.Add(MQC.CHANNEL_PROPERTY, controller.mqChannel); //WARNING: Hardcoded Channel Value !!! |
connProperties.Add(MQC.CHANNEL_PROPERTY, controller.mqChannel); |
417 |
return connProperties; |
return connProperties; |
418 |
} |
} |
419 |
|
|
425 |
DateTime now = DateTime.Now; |
DateTime now = DateTime.Now; |
426 |
string filename = controller.logDirectory + "\\"; |
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) |
switch (type) |
434 |
{ |
{ |
435 |
case LogfileType.LogEvents: |
case LogfileType.LogEvents: |
439 |
case LogfileType.LogTransactions: |
case LogfileType.LogTransactions: |
440 |
filename += "transactionlog_"; |
filename += "transactionlog_"; |
441 |
break; |
break; |
442 |
|
case LogfileType.LogDiscarded: |
443 |
|
filename += "discardedlog_"; |
444 |
|
break; |
445 |
} |
} |
446 |
|
|
447 |
|
|
448 |
filename += name + "_" + now.Year.ToString("D4") + now.Month.ToString("D2") + ".log"; |
filename += name + "_" + now.Year.ToString("D4") + "_W" + week.ToString("D2") + ".log"; |
449 |
|
|
450 |
return filename; |
return filename; |
451 |
} |
} |
457 |
return now.ToString("s"); |
return now.ToString("s"); |
458 |
} |
} |
459 |
|
|
460 |
|
private bool validateSalt2Header(string salt2String) |
461 |
|
{ |
462 |
|
int result; |
463 |
|
|
464 |
|
string afsender = salt2String.Substring(0, 5); |
465 |
|
string modtager = salt2String.Substring(5, 5); |
466 |
|
string afsenderTegnSaet = salt2String.Substring(10, 6); |
467 |
|
string standardNavn = salt2String.Substring(16, 6); |
468 |
|
string standardVersion = salt2String.Substring(22, 3); |
469 |
|
string afsenderSekvensnr = salt2String.Substring(25, 6); |
470 |
|
string afsenderTidsstempel = salt2String.Substring(31, 14); |
471 |
|
string afsenderBakkeIdent = salt2String.Substring(45, 5); |
472 |
|
string modtagerBakkeIdent = salt2String.Substring(50, 5); |
473 |
|
string transaktionForkortelse = salt2String.Substring(55, 4); |
474 |
|
string transaktionsLaengde = salt2String.Substring(59, 5); |
475 |
|
string prioritet = salt2String.Substring(64, 1); |
476 |
|
|
477 |
|
if (int.TryParse(standardVersion.Trim(), out result) == false) // standardVersion _skal_ være en int |
478 |
|
{ |
479 |
|
return false; |
480 |
|
} |
481 |
|
|
482 |
|
if (int.TryParse(afsenderSekvensnr.Trim(), out result) == false) // afsenderSekvensnr _skal_ være en int |
483 |
|
{ |
484 |
|
return false; |
485 |
|
} |
486 |
|
|
487 |
|
if (int.TryParse(afsenderTidsstempel.Trim(), out result) == false) // afsenderTidsstempel _skal_ være en int |
488 |
|
{ |
489 |
|
return false; |
490 |
|
} |
491 |
|
|
492 |
|
if (int.TryParse(transaktionsLaengde.Trim(), out result) == false) // transaktionsLaengde _skal_ være en int |
493 |
|
{ |
494 |
|
return false; |
495 |
|
} |
496 |
|
|
497 |
|
if ( int.TryParse(prioritet.Trim(), out result) == false ) // prioritet _skal_ være en int |
498 |
|
{ |
499 |
|
return false; |
500 |
|
} |
501 |
|
|
502 |
|
return true; |
503 |
|
} |
504 |
|
|
505 |
private void addLogEntry(string msg) |
private void addLogEntry(string msg) |
506 |
{ |
{ |
507 |
msg = getNowString() + " " + msg; |
msg = getNowString() + " " + msg; |