Source Viewer: Mq.Load.cs
Font Size:
1
2
3
4
5
6
7
Mq.InquireQueueNames.cs
Mq.Load.cs
..
// Mq.Load.cs // // This MQ app clears a queue, then enqueues 500 messages. // // Tested with MQ v5.3 server + CSD10, or MQv6.0. with MQ Server // installed locally, all on a Windows XP SP2 machine. // Should also work with MQ Client and a remote MQ server on // Windows or non-Windows, but I didn't test it that way. // // // compile with: // csc /target:exe /out:Mq.Load.exe /R:amqmdnet.dll Mq.Load.cs // // Mon, 13 Feb 2006 21:21 // using System; using IBM.WMQ; using IBM.WMQ.PCF; namespace Dinoch.Example.Mq { class Load { private const int NUMBER_MSGS_TO_ENQUEUE= 500; private const int MAX_TRIALS= 4; System.Random rnd= new System.Random(); // these things are defined in the MQ Classes for .NET PCFMessageAgent agent; MQQueueManager mqm= null; MQQueue queue= null; String QueueManagerName= null; String QueueName= "TestQueue"; string assyName; public Load() { assyName= System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()); } [MTAThread] static void Main(string[] args) { Load me= new Load(); me.Go(); } private void Go() { Console.WriteLine("Hello from ...{0}", assyName); int TrialsCompleted= 0; bool Success= false; do { try { mqm= (QueueManagerName!=null)? new MQQueueManager(QueueManagerName): new MQQueueManager(); QueueManagerName= mqm.Name; try { CreateQueue(QueueName); } catch (IBM.WMQ.MQException mqex) { System.Console.Error.WriteLine("{0}: While creating queue, MQ Exception: {1}\n{2}", assyName, mqex.Message, mqex.StackTrace); System.Console.Error.WriteLine("Maybe the queue already exists? Continuing..."); } Success= LoadMessages(QueueName); } catch (IBM.WMQ.MQException mqex) { System.Console.Error.WriteLine("{0}: MQ Exception: {1}\n{2}", assyName, mqex.Message, mqex.StackTrace); System.Environment.Exit (1); } finally { if (queue!=null) try {queue.Close();} catch{} if (mqm!=null) try {mqm.Disconnect();} catch{} } TrialsCompleted++; } while ((TrialsCompleted < MAX_TRIALS) && !Success) ; Console.WriteLine("\nAll done."); } private bool LoadMessages(string qname) { Console.WriteLine("Using MQ queue '{0}' ", qname); ClearQueue(qname); int openOptions= MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT ; queue= mqm.AccessQueue(qname,openOptions); for (int i=0; i < NUMBER_MSGS_TO_ENQUEUE; i++) { PutOneMessage(); if (i%10==0) { Console.Write("."); } } Console.WriteLine(); queue.Close(); return true; } private void CreateQueue(string queuename) { agent = new PCFMessageAgent(QueueManagerName); PCFMessage request= new PCFMessage(CMQCFC.MQCMD_CREATE_Q); request.AddParameter (MQC.MQCA_Q_NAME, queuename); request.AddParameter (MQC.MQIA_Q_TYPE, MQC.MQQT_LOCAL); request.AddParameter(MQC.MQCA_Q_DESC, "created by MQ.Create.exe on " + System.DateTime.Now.ToString("u")); PCFMessage[] responses = agent.Send(request); } private void ClearQueue(string qname) { agent = new PCFMessageAgent(QueueManagerName); PCFMessage request= new PCFMessage(CMQCFC.MQCMD_CLEAR_Q); request.AddParameter (MQC.MQCA_Q_NAME, qname); PCFMessage[] responses = agent.Send(request); } private void PutOneMessage() { String payload= "whatever-" + rnd.Next(1000); try { //Console.Write("{0} ", payload); MQMessage msg= new MQMessage(); msg.Persistence= MQC.MQPER_PERSISTENT; // retain MQ msgs across QM stop/restart msg.WriteUTF(payload); MQPutMessageOptions pmo= new MQPutMessageOptions(); //pmo.Options|= MQC.MQPMO_SYNCPOINT; // this will only work if we commit later! queue.Put(msg,pmo); } catch (MQException ex1) { Console.Error.WriteLine("{0}: MQ error: {1} {2}", assyName, ex1.Message, ex1.Reason); throw new Exception( "MQ Exception", ex1 ); } catch (System.Exception ex2) { Console.Error.WriteLine("{0}: System error: {1}",assyName, ex2); throw new Exception( "System error", ex2 ); } } } }
The srcview page has been enjoyed 292084 times since 18 September 2003