2012-11-28 24 views
7

जेएमएस कतार पर जाने और इसमें सभी संदेश प्राप्त करने का सबसे अच्छा तरीका क्या है?किसी जेएमएस कतार में संदेश की संख्या

कतार में संदेशों की संख्या कैसे गिन सकते हैं?

धन्यवाद।

+1

आप कुछ मामलों में JMX उपयोग कर सकते हैं (JMS कार्यान्वयन की निर्भर करता है) – user1516873

+0

मैं देख का उपयोग करना है, 'ActiveMQ टैग ActiveMQ के लिए उदाहरण http://java.dzone.com/articles/managing-activemq-jmx-apis – user1516873

उत्तर

6

यह आपको एक कतार

public static void main(String[] args) throws Exception 
    { 
     // get the initial context 
     InitialContext ctx = new InitialContext(); 

     // lookup the queue object 
     Queue queue = (Queue) ctx.lookup("queue/queue0"); 

     // lookup the queue connection factory 
     QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx. 
      lookup("queue/connectionFactory"); 

     // create a queue connection 
     QueueConnection queueConn = connFactory.createQueueConnection(); 

     // create a queue session 
     QueueSession queueSession = queueConn.createQueueSession(false, 
      Session.AUTO_ACKNOWLEDGE); 

     // create a queue browser 
     QueueBrowser queueBrowser = queueSession.createBrowser(queue); 

     // start the connection 
     queueConn.start(); 

     // browse the messages 
     Enumeration e = queueBrowser.getEnumeration(); 
     int numMsgs = 0; 

     // count number of messages 
     while (e.hasMoreElements()) { 
      Message message = (Message) e.nextElement(); 
      numMsgs++; 
     } 

     System.out.println(queue + " has " + numMsgs + " messages"); 

     // close the queue connection 
     queueConn.close(); 
    } 
+0

मैं वास्तव में इस उदाहरण को चलाता हूं और किसी कारण से मैसेज गिनती 400 दिखाती है जब मेरे पास कतार –

+0

पर 5000 मैसेज हैं, आप कैसे कहते हैं आपके पास कतार में 5000 संदेश हैं। – sunleo

+0

मैं इसे अपने ActiveMQ कंसोल –

5

में संदेश का कोई कैसे भरोसा कर सकते हैं JmsTemplate

public int getMessageCount(String messageSelector) 
{ 
    return jmsTemplate.browseSelected(messageSelector, new BrowserCallback<Integer>() { 
     @Override 
     public Integer doInJms(Session s, QueueBrowser qb) throws JMSException 
     { 
      return Collections.list(qb.getEnumeration()).size(); 
     } 
    }); 
} 
संबंधित मुद्दे