IBM MQ使用Java获取LGETTIME [英] IBM MQ fetch LGETTIME using Java

查看:151
本文介绍了IBM MQ使用Java获取LGETTIME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当前深度,上次投放时间&最后使用IBM Mq的Java来监视队列性能.我能够获取LPUTTIME&当前深度使用下面的代码.我正在使用MQMESSAGE浏览API.但是我很难获取LGETTIME(最后一条消息获取时间)-超出队列处理的最后一条消息.因为MQMESSAGE浏览没有像上一次获取时间那样的东西.关于此有任何帮助吗?????

I am trying to fetch Current Depth, Last Puttime & last get time using java from IBM Mq's to monitor queue performance. I was able to fetch LPUTTIME & current depth using below code. I am using MQMESSAGE browse api. But i am having hard time to fetch the LGETTIME (Last message get time)- last message that was processed out of queue. Because MQMESSAGE browse has nothing like Last get time. Any help regarding this ????

public int depthOf(String queueName) throws MQException {
    MQQueue queue = qmgr.accessQueue(queueName, mqOpnOpt, null, null, null);
    int qDpth = queue.getCurrentDepth();

    queue.close();
    //qmgr.disconnect();
    System.out.println("Current Depth of "+ queueName + " is " + qDpth);                
    return qDpth;
}

@SuppressWarnings("unchecked")
private MQQueueManager createQueueManager() throws MQException {
    MQEnvironment.channel = channel;
    MQEnvironment.port = port;
    MQEnvironment.hostname = host;
    MQEnvironment.properties.put(CMQC.TRANSPORT_PROPERTY,
            CMQC.TRANSPORT_MQSERIES);

    return new MQQueueManager(manager);
}

/**
 * Returns 1st message put time when it is put in to Queue and not consumed.
 * 
 * @return
 * @throws MQException
 */
public int queueMsgAge() throws MQException {
    MQQueue queue = null;
    MQMessage message = new MQMessage();
    queue = qmgr.accessQueue(queueName, MQC.MQOO_BROWSE
            | MQC.MQOO_FAIL_IF_QUIESCING);
    MQGetMessageOptions gmo = new MQGetMessageOptions();
    gmo.options = MQC.MQGMO_BROWSE_FIRST | MQC.MQGMO_NO_WAIT;
    queue.get(message, gmo);

    GregorianCalendar cal = message.putDateTime;
    long ageInMillis = new java.util.Date().getTime()
            - cal.getTime().getTime();
    int ageInSeconds = (int) ageInMillis / 1000;
    System.out.println("Put Date & Time: "+ cal.getTime()
            + " Age of Msg in seconds: " + ageInSeconds + " Queue Name "
            + queueName);
    _log.info("Put Date & Time: " + cal.getTime() + " Age of Msg in seconds: "
            + ageInSeconds + " Queue Name " + queueName);
    return ageInSeconds;
}

推荐答案

首先阅读JoshMc&莫拉格在上面的评论.

First read JoshMc & Morag's comments above.

第二,您应该使用runmqsc发出两种类型的队列状态"命令.即HANDLE vs QUEUE.注意:您对与队列状态类型(QUEUE)相关的值感兴趣.

Second, you should play around with runmqsc issuing both types of Queue Status commands. i.e. HANDLE vs QUEUE. Note: You are interested in the values related to Queue Status Type(QUEUE).

即runmqsc命令:

i.e. runmqsc command:

DIS QSTATUS(TEST.Q1) TYPE(QUEUE)

第三,不要使用MQEnvironment类,因为它不是线程安全的.将连接信息放入哈希表中,并将哈希表传递给MQQueueManager类.

Third, don't use the MQEnvironment class as it is not thread safe. Put the connection information in a Hashtable and pass the Hashtable to the MQQueueManager class.

如上面的注释中所述,您无法通过常规MQ程序检索该信息.您需要编写一个MQ Java程序,该程序将PCF命令发布到队列管理器的命令服务器.

As mentioned in the comments above, you cannot retrieve that information via a regular MQ program. You need to write a MQ Java program that issues a PCF command to the queue manager's command server.

创建PCF请求消息时,必须按特定顺序添加参数.有点痛苦,但您只是习惯了.

When creating a PCF request message, the parameters MUST added in a particular order. It is a bit of a pain but you just get use to it.

这是一个简单的MQ Java程序,它将发出PCF查询队列状态"命令,获取PCF响应消息,遍历PCF响应并输出信息.

Here is a simple MQ Java program that will issue a PCF "Inquire Queue Status" command, get the PCF response messages, loop through the PCF responses and output the information.

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import com.ibm.mq.MQException;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.headers.MQDataException;
import com.ibm.mq.headers.pcf.PCFMessage;
import com.ibm.mq.headers.pcf.PCFMessageAgent;

/**
 * Program Name
 *  MQListQueueStatus01
 *
 * Description
 *  This java class issues a PCF "inquire queue status" request message for all ("*") queues 
 *  of a remote queue manager. 
 *
 * Sample Command Line Parameters
 *  -m MQA1 -h 127.0.0.1 -p 1414 -c TEST.CHL -q TEST.Q1 -u UserID -x Password
 *
 * @author Roger Lacroix
 */
public class MQListQueueStatus01
{
   private static final SimpleDateFormat  lOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");

   private Hashtable<String,String> params;
   private Hashtable<String,Object> mqht;
   private String qMgrName;

   public MQListQueueStatus01()
   {
      super();
      params = new Hashtable<String,String>();
      mqht = new Hashtable<String,Object>();
   }

   /**
    * Make sure the required parameters are present.
    * @return true/false
    */
   private boolean allParamsPresent()
   {
      boolean b = params.containsKey("-h") && params.containsKey("-p") &&
                  params.containsKey("-c") && params.containsKey("-m") &&
                  params.containsKey("-u") && params.containsKey("-x");
      if (b)
      {
         try
         {
            Integer.parseInt((String) params.get("-p"));
         }
         catch (NumberFormatException e)
         {
            b = false;
         }
      }

      return b;
   }

   /**
    * Extract the command-line parameters and initialize the MQ HashTable.
    * @param args
    * @throws IllegalArgumentException
    */
   private void init(String[] args) throws IllegalArgumentException
   {
      int port = 1414;
      if (args.length > 0 && (args.length % 2) == 0)
      {
         for (int i = 0; i < args.length; i += 2)
         {
            params.put(args[i], args[i + 1]);
         }
      }
      else
      {
         throw new IllegalArgumentException();
      }

      if (allParamsPresent())
      {
         qMgrName = (String) params.get("-m");

         try
         {
            port = Integer.parseInt((String) params.get("-p"));
         }
         catch (NumberFormatException e)
         {
            port = 1414;
         }

         mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c"));
         mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h"));
         mqht.put(CMQC.PORT_PROPERTY, new Integer(port));
         mqht.put(CMQC.USER_ID_PROPERTY, params.get("-u"));
         mqht.put(CMQC.PASSWORD_PROPERTY, params.get("-x"));

         // I don't want to see MQ exceptions at the console.
         MQException.log = null;
      }
      else
      {
         throw new IllegalArgumentException();
      }
   }

   private void doPCF()
   {
      MQQueueManager qMgr = null;
      PCFMessageAgent agent = null;
      PCFMessage   request = null;
      PCFMessage[] responses = null;

      try
      {
         qMgr = new MQQueueManager(qMgrName, mqht);
         MQListQueueStatus01.logger("successfully connected to "+ qMgrName);

         agent = new PCFMessageAgent(qMgr);
         MQListQueueStatus01.logger("successfully created agent");

         // https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_9.1.0/com.ibm.mq.ref.adm.doc/q087880_.htm
         request = new PCFMessage(CMQCFC.MQCMD_INQUIRE_Q_STATUS);

         /**
          * You can explicitly set a queue name like "TEST.Q1" or
          * use a wild card like "TEST.*"
          */
         request.addParameter (CMQC.MQCA_Q_NAME, "*");
         request.addParameter(CMQCFC.MQIACF_Q_STATUS_TYPE, CMQCFC.MQIACF_Q_STATUS);

         request.addParameter(CMQCFC.MQIACF_Q_STATUS_ATTRS,
               new int [] { CMQC.MQCA_Q_NAME,
                            CMQC.MQIA_CURRENT_Q_DEPTH,
                            CMQC.MQIA_OPEN_INPUT_COUNT,
                            CMQC.MQIA_OPEN_OUTPUT_COUNT,
                            CMQCFC.MQCACF_LAST_PUT_DATE,
                            CMQCFC.MQCACF_LAST_PUT_TIME,
                            CMQCFC.MQCACF_LAST_GET_DATE,
                            CMQCFC.MQCACF_LAST_GET_TIME,
                          });

         responses = agent.send(request);

         for (int i = 0; i < responses.length; i++)
         {
            if ((responses[i]).getCompCode() == CMQC.MQCC_OK)
            {
               String name = responses[i].getStringParameterValue(CMQC.MQCA_Q_NAME);

               int depth = responses[i].getIntParameterValue(CMQC.MQIA_CURRENT_Q_DEPTH);
               int iprocs = responses[i].getIntParameterValue(CMQC.MQIA_OPEN_INPUT_COUNT);
               int oprocs = responses[i].getIntParameterValue(CMQC.MQIA_OPEN_OUTPUT_COUNT);

               String lastPutDate = responses[i].getStringParameterValue(CMQCFC.MQCACF_LAST_PUT_DATE);
               String lastPutTime = responses[i].getStringParameterValue(CMQCFC.MQCACF_LAST_PUT_TIME);

               String lastGetDate = responses[i].getStringParameterValue(CMQCFC.MQCACF_LAST_GET_DATE);
               String lastGetTime = responses[i].getStringParameterValue(CMQCFC.MQCACF_LAST_GET_TIME);

               MQListQueueStatus01.logger("Name="+name.trim()+" : depth="+depth + " : iprocs="+iprocs+" : oprocs="+oprocs+" : lastPutDate='"+lastPutDate.trim()+"' : lastPutTime='"+lastPutTime.trim()+"' : lastGetDate='"+lastGetDate.trim()+"' : lastGetTime='"+lastGetTime.trim()+"'");
            }
         }
      }
      catch (MQException e)
      {
         MQListQueueStatus01.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
      }
      catch (IOException e)
      {
         MQListQueueStatus01.logger("IOException:" +e.getLocalizedMessage());
      }
      catch (MQDataException e)
      {
         MQListQueueStatus01.logger("MQDataException:" +e.getLocalizedMessage());
      }
      finally
      {
         try
         {
            if (agent != null)
            {
               agent.disconnect();
               MQListQueueStatus01.logger("disconnected from agent");
            }
         }
         catch (MQDataException e)
         {
            MQListQueueStatus01.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
         }

         try
         {
            if (qMgr != null)
            {
               qMgr.disconnect();
               MQListQueueStatus01.logger("disconnected from "+ qMgrName);
            }
         }
         catch (MQException e)
         {
            MQListQueueStatus01.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
         }
      }
   }

   /**
    * A simple logger method
    * @param data
    */
   public static void logger(String data)
   {
      String className = Thread.currentThread().getStackTrace()[2].getClassName();

      // Remove the package info.
      if ( (className != null) && (className.lastIndexOf('.') != -1) )
         className = className.substring(className.lastIndexOf('.')+1);

      System.out.println(lOGGER_TIMESTAMP.format(new Date())+" "+className+": "+Thread.currentThread().getStackTrace()[2].getMethodName()+": "+data);
   }

   public static void main(String[] args)
   {
      MQListQueueStatus01 mqlqs = new MQListQueueStatus01();

      try
      {
         mqlqs.init(args);
         mqlqs.doPCF();
      }
      catch (IllegalArgumentException e)
      {
         MQListQueueStatus01.logger("Usage: java MQListQueueStatus01 -m QueueManagerName -h host -p port -c channel -u UserID -x Password");
         System.exit(1);
      }

      System.exit(0);
   }
}

这篇关于IBM MQ使用Java获取LGETTIME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆