Peter Howe
Peter Howe

Reputation: 429

Websphere MQ - Keeping message context and identity when moving from one queue to another in .NET

I have been tasked with writing a custom application in C#.NET to move messages from one queue to another, while keeping all of the context and identity information the same as the original message.

I have tried to decipher the online docs, and have tried every combination of MQC.MQOO_PASS_ALL_CONTEXT and MQOO_SAVE_ALL_CONTEXT in opening queues, and MQC.MQPMO_PASS_ALL_CONTEXT in put calls, to no avail. I have tried setting the MQPutMessageOptions.ContextReference to both the source and the destination queues, but I continue to get exceptions, either MQRC_CONTEXT_HANDLE_ERROR or MQRC_OPTIONS_ERROR, and I cannot find enough information in the online docs to determine the problem.

I am using runtime version v2.0.50727, Version 7.5.0.1 of the amqmdnet.dll. If anyone knows the correct open and/or put message option settings I need to use, I would appreciate the leg up. Thanks.

***** SECOND UPDATE *****

Here is a very simple gui program to test the classes, and I get the same results:

    public partial class frmMain : Form
{
    // Simple test - all data hard-coded

    string sourceQStr = "PETE.TQ1";
    string targetQStr = "PETE.TQ2";
    string targetMsgIdStr = "414D5120514D4942584330352020202028ED0155202EB302";
    string connName = "localhost";
    string connPort = "1414";


    MQQueueManager sourceManager;
    MQQueueManager targetManager;
    MQQueue sourceQueue;
    MQQueue targetQueue;

    MQMessage msg = new MQMessage();
    MQGetMessageOptions gmo = new MQGetMessageOptions();
    MQPutMessageOptions pmo = new MQPutMessageOptions();


    public frmMain()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void txtPassword_Validating(object sender, CancelEventArgs e)
    {

        if (txtPassword.Text.Trim() != string.Empty)
        {
            if (LoginUser())
                btnTest.Focus();
            else
                txtUserId.Focus();
        }
    }

    public void LogoutUser()
    {
        txtUserId.Text = "";
        txtPassword.Text = "";
        btnTest.Enabled = false;

    }

    public bool LoginUser()
    {
        bool OK = ValidateUserAndPassword();
        if (OK)
        {
            btnTest.Enabled = true;
        }
        else LogoutUser();
        return OK;
    }

    private bool ValidateUserAndPassword()
    {
        if ((txtUserId.Text.Trim() == string.Empty) || (txtPassword.Text.Trim() == string.Empty))
            return false;
        try
        {
            bool _passwordValid = false;
            ContextOptions options = ContextOptions.SimpleBind | ContextOptions.SecureSocketLayer;
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "UP"))
            {
                _passwordValid = pc.ValidateCredentials
                    (txtUserId.Text.Trim(), txtPassword.Text.Trim(), options);
            }
            if (!_passwordValid)
                MessageBox.Show("Invalid username / password", "Invalid", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            return _passwordValid;
        }
        catch (PrincipalServerDownException pex)
        {
            string msg = pex.Message.Insert(pex.Message.IndexOf("server"), "Authentication ");
            MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
    }

    private void btnTest_Click(object sender, EventArgs e)
    {
        try
        {
            SetupObjects();
            sourceQueue.Get(msg, gmo);
            targetQueue.Put(msg, pmo);
            sourceManager.Commit();
            targetManager.Commit();
            MessageBox.Show("Test appears successful - verify with MQ Explorer","OK", 
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        catch (Exception ex) // MQRC_CONTEXT_HANDLE_ERROR is always thrown
        {
            MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            sourceManager.Backout();
            targetManager.Backout();
        }
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    /************************************** Utiility methods *****************************************/

    private void SetupObjects()
    {
        // set up objects
        string connectName = connName + "(" + connPort + ")";
        int ConnOptions = MQC.MQCNO_HANDLE_SHARE_BLOCK;

        sourceManager = new MQQueueManager("", ConnOptions, "CHANNEL1", connectName);
        targetManager = new MQQueueManager("", ConnOptions, "CHANNEL1", connectName);

        MQEnvironment.UserId = txtUserId.Text.Trim();
        MQEnvironment.Password = txtPassword.Text.Trim();

        int options = 
            MQC.MQOO_INPUT_SHARED + MQC.MQOO_FAIL_IF_QUIESCING + 
            MQC.MQOO_SAVE_ALL_CONTEXT + MQC.MQOO_INQUIRE;
        sourceQueue = sourceManager.AccessQueue
            (sourceQStr, options, sourceManager.Name, null, txtUserId.Text.Trim());
        options = 
            MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING + 
            MQC.MQOO_PASS_ALL_CONTEXT + MQC.MQOO_INQUIRE;
        targetQueue = targetManager.AccessQueue
            (targetQStr, options, targetManager.Name, null, txtUserId.Text.Trim());

        int i =  0;
        try
        {
            i = sourceQueue.CurrentDepth;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        gmo.Options = MQC.MQGMO_COMPLETE_MSG + MQC.MQGMO_FAIL_IF_QUIESCING +
            MQC.MQGMO_NO_WAIT + MQC.MQGMO_SYNCPOINT;

        pmo.Options = MQC.MQPMO_PASS_ALL_CONTEXT +
            MQC.MQPMO_SYNCPOINT + MQC.MQPMO_SYNC_RESPONSE;
        pmo.ContextReference = sourceQueue;

        msg.MessageId = StringToByteArray(targetMsgIdStr);
    }

    public byte[] StringToByteArray(String hex)
    {
        hex = hex.Trim();
        int NumberChars = hex.Length;
        if ((NumberChars % 2) != 0)
        {
            hex = "0" + hex;
            NumberChars++;
        }
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = StringToByte(hex.Substring(i, 2));
        return bytes;
    }

    public byte StringToByte(string hexDigits)
    {
        if (hexDigits.Length != 2)
            return 0;

        int high = hexValue(hexDigits[0]);
        int low = hexValue(hexDigits[1]);

        return (byte)(((high << 4) & 240) | (low & 15));
    }

    public int hexValue(char c)
    {
        int retval = 0;
        if (c > '9')
            retval = ((int)c) - 55;
        else
            retval = ((int)c) - 48;
        return retval;
    }
}

The GUI simply prompts for a username and password (which are validated with an LDAP call) and then enables a "test" button which runs the btnTest_Click method. I still get the same exception (MQRC_CONTEXT_HANDLE_ERROR) even thought I have used all the open and get message options specified.

Upvotes: 1

Views: 1191

Answers (2)

Morag Hughson
Morag Hughson

Reputation: 7515

The correct options to use to move messages whilst retaining all the context information within them are as follows.

When opening the queue to get messages from, use MQOO_SAVE_ALL_CONTEXT. The context will be saved at get time in the object handle that represents this opened queue.

When opening the queue to put messages to, use MQOO_PASS_ALL_CONTEXT. Then when putting the message, use MQPMO_PASS_ALL_CONTEXT and provide the object handle that contains the context.

Receiving return code MQRC_CONTEXT_HANDLE_ERROR means that you didn't use MQOO_SAVE_ALL_CONTEXT on the first open but then tried to use the object handle on the put. It could also mean that it's just not a correct reference to the first queue. In the MQ API it's an object handle (MQHOBJ) whereas in .Net it's the MQQueue (see MQPutMessageOptions .NET class).

Receiving return code MQRC_OPTIONS_ERROR means that you mixed and matched the options too much.

Upvotes: 2

Shashi
Shashi

Reputation: 15263

Adding to Morag's answer, here is snippet for moving messages with context. I am also using a SYNC_POINT to ensure the message is removed from source queue after the message is successfully put to the destination queue.

    /// <summary>
    /// Moves messages from one queue to another with context
    /// </summary>
    public void moveMessagesWithContext()
    {
        MQQueueManager qmSource = null;
        MQQueueManager qmDestination = null;
        MQQueue qSource = null;
        MQQueue qDestination = null;
        Hashtable htSource = null;
        Hashtable htDestination = null;

        try
        {
            htSource = new Hashtable();
            htDestination = new Hashtable();

            htSource.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
            htSource.Add(MQC.HOST_NAME_PROPERTY, "localhost");
            htSource.Add(MQC.PORT_PROPERTY, 2020);
            htSource.Add(MQC.CHANNEL_PROPERTY, "A_QM_SVRCONN");
            qmSource = new MQQueueManager("A_QM", htSource);
            qSource = qmSource.AccessQueue("Q_SOURCE", MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_SAVE_ALL_CONTEXT);

            htDestination.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
            htDestination.Add(MQC.HOST_NAME_PROPERTY, "localhost");
            htDestination.Add(MQC.PORT_PROPERTY, 3030);
            htDestination.Add(MQC.CHANNEL_PROPERTY, "B_QM_SVRCONN");
            qmDestination = new MQQueueManager("B_QM", htDestination);
            qDestination = qmDestination.AccessQueue("Q_DESTINATION", MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_PASS_ALL_CONTEXT);

            MQMessage msgSource = new MQMessage();
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.Options |= MQC.MQGMO_SYNCPOINT;
            qSource.Get(msgSource,gmo);
            if (msgSource != null)
            {
                MQMessage msgDestination = new MQMessage();
                MQPutMessageOptions pmo = new MQPutMessageOptions();
                pmo.ContextReference = qSource;
                qDestination.Put(msgSource, pmo);
                qmSource.Commit();
            }
        }
        catch (MQException mqEx)
        {
            qmSource.Backout();
            Console.WriteLine(mqEx);
        }
        catch (Exception otherEx)
        {
            Console.WriteLine(otherEx);
        }
    }

Upvotes: 2

Related Questions