139 |
|
|
140 |
private void transportSql2Mq() |
private void transportSql2Mq() |
141 |
{ |
{ |
142 |
|
MQQueueManager mqMgr = null; |
143 |
|
MQQueue out_queue = null; |
144 |
|
|
145 |
string filename = getLogFilename(LogfileType.LogTransactions); |
string filename = getLogFilename(LogfileType.LogTransactions); |
146 |
using (StreamWriter translog = new StreamWriter(filename, true) ) |
using (StreamWriter translog = new StreamWriter(filename, true) ) |
147 |
try |
try |
152 |
|
|
153 |
//MySQL Options |
//MySQL Options |
154 |
string mysqlString = buildMysqlConnString(); |
string mysqlString = buildMysqlConnString(); |
|
|
|
155 |
|
|
156 |
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 |
157 |
using (MQQueue out_queue = mqMgr.AccessQueue(queueName, openOptions)) |
mqMgr = new MQQueueManager(controller.mqQueueManager, connProps);//stage 1 connect to mq |
158 |
|
out_queue = mqMgr.AccessQueue(queueName, openOptions); |
159 |
using (MySqlConnection sqlReadConnection = new MySqlConnection(mysqlString)) //stage 2 connect to mysql |
using (MySqlConnection sqlReadConnection = new MySqlConnection(mysqlString)) //stage 2 connect to mysql |
160 |
using (MySqlConnection sqlWriteConnection = new MySqlConnection(mysqlString)) |
using (MySqlConnection sqlWriteConnection = new MySqlConnection(mysqlString)) |
161 |
{ |
{ |
187 |
MySqlCommand updateCmd = new MySqlCommand(updateSql, sqlWriteConnection); |
MySqlCommand updateCmd = new MySqlCommand(updateSql, sqlWriteConnection); |
188 |
int numrows = updateCmd.ExecuteNonQuery(); |
int numrows = updateCmd.ExecuteNonQuery(); |
189 |
|
|
190 |
translog.WriteLine(getNowString() + " " + msgString + "\n"); |
translog.WriteLine(getNowString() + " " + msgString); |
191 |
|
|
192 |
if (numrows != 1) |
if (numrows != 1) |
193 |
{ |
{ |
206 |
Console.WriteLine(e.StackTrace); |
Console.WriteLine(e.StackTrace); |
207 |
EventLog.WriteEntry("DaoMqPump2", statusData.lastErrorMessage, EventLogEntryType.Warning); |
EventLog.WriteEntry("DaoMqPump2", statusData.lastErrorMessage, EventLogEntryType.Warning); |
208 |
} |
} |
209 |
|
finally |
210 |
|
{ |
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 |
|
if (mqMgr != null && mqMgr.IsOpen) |
225 |
|
{ |
226 |
|
try |
227 |
|
{ |
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 |
private void transportMq2Sql() |
private void transportMq2Sql() |
240 |
{ |
{ |
241 |
|
MQQueueManager mqMgr = null; |
242 |
|
MQQueue in_queue = null; |
243 |
string filename = getLogFilename(LogfileType.LogTransactions); |
string filename = getLogFilename(LogfileType.LogTransactions); |
244 |
using (StreamWriter translog = new StreamWriter(filename, true)) |
using (StreamWriter translog = new StreamWriter(filename, true)) |
245 |
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 |
|
246 |
{ |
{ |
247 |
|
//MQ options |
248 |
|
Hashtable connProps = getConnectionProperties(); |
249 |
|
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 |
sqlConnection.Open(); |
sqlConnection.Open(); |
261 |
|
|
262 |
|
|
263 |
//stage 3 move messages |
//stage 3 move messages |
264 |
bool isContinue = true; |
bool isContinue = true; |
265 |
while (isContinue) |
while (isContinue) |
266 |
{ |
{ |
267 |
|
|
268 |
MQMessage mqMsg = new MQMessage(); |
MQMessage mqMsg = new MQMessage(); |
269 |
MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions(); |
MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions(); |
270 |
|
|
271 |
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 |
272 |
|
|
273 |
try |
try |
|
{ |
|
|
in_queue.Get(mqMsg, mqGetMsgOpts); |
|
|
if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0) |
|
274 |
{ |
{ |
275 |
string msgString = mqMsg.ReadString(mqMsg.MessageLength); |
in_queue.Get(mqMsg, mqGetMsgOpts); |
276 |
System.Console.WriteLine(msgString); |
if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0) |
277 |
|
{ |
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 |
string sql = "CALL " + mq2sqlInsertQuery + "( '" + MySqlHelper.EscapeString(msgString) + "' )"; //opbygger en CALL somestoredprocedure('msgString'); sql streng |
282 |
|
|
283 |
MySqlCommand sqlcmd = new MySqlCommand(sql, sqlConnection); |
MySqlCommand sqlcmd = new MySqlCommand(sql, sqlConnection); |
284 |
int numrows = sqlcmd.ExecuteNonQuery(); |
int numrows = sqlcmd.ExecuteNonQuery(); |
285 |
|
|
286 |
|
if (numrows == 1) |
287 |
|
{ |
288 |
|
translog.WriteLine(getNowString() + " " + msgString); |
289 |
|
mqMgr.Commit(); |
290 |
|
statusData.counter++; |
291 |
|
} |
292 |
|
else |
293 |
|
{ |
294 |
|
mqMgr.Backout(); |
295 |
|
isContinue = false; |
296 |
|
} |
297 |
|
|
|
if (numrows == 1) |
|
|
{ |
|
|
translog.WriteLine(getNowString() + " " + msgString + "\n"); |
|
|
mqMgr.Commit(); |
|
|
statusData.counter++; |
|
298 |
} |
} |
299 |
else |
else |
300 |
{ |
{ |
301 |
mqMgr.Backout(); |
System.Console.WriteLine("Non-text message"); |
|
isContinue = false; |
|
302 |
} |
} |
|
|
|
303 |
} |
} |
304 |
else |
catch (MQException mqe) |
305 |
{ |
{ |
306 |
System.Console.WriteLine("Non-text message"); |
isContinue = false; |
307 |
} |
|
308 |
} |
// report reason, if any |
309 |
catch (MQException mqe) |
if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE) |
310 |
{ |
{ |
311 |
isContinue = false; |
// 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 |
|
|
|
// 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; |
|
321 |
} |
} |
322 |
|
|
323 |
|
|
324 |
} |
} |
325 |
|
|
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 |
|
mqMgr.Backout(); |
337 |
|
} |
338 |
} |
} |
339 |
|
catch (Exception e2) |
340 |
|
{ |
341 |
|
this.addLogEntry("Error backing out from MQ Transaction: " + e2.Message); |
342 |
|
} |
343 |
|
|
344 |
|
statusData.lastrunOk = false; |
345 |
|
|
346 |
|
statusData.lastErrorMessage = name + ".transportMq2Sql error: " + e.GetType().FullName + " " + e.Message; |
347 |
|
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 |
catch (Exception e) |
{ |
356 |
{ |
try |
357 |
statusData.lastrunOk = false; |
{ |
358 |
|
in_queue.Close(); |
359 |
statusData.lastErrorMessage = name + ".transportMq2Sql error: " + e.GetType().FullName + " " + e.Message; |
} |
360 |
Console.WriteLine(statusData.lastErrorMessage); |
catch (Exception e) |
361 |
Console.WriteLine(e.StackTrace); |
{ |
362 |
EventLog.WriteEntry("DaoMqPump2", statusData.lastErrorMessage, EventLogEntryType.Warning); |
Console.WriteLine("Error cleaning up qmgr " + e.Message); |
363 |
} |
} |
364 |
|
} |
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 |
|
} |
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 += "maximumpoolsize=10;"; |
connectionString += "Max Pool Size=20;"; |
388 |
//connectionString += "ConnectionReset=true;"; |
//connectionString += "ConnectionReset=true;";//ConnectionReset kræver muligvis at Default Database er angivet - det virker ihvertfald ikke uden |
389 |
|
|
390 |
return connectionString; |
return connectionString; |
391 |
} |
} |