浏览 IBM MQ 但不获取消息

Browse IBM MQ whitout get the messages

您好,我是 IBM MQ 环境的新手,所以我需要您的帮助.... 我需要开发一个脚本来检查 IBMMQ(在我的工厂之外)队列以验证是否有消息等待从我的应用程序服务器获取(我的意思是如果我的应用程序服务器由于任何原因失去了与 IBMMQ 的连接,我想要被告知,在这种模式下我可以进行更正)。 如果可能的话,我需要 PowereShell 或 C# 中的脚本。 非常感谢。

有示例代码可以帮助您入门 - https://github.com/ibm-messaging/mq-dev-patterns/blob/master/dotnet/dotNetGet.cs

CreateConsumer 方法调用替换为 CreateBrowser

即。修改后的代码段变为

        private void ReceiveMessagesFromEndpoint(IConnectionFactory cf)
        {
            IConnection connectionWMQ;
            ISession sessionWMQ;
            IDestination destination;
            IMessageBrowser browser;
            ITextMessage textMessage;

            // Create connection.
            connectionWMQ = cf.CreateConnection();
            Console.WriteLine("Connection created");

            // Create session
            sessionWMQ = connectionWMQ.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
            Console.WriteLine("Session created");

            // Create destination
            destination = sessionWMQ.CreateQueue(env.Conn.queue_name);
            Console.WriteLine("Destination created");

            // Create browser
            browser = sessionWMQ.CreateBrowser(destination);
            Console.WriteLine("Browser created");

            ...

有关设置的说明可在相关的自述文件中找到 - https://github.com/ibm-messaging/mq-dev-patterns/blob/master/dotnet/README.md

不要浏览队列以获取当前队列深度,因为这会导致过多的网络流量并且完全没有必要。

这是一个 C#/.NET/MQ 示例程序(功能齐全),它将查询队列的当前深度。

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

/// <summary> Program Name
/// MQTest75
///
/// Description
/// This C# class will connect to a remote queue manager
/// and inquire on the current depth of the queue using a managed .NET environment.
///
/// </summary>
/// <author>  Roger Lacroix
/// </author>
namespace MQTest75
{
   class MQTest75
   {
      private Hashtable qMgrProp = null;
      private System.String qManager;
      private System.String inputQName;

      /*
      * The constructor
      */
      public MQTest75()
         : base()
      {
      }

      /// <summary> Make sure the required parameters are present.</summary>
      /// <returns> true/false
      /// </returns>
      private bool allParamsPresent()
      {
         bool b = false;

         if ( (ConfigurationManager.AppSettings["ConnectionName"] != null) &&
              (ConfigurationManager.AppSettings["Port"] != null) &&
              (ConfigurationManager.AppSettings["ChannelName"] != null) &&
              (ConfigurationManager.AppSettings["QMgrName"] != null) &&
              (ConfigurationManager.AppSettings["QueueName"] != null) )
         {
            try
            {
               System.Int32.Parse(ConfigurationManager.AppSettings["Port"]);
               b = true;
            }
            catch (System.FormatException e)
            {
               b = false;
            }
         }

         return b;
      }

      /// <summary> Extract the configuration applicaiton settings and initialize the MQ variables.</summary>
      /// <param name="args">
      /// </param>
      /// <throws>  IllegalArgumentException </throws>
      private void init(System.String[] args)
      {
         if (allParamsPresent())
         {
            qManager = ConfigurationManager.AppSettings["QMgrName"];
            inputQName = ConfigurationManager.AppSettings["QueueName"];

            qMgrProp = new Hashtable();
            qMgrProp.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);

            qMgrProp.Add(MQC.HOST_NAME_PROPERTY, ConfigurationManager.AppSettings["ConnectionName"]);
            qMgrProp.Add(MQC.CHANNEL_PROPERTY, ConfigurationManager.AppSettings["ChannelName"]);

            try
            {
               qMgrProp.Add(MQC.PORT_PROPERTY, System.Int32.Parse(ConfigurationManager.AppSettings["Port"]));
            }
            catch (System.FormatException e)
            {
               qMgrProp.Add(MQC.PORT_PROPERTY, 1414);
            }

            if (ConfigurationManager.AppSettings["UserId"] != null)
               qMgrProp.Add(MQC.USER_ID_PROPERTY, ConfigurationManager.AppSettings["UserId"]);

            if (ConfigurationManager.AppSettings["Password"] != null)
               qMgrProp.Add(MQC.PASSWORD_PROPERTY, ConfigurationManager.AppSettings["Password"]);

            logger("Parameters:");
            logger("  QMgrName ='" + qManager + "'");
            logger("  Queue Name ='" + inputQName + "'");

            logger("Connection values:");
            foreach (DictionaryEntry de in qMgrProp)
            {
               logger("  " + de.Key + " = '" + de.Value + "'");
            }
         }
         else
         {
            throw new System.ArgumentException();
         }
      }

      /// <summary> Connect, open queue, output the current queue depth, close queue and disconnect.</summary>
      /// <throws>  MQException </throws>
      private void handleIt()
      {
         MQQueueManager qMgr = null;
         MQQueue inQ = null;
         int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING;

         try
         {
            qMgr = new MQQueueManager(qManager, qMgrProp);
            logger("Successfully connected to " + qManager);

            inQ = qMgr.AccessQueue(inputQName, openOptions);
            logger("Successfully opened " + inputQName);

            logger("Current queue depth is " + inQ.CurrentDepth);
         }
         catch (MQException mqex)
         {
            logger("CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
         }
         catch (System.IO.IOException ioex)
         {
            logger("Error: ioex=" + ioex);
         }
         finally
         {
            try
            {
                if (inQ != null)
                {
                    inQ.Close();
                    logger("Closed: " + inputQName);
                }
            }
            catch (MQException mqex)
            {
                logger("CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
            }

            try
            {
                if (qMgr != null)
                {
                    qMgr.Disconnect();
                    logger("Disconnected from " + qManager);
                }
            }
            catch (MQException mqex)
            {
                logger("CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
            }
         }
      }

      /// <summary> Output the log message to stdio.</summary>
      /// <param name="data">
      /// </param>
      private void logger(String data)
      {
         DateTime myDateTime = DateTime.Now;
         System.Console.Out.WriteLine(myDateTime.ToString("yyyy/MM/dd HH:mm:ss.fff") + " " + this.GetType().Name + ": " + data);
      }

      /// <summary> main line</summary>
      /// <param name="args">
      /// </param>
      //        [STAThread]
      public static void Main(System.String[] args)
      {
         MQTest75 mqt = new MQTest75();

         try
         {
            mqt.init(args);
            mqt.handleIt();
         }
         catch (System.ArgumentException e)
         {
            System.Console.Out.WriteLine("Check your configuration file for an incorrect parameter.");
            System.Environment.Exit(1);
         }
         catch (MQException e)
         {
            System.Console.Out.WriteLine(e);
            System.Environment.Exit(1);
         }

         System.Environment.Exit(0);
      }
   }
}

只需创建一个名为 MQTest75.exe.config 的配置并将以下内容放入其中:

<configuration>
  <appSettings>
    <add key="QMgrName" value="MQA1"/>
    <add key="ConnectionName" value="10.10.10.10"/>
    <add key="Port" value="1414"/>
    <add key="ChannelName" value="TEST.CHL"/>
    <add key="QueueName" value="TEST.Q1"/>
    <add key="UserId" value="tester"/>
    <add key="Password" value="mypwd"/>
  </appSettings>
</configuration>