Your Daily Mark.Net


Treat others as you want to be treated

March 2007 - Posts

Visual studio Team System The future
This post is about the future release of team system.
Some nice features in the next version of team edition for developers: Code metrics, "Hotpathing" and Profiler Support for WCF Applications. The last is interesting for me because currently we are developing a wcf application.

The team server foundation has also some interesting new features:
  • Folder Diff
  • Support multi-threaded builds with the new MSBuild
  • Get Latest on Checkout
  • and a lot more
so check out the future releases of TFS

Using IBM Webphere MQ with .Net
In my current project we use IBM Websphere MQ for queueing. Legacy systems communicate through the queue with the system.

I never used IBM Websphere MQ before, so it is a new experience for me.
The documentation of the MQ api is ever brief and it was a lot of debugging.

But I want to share the experience.

To create a Queue manager:
//add reference to amqmdnet.dll
using IBM.WMQ;
...

string managerName = "IBMQueueManager";
string hostName = "TestMachine"; // The name of the machine where the queuemanager is installed.
string channelName = "TestChannel";
string queueName = "testQueue";
//init queue manager
MQQueueManager queueManager = new MQQueueManager( managerName, channelName, hostName);

//init read queue
MQQueue queue = queueManager.AccessQueue( queueName, MQC.MQOO_INPUT_AS_Q_DEF // open queue for input
                                                                      + MQC.MQOO_FAIL_IF_QUIESCING);

//read message without transaction:
MQGetMessageOptions messageOptions = new MQGetMessageOptions();
messageOptions.Options |= MQC.MQGMO_WAIT;
messageOptions.Options.WaitInterval = 1000; // 1 second to wait.
MQMessage message = new MQMessage();
try
{
    queue.Get( message, messageOptions);
    //process message.
}
catch(MQException mqEx) //an exception is thrown when no message is available.
{
    if (mqEx.ReasonCode != MQC.MQRC_NO_MSG_AVAILABLE)
    {
       throw;//something wrong rethrow the exception
    }
}
finally
{
    queue.Close();
}

...
//read message with with transaction
MQGetMessageOptions messageOptions = new MQGetMessageOptions();
messageOptions.Options |= MQC.MQGMO_WAIT + MQC.MQGMO_SYNCPOINT;
messageOptions.Options.WaitInterval = 1000; // 1 second to wait.
MQMessage message = new MQMessage();
try
{
    queue.Get( message, messageOptions);
    //process message.
    queueManager.Commit();
}
catch(MQException mqEx) //an exception is thrown when no message is available.
{
    if (mqEx.ReasonCode != MQC.MQRC_NO_MSG_AVAILABLE)
    {
       queueManager.Backout();
       throw;//something wrong rethrow the exception
    }
}
finally
{
    queue.Close();
}

            //browse messages:
            MQQueue browseQueue = queueManager.AccessQueue( queueName, MQC.MQOO_BROWSE // open queue for browse
                                                                      + MQC.MQOO_FAIL_IF_QUIESCING);
            ///
            /// Get messages from the message queue
            /// Loop until there is a failure
            ///
            ///
            bool firstBrowsed = false;
            bool isContinue = true;
            while (isContinue)
            {
                MQMessage mqMsg; // MQMessage instance
                MQGetMessageOptions mqGetMsgOpts; // MQGetMessageOptions instance

                mqMsg = new MQMessage();
                mqGetMsgOpts = new MQGetMessageOptions();
                mqGetMsgOpts.WaitInterval = 15000; // 15 second limit for waiting
                if (!firstBrowsed)
                {
                    mqGetMsgOpts.Options |= MQC.MQGMO_WAIT + MQC.MQGMO_BROWSE_FIRST;

                }
                else
                {
                    mqGetMsgOpts.Options |= MQC.MQGMO_WAIT + MQC.MQGMO_BROWSE_NEXT;
                }


                try
                {

                    mqQueue.Get(mqMsg, mqGetMsgOpts);

                    firstBrowsed = true;
                    if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0)
                    {
                        if (mqMsg.MessageLength > 0)
                        {
                            System.Console.WriteLine(mqMsg.ReadString(mqMsg.MessageLength));
                         }
                      }
                }
                catch (MQException mqe)
                {
                    if (mqEx.ReasonCode != MQC.MQRC_NO_MSG_AVAILABLE)
                    {
                       queueManager.Backout();
                       throw;//something wrong rethrow the exception
                    }
                   else
                   {
                      isContinue = false;
                    }
                }
          }

//Have fun with it.