Websphere 7 MQueue: how to access queue depth from Java?

前端 未结 3 436
北荒
北荒 2020-12-20 01:14

I\'d like to write some code to monitor the queue size on Websphere 7 MQ. This is the code I\'ve come up with

   MQEnvironment.hostname = \"10.21.1.19\"; 
           


        
3条回答
  •  太阳男子
    2020-12-20 02:06

    If you want something that works for both SIBus and MQ implementations you are best to stick with the JMS API's (as these are then also portable to other implementations of JMS as well).

    So what I'd do is:

    //ctx is InitialContext
    ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/CF");
    Connection conn = cf.createConnection();
    conn.start();
    
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = (Queue) ctx.lookup("jms/MyQueue");
    QueueBrowser qb = session.createBrowser(queue);
    
    //sadly, getting this enum is the best the JMS API can offer.
    //but the upside is the code is portable AND it can run over MQ and SIBus
    //implementations.
    Enumeration queueMessageEnum = qb.getEnumeration();
    int count = 0;
    while(queueMessageEnum.hasMoreElements()) {
      queueMessageEnum.nextElement();
      count++;
    }
    

提交回复
热议问题