【问题标题】:Websphere MQ - Keeping message context and identity when moving from one queue to another in .NETWebsphere MQ - 在 .NET 中从一个队列移动到另一个队列时保持消息上下文和身份
【发布时间】:2015-04-13 22:45:34
【问题描述】:

我的任务是用 C#.NET 编写一个自定义应用程序,将消息从一个队列移动到另一个队列,同时保持所有上下文和身份信息与原始消息相同。

我尝试破译在线文档,并尝试了 MQC.MQOO_PASS_ALL_CONTEXTMQOO_SAVE_ALL_CONTEXT 在打开队列中的所有组合,以及 MQC.MQPMO_PASS_ALL_CONTEXT strong> 看跌期权,无济于事。我尝试将 MQPutMessageOptions.ContextReference 设置为源和 目标队列,但我继续收到异常,MQRC_CONTEXT_HANDLE_ERRORMQRC_OPTIONS_ERROR,我无法在在线文档中找到足够的信息来确定问题。

我正在使用运行时版本 v2.0.50727,amqmdnet.dll 版本 7.5.0.1。如果有人知道我需要使用的正确打开和/或放置消息选项设置,我将不胜感激。谢谢。

***** 第二次更新 *****

这是一个非常简单的 gui 程序来测试这些类,我得到了相同的结果:

    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;
    }
}

GUI 只提示输入用户名和密码(通过 LDAP 调用验证),然后启用运行 btnTest_Click 方法的“测试”按钮。即使我已经使用了所有指定的打开和获取消息选项,我仍然得到相同的异常 (MQRC_CONTEXT_HANDLE_ERROR)。

【问题讨论】:

  • 我已经对所有的 open、get 和 put 选项进行了三次检查 - 关于“上下文”,我没有得到一些东西,因为现在我得到的异常是“MQRC_CONTEXT_HANDLE_ERROR”(原因代码2097.)

标签: c# .net ibm-mq


【解决方案1】:

添加到 Morag 的答案,这里是 sn-p 用于移动带有上下文的消息。我还使用 SYNC_POINT 来确保在消息成功放入目标队列后从源队列中删除消息。

    /// <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);
        }
    }

【讨论】:

    【解决方案2】:

    用于移动消息同时保留其中所有上下文信息的正确选项如下。

    打开队列以从中获取消息时,请使用MQOO_SAVE_ALL_CONTEXT。上下文将在获取时保存在表示此打开队列的对象句柄中。

    当打开队列来放置消息时,使用MQOO_PASS_ALL_CONTEXT。然后在放置消息时,使用MQPMO_PASS_ALL_CONTEXT 并提供包含上下文的对象句柄。

    收到返回码MQRC_CONTEXT_HANDLE_ERROR 表示您在第一次打开时没有使用MQOO_SAVE_ALL_CONTEXT,但随后尝试在放置时使用对象句柄。这也可能意味着它不是对第一个队列的正确引用。在 MQ API 中,它是一个对象句柄 (MQHOBJ),而在 .Net 中,它是 MQQueue(参见 MQPutMessageOptions .NET class)。

    收到返回码MQRC_OPTIONS_ERROR 表示您混合和匹配选项过多。

    【讨论】:

    • 我收到 MQRC_CONTEXT_HANDLE_ERROR 异常,即使源队列是使用 MQC.MQOO_INPUT_SHARED + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_SAVE_ALL_CONTEXT 打开的。请给我一个功能组合。
    • 此外,MQPutMessageOptions 中的属性“Context”如果没有记录该值的来源,则价值不大。当我填充 MQPutMessageOptions.ContextReference 时,“上下文”属性中会自动出现一个值。这是它应该有的价值吗?
    猜你喜欢
    • 2011-03-15
    • 2022-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多