Skip to main content
 首页 » 编程设计

c#之Websphere MQ之在 .NET 中从一个队列移动到另一个队列时保持消息上下文和身份

2024年02月06日26lvdongjie

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

我试图破译在线文档,并尝试了 MQC.MQOO_PASS_ALL_CONTEXT 和 MQOO_SAVE_ALL_CONTEXT 在打开队列中的所有组合,以及 MQC.MQPMO_PASS_ALL_CONTEXT 投入调用,无济于事。我尝试将 MQPutMessageOptions.ContextReference 设置为源和 目标队列,但我继续收到异常,MQRC_CONTEXT_HANDLE_ERROR 或 MQRC_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),即使我已经使用了所有指定的打开和获取消息选项。

请您参考如下方法:

添加到 Morag 的回答中,这里是移动带有上下文的消息的片段。我还使用 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); 
        } 
    }