MQ消息有时不下载,建议使用远程MQ监控工具

MQ mesage is not downloading sometime , Suggest any remote MQ monitor tool

我编写了代码来连接远程 MQ 并下载消息,但有时会丢失一些消息。我正在发布我的代码。谁能告诉我这段代码有什么错误

using IBM.WMQ;

internal class MQReader
{
    private static MQQueueManager queueManager;
    private static MQMessage queueMessage;
    private static MQGetMessageOptions queueGetMessageOptions;
    private static MQQueue queue;
    static string strReturn = "";
    static string message = "";
    static string mqexception = "";
    static string connerror = "";
    static string QueueName;
    static string QueueManagerName;
    static string ChannelInfo;
    static string channelName;
    static string PortNumber;
    static string transportType;
    static string connectionName;
    static bool running;
    static bool conresult;
    static string checkconnexp;
    static bool connerrorflag;
    static bool checkconnresult;

    public static bool connectMQ()
    {
        bool flag;
        QueueManagerName = ConfigurationManager.AppSettings["QueueManager"];
        QueueName = ConfigurationManager.AppSettings["Queuename"];
        ChannelInfo = ConfigurationManager.AppSettings["ChannelInformation"];
        PortNumber = ConfigurationManager.AppSettings["Port"];
        char[] separator = { '/' };
        string[] ChannelParams;
        ChannelParams = ChannelInfo.Split(separator);
        channelName = ConfigurationManager.AppSettings["Channel"];
        transportType = ConfigurationManager.AppSettings["TransportType"];
        connectionName = ConfigurationManager.AppSettings["ConnectionName"];

        try
        {
            queueManager = new MQQueueManager(QueueManagerName,
                channelName, connectionName);
            queue = queueManager.AccessQueue(QueueName,
                MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
            connerror = "Connected Successfully";
            flag = true;
        }
        catch (MQException connectionexp)
        {
            connerror = "Exception: " + connectionexp.Message;
            logFile(connerror);
            flag = false;
        }

        return flag;
    }

    private static void logFile(string errormsg)
    {
        string path = ConfigurationManager.AppSettings["logFilePath"];
        string fileName = path + "MQLog" + DateTime.Now.Date.ToString("yyyyMMdd") + ".txt";
        System.IO.File.AppendAllText(fileName, errormsg + Environment.NewLine);
    }

    public static void getMessage()
    {
        conresult = connectMQ();
        running = true;

        while (running)
        {
            if (conresult == true)
            {
                try
                {
                    queueMessage = new MQMessage();
                    queueMessage.Format = MQC.MQFMT_STRING;
                    queueGetMessageOptions = new MQGetMessageOptions();
                    queueGetMessageOptions.Options |= MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING;
                    queueGetMessageOptions.WaitInterval = 60000;
                    queue.Get(queueMessage, queueGetMessageOptions);
                    message = queueMessage.ReadString(queueMessage.MessageLength);
                }
                catch (MQException exp)
                {
                    mqexception = exp.Message;
                    message = "Exception: " + mqexception;
                }

                if (message != "Exception: " + mqexception)
                {
                    string path = ConfigurationManager.AppSettings["xmlFilePath"];
                    string fileName = path + "MQMessage" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";
                    System.IO.File.WriteAllText(fileName, message);         
                }
                else   
                {
                    logFile(message);
                    queueManager.Close();
                    queueManager.Disconnect();
                    conresult = false;
                }
            }

            if (conresult == false)
            {                   
                conresult = connectMQ();
            }
        }
    }

    public void InitializeConnections()
    {
        getMessage();
    }       

    public void StopIt()
    {
        queueManager.Disconnect();
        running = false;
    }
}

有时邮件未下载或丢失到本地目录中。所有连接代码都是凭据来自 web.config.

编辑 请建议任何远程 mq 监控工具,该工具将在消息发送到 mq 和下载时进行记录。任何强大的 shell 脚本或任何工具都可以。

我会很礼貌,只对那个代码说 'Yuk'。

首先,C# 是一种面向对象的语言,因此您应该学习如何使用 try、catch 和 finally。

if (message != "Exception: " + mqexception)

那是什么?这不是您进行字符串比较的方式,为什么 try{} 部分中没有该代码?

string fileName = path + "MQMessage" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";

嗯。你没有丢失你的信息,你把它们扔掉了。如果您在同一毫秒内收到 2 条或更多条消息会怎样?

来自 Write All Text 的文档:

Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.

因此,这意味着当您在同一毫秒内收到 2 条或更多消息时,下一条会覆盖前一条。