由于我们使用 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));
Run Code Online (Sandbox Code Playgroud)
我浏览了等的属性session,queue但是,当然,没有“当前队列深度”属性。我可以在这些对象上使用GetIntProperty()或GetLongProperty(),但我不知道要使用哪个常量(我已经看到 IBM.XMS.MQC.MQIA_CURRENT_Q_DEPTH 但它包含一个int并Get...Property()期望一个string作为参数)。
长话短说:我将如何以上述代码为起点检索队列深度?是否可以使用 XMS.Net?
正如 Shashi 建议的那样,我能够使用MQ API来解决它。为此,您需要引用 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这比我最初的“解决方法”要好得多。
使用 XMS .NET 无法确定队列深度。队列深度特定于消息传递提供程序,而不是 JMS/XMS,因此您需要使用 MQ API 来获取队列深度。您可以使用 MQ .NET API 来查找队列深度。MQQueue.CurrentDepth 将给出队列中消息的数量。
IMO 最好调查一下为什么 XMS .NET 服务停止侦听消息,而不是编写另一个程序来监视队列深度。
| 归档时间: |
|
| 查看次数: |
6114 次 |
| 最近记录: |