12 |
{ |
{ |
13 |
public class Transport |
public class Transport |
14 |
{ |
{ |
15 |
|
|
16 |
|
enum LogfileType { |
17 |
|
LogTransactions, |
18 |
|
LogEvents |
19 |
|
} |
20 |
|
|
21 |
public static string SQL2MQ = "sql2mq"; |
public static string SQL2MQ = "sql2mq"; |
22 |
public static string MQ2SQL = "mq2sql"; |
public static string MQ2SQL = "mq2sql"; |
23 |
|
|
24 |
|
//private bool enabled; |
|
public bool enabled { get; set; } |
|
25 |
|
|
26 |
TransportController controller; |
TransportController controller; |
27 |
|
|
28 |
|
StatusData statusData = new StatusData(); |
29 |
|
|
30 |
public string name { get; private set; } |
public string name { get; private set; } |
31 |
public string direction { get; private set; } |
public string direction { get; private set; } |
32 |
public string queueName { get; private set; } |
public string queueName { get; private set; } |
34 |
public string sql2mqReadQuery { get; private set; } |
public string sql2mqReadQuery { get; private set; } |
35 |
public string sql2mqUpdateQuery { get; private set; } |
public string sql2mqUpdateQuery { get; private set; } |
36 |
|
|
37 |
public bool lastrunOk { get; private set; } |
//public bool lastrunOk { get; private set; } |
38 |
public string lastErrorMessage { get; private set; } |
//public string lastErrorMessage { get; private set; } |
39 |
|
|
40 |
|
//public string lastOkTime { get; private set; } |
41 |
|
//public string lastErrorTime { get; private set; } |
42 |
|
//public string lastTransferTime { get; private set; } |
43 |
|
|
44 |
public string lastOkTime { get; private set; } |
//public int counter { get; private set; } |
45 |
public string lastErrorTime { get; private set; } |
|
46 |
|
public StatusData TransportStatusData |
47 |
|
{ |
48 |
|
get |
49 |
|
{ |
50 |
|
return this.statusData; |
51 |
|
} |
52 |
|
} |
53 |
|
|
54 |
|
|
55 |
|
public bool Enabled |
56 |
|
{ |
57 |
|
get { |
58 |
|
return statusData.transportEnabled; |
59 |
|
} |
60 |
|
set |
61 |
|
{ |
62 |
|
statusData.transportEnabled = value; |
63 |
|
if (value == true) |
64 |
|
{ |
65 |
|
this.addLogEntry("Transport enabled"); |
66 |
|
} |
67 |
|
else |
68 |
|
{ |
69 |
|
this.addLogEntry("Transport disabled"); |
70 |
|
} |
71 |
|
} |
72 |
|
} |
73 |
|
|
|
public int counter { get; private set; } |
|
74 |
|
|
75 |
private LinkedList<string> logEntries = new LinkedList<string>(); |
private LinkedList<string> logEntries = new LinkedList<string>(); |
76 |
|
|
85 |
this.sql2mqReadQuery = sql2mqReadQuery; |
this.sql2mqReadQuery = sql2mqReadQuery; |
86 |
this.sql2mqUpdateQuery = sql2mqUpdateQuery; |
this.sql2mqUpdateQuery = sql2mqUpdateQuery; |
87 |
|
|
88 |
this.enabled = enabled; |
statusData.transportEnabled = enabled; |
89 |
|
|
90 |
|
|
91 |
lastrunOk = true; |
statusData.lastrunOk = true; |
92 |
counter = 0; |
statusData.counter = 0; |
93 |
lastErrorMessage = lastOkTime = lastErrorTime = ""; |
statusData.lastErrorMessage = statusData.lastOkTime = statusData.lastErrorTime = ""; |
94 |
|
|
95 |
addLogEntry( "Starting ... " ); |
addLogEntry( "Starting ... " ); |
96 |
} |
} |
97 |
|
|
98 |
|
~Transport() |
99 |
|
{ |
100 |
|
addLogEntry("Stopping ... "); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
public void transportMessages() |
public void transportMessages() |
105 |
{ |
{ |
106 |
if (enabled == false) |
if (statusData.transportEnabled == false) |
107 |
return; |
return; |
108 |
|
|
109 |
Console.WriteLine(name + " -> transportMessages() "); |
Console.WriteLine(name + " -> transportMessages() "); |
110 |
lastrunOk = true; |
statusData.lastrunOk = true; |
111 |
|
|
112 |
|
int startCounter = statusData.counter; |
113 |
|
|
114 |
if (direction == SQL2MQ) |
if (direction == SQL2MQ) |
115 |
{ |
{ |
120 |
transportMq2Sql(); |
transportMq2Sql(); |
121 |
} |
} |
122 |
|
|
123 |
if (lastrunOk == true) |
if (statusData.lastrunOk == true) |
124 |
{ |
{ |
125 |
lastOkTime = getNowString(); |
statusData.lastOkTime = getNowString(); |
126 |
|
|
127 |
|
if (statusData.counter != startCounter) |
128 |
|
{ |
129 |
|
//Vi har transporteret beskeder - så gemmer vi lige transfer tidspunktet |
130 |
|
statusData.lastTransferTime = getNowString(); |
131 |
|
} |
132 |
} |
} |
133 |
else |
else |
134 |
{ |
{ |
135 |
addLogEntry(lastErrorMessage); |
addLogEntry(statusData.lastErrorMessage); |
136 |
lastErrorTime = getNowString(); |
statusData.lastErrorTime = getNowString(); |
137 |
} |
} |
138 |
} |
} |
139 |
|
|
140 |
private void transportSql2Mq() |
private void transportSql2Mq() |
141 |
{ |
{ |
142 |
string filename = getTransactionlogFilename(); |
MQQueueManager mqMgr = null; |
143 |
|
MQQueue out_queue = null; |
144 |
|
|
145 |
|
string filename = getLogFilename(LogfileType.LogTransactions); |
146 |
using (StreamWriter translog = new StreamWriter(filename, true) ) |
using (StreamWriter translog = new StreamWriter(filename, true) ) |
147 |
try |
try |
148 |
{ |
{ |
149 |
//stage 1 connect to mq |
//MQ Options |
150 |
Hashtable connProps = getConnectionProperties(); |
Hashtable connProps = getConnectionProperties(); |
|
MQQueueManager mqMgr = new MQQueueManager(controller.mqQueueManager, connProps); |
|
151 |
int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING; |
int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING; |
152 |
|
|
153 |
MQQueue out_queue = mqMgr.AccessQueue(queueName, openOptions); |
//MySQL Options |
|
|
|
|
|
|
|
//stage 2 connect to mysql |
|
154 |
string mysqlString = buildMysqlConnString(); |
string mysqlString = buildMysqlConnString(); |
|
MySqlConnection sqlConnection = new MySqlConnection(mysqlString); |
|
|
sqlConnection.Open(); |
|
|
|
|
155 |
|
|
156 |
//stage 3 move messages |
//MQ objects i v6 implementerer ikke IDisposable og kan derfor ikke bruges med "using" statement |
157 |
string readSql = "CALL " + sql2mqReadQuery + "()"; |
mqMgr = new MQQueueManager(controller.mqQueueManager, connProps);//stage 1 connect to mq |
158 |
MySqlCommand readCmd = new MySqlCommand(readSql, sqlConnection); |
out_queue = mqMgr.AccessQueue(queueName, openOptions); |
159 |
MySqlDataReader dataReader = readCmd.ExecuteReader(); |
using (MySqlConnection sqlReadConnection = new MySqlConnection(mysqlString)) //stage 2 connect to mysql |
160 |
while (dataReader.Read()) |
using (MySqlConnection sqlWriteConnection = new MySqlConnection(mysqlString)) |
161 |
{ |
{ |
162 |
int id = dataReader.GetInt32(0); |
sqlReadConnection.Open(); |
163 |
string msgString = dataReader.GetString(1); |
sqlWriteConnection.Open(); |
164 |
|
|
165 |
MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, |
//stage 3 move messages |
166 |
// same as MQPMO_DEFAULT |
string readSql = "CALL " + sql2mqReadQuery + "()"; |
167 |
|
MySqlCommand readCmd = new MySqlCommand(readSql, sqlReadConnection); |
168 |
|
MySqlDataReader dataReader = readCmd.ExecuteReader(); |
169 |
|
while (dataReader.Read()) |
170 |
|
{ |
171 |
|
int id = dataReader.GetInt32(0); |
172 |
|
string msgString = dataReader.GetString(1); |
173 |
|
|
174 |
MQMessage msg = new MQMessage(); |
MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, |
175 |
msg.Format = MQC.MQFMT_STRING; |
// same as MQPMO_DEFAULT |
|
msg.CharacterSet = 1252; |
|
|
msg.WriteString(msgString); |
|
176 |
|
|
177 |
out_queue.Put(msg, pmo); |
MQMessage msg = new MQMessage(); |
178 |
|
msg.Format = MQC.MQFMT_STRING; |
179 |
|
msg.CharacterSet = 1252; |
180 |
|
msg.WriteString(msgString); |
181 |
|
|
182 |
//now that the message has been put on queue mark it as such |
out_queue.Put(msg, pmo); |
183 |
|
|
184 |
string updateSql = "CALL " + sql2mqUpdateQuery + "(" + id + ")"; |
//now that the message has been put on queue mark it as such |
|
MySqlCommand updateCmd = new MySqlCommand(updateSql, sqlConnection); |
|
|
int numrows = updateCmd.ExecuteNonQuery(); |
|
185 |
|
|
186 |
translog.WriteLine(getNowString() + " " + msgString + "\n"); |
string updateSql = "CALL " + sql2mqUpdateQuery + "(" + id + ")"; |
187 |
|
MySqlCommand updateCmd = new MySqlCommand(updateSql, sqlWriteConnection); |
188 |
|
int numrows = updateCmd.ExecuteNonQuery(); |
189 |
|
|
190 |
if (numrows != 1) |
translog.WriteLine(getNowString() + " " + msgString + "\n"); |
191 |
{ |
|
192 |
break; |
if (numrows != 1) |
193 |
|
{ |
194 |
|
break; |
195 |
|
} |
196 |
|
statusData.counter++; |
197 |
} |
} |
|
counter++; |
|
|
} |
|
|
|
|
198 |
|
|
199 |
//stage 4: everything went smooth so clean up afterwards |
} |
|
dataReader.Close(); |
|
|
out_queue.Close(); |
|
|
mqMgr.Close(); |
|
|
sqlConnection.Close(); |
|
200 |
} |
} |
201 |
catch (Exception e) |
catch (Exception e) |
202 |
{ |
{ |
203 |
lastrunOk = false; |
statusData.lastrunOk = false; |
204 |
lastErrorMessage = name + ".transportSql2Mq error: " + e.Message; |
statusData.lastErrorMessage = name + ".transportMq2Sql error: " + e.GetType().FullName + " " + e.Message; |
205 |
Console.WriteLine(lastErrorMessage); |
Console.WriteLine(statusData.lastErrorMessage); |
206 |
EventLog.WriteEntry("DaoMqPump2", lastErrorMessage, EventLogEntryType.Warning); |
Console.WriteLine(e.StackTrace); |
207 |
|
EventLog.WriteEntry("DaoMqPump2", statusData.lastErrorMessage, EventLogEntryType.Warning); |
208 |
} |
} |
209 |
} |
finally |
|
|
|
|
private void transportMq2Sql() |
|
|
{ |
|
|
string filename = getTransactionlogFilename(); |
|
|
using (StreamWriter translog = new StreamWriter(filename, true)) |
|
|
try |
|
210 |
{ |
{ |
|
//stage 1 connect to mq |
|
|
Hashtable connProps = getConnectionProperties(); |
|
|
MQQueueManager mqMgr = new MQQueueManager(controller.mqQueueManager, connProps); |
|
|
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING; |
|
|
|
|
|
MQQueue in_queue = mqMgr.AccessQueue(queueName, openOptions); |
|
211 |
|
|
212 |
|
if (out_queue != null && out_queue.IsOpen) |
213 |
|
{ |
214 |
|
try |
215 |
|
{ |
216 |
|
out_queue.Close(); |
217 |
|
} |
218 |
|
catch (Exception e) |
219 |
|
{ |
220 |
|
Console.WriteLine("Error cleaning up qmgr " + e.Message); |
221 |
|
} |
222 |
|
} |
223 |
|
|
224 |
//stage 2 connect to mysql |
if (mqMgr != null && mqMgr.IsOpen) |
225 |
string mysqlString = buildMysqlConnString(); |
{ |
226 |
MySqlConnection sqlConnection = new MySqlConnection(mysqlString); |
try |
227 |
sqlConnection.Open(); |
{ |
228 |
|
mqMgr.Close(); |
229 |
|
} |
230 |
|
catch (Exception e) |
231 |
|
{ |
232 |
|
Console.WriteLine("Error cleaning up qmgr " + e.Message); |
233 |
|
} |
234 |
|
} |
235 |
|
|
236 |
|
} |
237 |
|
} |
238 |
|
|
239 |
//stage 3 move messages |
private void transportMq2Sql() |
240 |
bool isContinue = true; |
{ |
241 |
while (isContinue) |
MQQueueManager mqMgr = null; |
242 |
|
MQQueue in_queue = null; |
243 |
|
string filename = getLogFilename(LogfileType.LogTransactions); |
244 |
|
using (StreamWriter translog = new StreamWriter(filename, true)) |
245 |
|
try |
246 |
{ |
{ |
247 |
|
//MQ options |
248 |
MQMessage mqMsg = new MQMessage(); |
Hashtable connProps = getConnectionProperties(); |
249 |
MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions(); |
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING; |
250 |
|
|
251 |
|
//MySQL options |
252 |
|
string mysqlString = buildMysqlConnString(); |
253 |
|
|
254 |
|
//MQ objects i v6 implementerer ikke IDisposable og kan derfor ikke bruges med "using" statement |
255 |
|
mqMgr = new MQQueueManager(controller.mqQueueManager, connProps);//stage 1 connect to mq |
256 |
|
in_queue = mqMgr.AccessQueue(queueName, openOptions); |
257 |
|
using (MySqlConnection sqlConnection = new MySqlConnection(mysqlString)) //stage 2 connect to mysql |
258 |
|
{ |
259 |
|
|
260 |
mqGetMsgOpts.Options = MQC.MQGMO_SYNCPOINT; //kræver en commit førend at den er fjernet fra køen |
sqlConnection.Open(); |
261 |
|
|
|
try |
|
|
{ |
|
|
in_queue.Get(mqMsg, mqGetMsgOpts); |
|
|
if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0) |
|
|
{ |
|
|
string msgString = mqMsg.ReadString(mqMsg.MessageLength); |
|
|
System.Console.WriteLine(msgString); |
|
262 |
|
|
263 |
string sql = "CALL " + mq2sqlInsertQuery + "( '" + MySqlHelper.EscapeString(msgString) + "' )"; //opbygger en CALL somestoredprocedure('msgString'); sql streng |
//stage 3 move messages |
264 |
|
bool isContinue = true; |
265 |
|
while (isContinue) |
266 |
|
{ |
267 |
|
|
268 |
|
MQMessage mqMsg = new MQMessage(); |
269 |
|
MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions(); |
270 |
|
|
271 |
MySqlCommand sqlcmd = new MySqlCommand(sql, sqlConnection); |
mqGetMsgOpts.Options = MQC.MQGMO_SYNCPOINT; //kræver en commit førend at den er fjernet fra køen |
|
int numrows = sqlcmd.ExecuteNonQuery(); |
|
272 |
|
|
273 |
if (numrows == 1) |
try |
274 |
{ |
{ |
275 |
translog.WriteLine(getNowString() + " " + msgString + "\n"); |
in_queue.Get(mqMsg, mqGetMsgOpts); |
276 |
mqMgr.Commit(); |
if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0) |
277 |
counter++; |
{ |
278 |
|
string msgString = mqMsg.ReadString(mqMsg.MessageLength); |
279 |
|
System.Console.WriteLine(msgString); |
280 |
|
|
281 |
|
string sql = "CALL " + mq2sqlInsertQuery + "( '" + MySqlHelper.EscapeString(msgString) + "' )"; //opbygger en CALL somestoredprocedure('msgString'); sql streng |
282 |
|
|
283 |
|
MySqlCommand sqlcmd = new MySqlCommand(sql, sqlConnection); |
284 |
|
int numrows = sqlcmd.ExecuteNonQuery(); |
285 |
|
|
286 |
|
if (numrows == 1) |
287 |
|
{ |
288 |
|
translog.WriteLine(getNowString() + " " + msgString + "\n"); |
289 |
|
mqMgr.Commit(); |
290 |
|
statusData.counter++; |
291 |
|
} |
292 |
|
else |
293 |
|
{ |
294 |
|
mqMgr.Backout(); |
295 |
|
isContinue = false; |
296 |
|
} |
297 |
|
|
298 |
|
} |
299 |
|
else |
300 |
|
{ |
301 |
|
System.Console.WriteLine("Non-text message"); |
302 |
|
} |
303 |
} |
} |
304 |
else |
catch (MQException mqe) |
305 |
{ |
{ |
|
mqMgr.Backout(); |
|
306 |
isContinue = false; |
isContinue = false; |
307 |
} |
|
308 |
|
// report reason, if any |
309 |
|
if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE) |
310 |
|
{ |
311 |
|
// special report for normal end |
312 |
|
System.Console.WriteLine("no more messages"); |
313 |
|
} |
314 |
|
else |
315 |
|
{ |
316 |
|
// general report for other reasons |
317 |
|
System.Console.WriteLine("MQQueue::Get ended with " + mqe.Message); ; |
318 |
|
statusData.lastrunOk = false; |
319 |
|
} |
320 |
|
|
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
} |
} |
325 |
else |
|
326 |
|
} |
327 |
|
|
328 |
|
} |
329 |
|
catch (Exception e) |
330 |
|
{ |
331 |
|
//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 |
332 |
|
try |
333 |
|
{ |
334 |
|
if (mqMgr != null) |
335 |
{ |
{ |
336 |
System.Console.WriteLine("Non-text message"); |
mqMgr.Backout(); |
337 |
} |
} |
338 |
} |
} |
339 |
catch (MQException mqe) |
catch (Exception e2) |
340 |
{ |
{ |
341 |
isContinue = false; |
this.addLogEntry("Error backing out from MQ Transaction: " + e2.Message); |
342 |
|
} |
343 |
|
|
344 |
|
statusData.lastrunOk = false; |
345 |
|
|
346 |
// report reason, if any |
statusData.lastErrorMessage = name + ".transportMq2Sql error: " + e.GetType().FullName + " " + e.Message; |
347 |
if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE) |
Console.WriteLine(statusData.lastErrorMessage); |
348 |
|
Console.WriteLine(e.StackTrace); |
349 |
|
EventLog.WriteEntry("DaoMqPump2", statusData.lastErrorMessage, EventLogEntryType.Warning); |
350 |
|
} |
351 |
|
finally |
352 |
|
{ |
353 |
|
|
354 |
|
if (in_queue != null && in_queue.IsOpen) |
355 |
|
{ |
356 |
|
try |
357 |
{ |
{ |
358 |
// special report for normal end |
in_queue.Close(); |
|
System.Console.WriteLine("no more messages"); |
|
359 |
} |
} |
360 |
else |
catch (Exception e) |
361 |
{ |
{ |
362 |
// general report for other reasons |
Console.WriteLine("Error cleaning up qmgr " + e.Message); |
363 |
System.Console.WriteLine("MQQueue::Get ended with " + mqe.Message);; |
} |
364 |
lastrunOk = false; |
} |
365 |
|
|
366 |
|
if (mqMgr != null && mqMgr.IsOpen) |
367 |
|
{ |
368 |
|
try |
369 |
|
{ |
370 |
|
mqMgr.Close(); |
371 |
|
} catch (Exception e) { |
372 |
|
Console.WriteLine("Error cleaning up qmgr " + e.Message); |
373 |
} |
} |
|
|
|
374 |
} |
} |
|
|
|
375 |
|
|
376 |
} |
} |
|
|
|
|
//stage 4: everything went smooth so clean up afterwards |
|
|
in_queue.Close(); |
|
|
mqMgr.Close(); |
|
|
sqlConnection.Close(); |
|
|
|
|
|
|
|
|
} |
|
|
catch (Exception e) |
|
|
{ |
|
|
lastrunOk = false; |
|
|
|
|
|
lastErrorMessage = name + ".transportMq2Sql error: " + e.Message; |
|
|
Console.WriteLine(lastErrorMessage); |
|
|
EventLog.WriteEntry("DaoMqPump2", lastErrorMessage, EventLogEntryType.Warning); |
|
|
} |
|
377 |
} |
} |
378 |
|
|
379 |
private string buildMysqlConnString() |
private string buildMysqlConnString() |
384 |
//connectionString += "DATABASE=" + controller.mysqlHost + ";"; |
//connectionString += "DATABASE=" + controller.mysqlHost + ";"; |
385 |
connectionString += "UID=" + controller.mysqlUser + ";"; |
connectionString += "UID=" + controller.mysqlUser + ";"; |
386 |
connectionString += "PASSWORD=" + controller.mysqlPassword + ";"; |
connectionString += "PASSWORD=" + controller.mysqlPassword + ";"; |
387 |
|
connectionString += "Max Pool Size=20;"; |
388 |
|
//connectionString += "ConnectionReset=true;";//ConnectionReset kræver muligvis at Default Database er angivet - det virker ihvertfald ikke uden |
389 |
|
|
390 |
return connectionString; |
return connectionString; |
391 |
} |
} |
399 |
return connProperties; |
return connProperties; |
400 |
} |
} |
401 |
|
|
402 |
private string getTransactionlogFilename() |
|
403 |
|
|
404 |
|
private string getLogFilename(LogfileType type) |
405 |
{ |
{ |
406 |
|
|
407 |
DateTime now = DateTime.Now; |
DateTime now = DateTime.Now; |
408 |
string filename = controller.logDirectory + "\\"; |
string filename = controller.logDirectory + "\\"; |
409 |
filename += "transactionlog_" + name + "_" + now.Year.ToString("D4") + now.Month.ToString("D2") + ".log"; |
|
410 |
|
switch (type) |
411 |
|
{ |
412 |
|
case LogfileType.LogEvents: |
413 |
|
filename += "eventlog_"; |
414 |
|
break; |
415 |
|
|
416 |
|
case LogfileType.LogTransactions: |
417 |
|
filename += "transactionlog_"; |
418 |
|
break; |
419 |
|
} |
420 |
|
|
421 |
|
|
422 |
|
filename += name + "_" + now.Year.ToString("D4") + now.Month.ToString("D2") + ".log"; |
423 |
|
|
424 |
return filename; |
return filename; |
425 |
} |
} |
441 |
if (logEntries.Count > 20) |
if (logEntries.Count > 20) |
442 |
{ |
{ |
443 |
logEntries.RemoveLast(); |
logEntries.RemoveLast(); |
444 |
} |
} |
445 |
|
} |
446 |
|
|
447 |
|
string filename = getLogFilename(LogfileType.LogEvents); |
448 |
|
using (StreamWriter eventlog = new StreamWriter(filename, true)) |
449 |
|
{ |
450 |
|
eventlog.WriteLine(msg); |
451 |
} |
} |
452 |
} |
} |
453 |
|
|