使用XMS.Net的Websphere MQ队列深度

时间:2012-07-17 08:12:08

标签: c# .net ibm-mq

由于我们使用XMS.net(有时似乎放弃侦听队列中的消息的Windows服务)遇到了IBM Websphere MQ的一些问题,我们想创建一个简单的应用程序来监视某些队列的深度(或者队列深度超过某个阈值时能够提醒某人的队列中的消息数量。此应用程序将由任务调度程序在特定时间间隔启动,并将“读出”X队列的队列深度(以及其他一些统计信息)。

我们的Windows服务正在使用以下代码,我希望我可以为我们的“监控”应用程序重用相同的“知识”。

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    //Read config values
    string QueueManager = ConfigurationManager.AppSettings["queuemanager"];
    string Channel = ConfigurationManager.AppSettings["channel"];
    string Queue = ConfigurationManager.AppSettings["queue"];
    string HostIP = ConfigurationManager.AppSettings["host"];
    int Port = int.Parse(ConfigurationManager.AppSettings["port"]);

    //Create connection
    var factoryfactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
    var connectionfactory = factoryfactory.CreateConnectionFactory();

    connectionfactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, QueueManager);
    connectionfactory.SetStringProperty(XMSC.WMQ_HOST_NAME, HostIP);
    connectionfactory.SetIntProperty(XMSC.WMQ_PORT, Port);
    connectionfactory.SetStringProperty(XMSC.WMQ_CHANNEL, Channel);
    connectionfactory.SetIntProperty(XMSC.WMQ_BROKER_VERSION, XMSC.WMQ_BROKER_V2);
    connectionfactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT_UNMANAGED);

    Console.WriteLine("Creating connection");
    var connection = connectionfactory.CreateConnection();
    connection.ExceptionListener = new ExceptionListener(OnXMSExceptionReceived);

    //Create a_session
    Console.WriteLine("Creating sessions");
    var session = connection.CreateSession(false, AcknowledgeMode.ClientAcknowledge);

    //Create queue
    Console.WriteLine("Creating queue");
    var queue = session.CreateQueue(string.Format("queue://{0}/{1}", QueueManager, Queue));

我浏览了sessionqueue等属性,但是,当然,没有“当前队列深度”属性。我可以在这些对象上使用GetIntProperty()GetLongProperty(),但我不知道要使用哪个常量(我已经看过IBM.XMS.MQC.MQIA_CURRENT_Q_DEPTH但包含intGet...Property()期望string作为参数。)

长话短说:如何以上述代码作为起点来检索队列深度?是否可以使用XMS.Net?

2 个答案:

答案 0 :(得分:4)

我能够使用MQ as Shashi suggested来解决它。为此,您需要引用amqmdnet.dll(C:\ Program Files(x86)\ IBM \ WebSphere MQ \ bin \ amqmdnet.dll)并使用以下(示例)代码。请注意,这是一个简单的例子,不包括异常处理等。

using System;
using System.Collections;
using System.Configuration;
using IBM.WMQ;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //Connection properties
            var properties = new Hashtable();
            properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
            properties.Add(MQC.CHANNEL_PROPERTY, "SOME.CHANNEL.TCP");
            properties.Add(MQC.HOST_NAME_PROPERTY, "12.34.56.78");
            properties.Add(MQC.PORT_PROPERTY, 1416);

            var qmgr = new MQQueueManager("MYQMGR", properties);

            Console.WriteLine("Local  : {0}", GetQueueDepth(qmgr, "FOO.LOCALQ"));
            Console.WriteLine("Report : {0}", GetQueueDepth(qmgr, "FOO.REPORTQ"));
        }

        public static int GetQueueDepth(MQQueueManager queuemgr, string queue)
        {
            return queuemgr.AccessQueue(queue,
                MQC.MQOO_INPUT_AS_Q_DEF + 
                MQC.MQOO_FAIL_IF_QUIESCING + 
                MQC.MQOO_INQUIRE).CurrentDepth;
        }
    }
}

这比my initial "workaround"更好。

答案 1 :(得分:2)

无法确定使用XMS .NET队列深度。队列深度特定于消息传递提供程序而非JMS / XMS,因此您需要使用MQ API来获取队列深度。您可以使用MQ .NET API来查找队列深度。 MQQueue.CurrentDepth将给出队列中的消息数。

IMO最好调查为什么XMS .NET服务停止侦听消息而不是编写另一个程序来监视队列深度。

相关问题