2012-11-28 44 views
7

Cách tốt nhất để đi qua hàng đợi JMS và nhận tất cả thư trong đó là gì?Đếm số lượng thư trong hàng đợi JMS

Làm cách nào để đếm số lượng thư trong hàng đợi?

Cảm ơn.

+1

Bạn có thể sử dụng jmx trong một số trường hợp (phụ thuộc thực hiện JMS) – user1516873

+0

tôi thấy, 'ActiveMQ 'tag. Ví dụ cho ActiveMQ http://java.dzone.com/articles/managing-activemq-jmx-apis – user1516873

Trả lời

6

Đây là cách bạn có thể đếm Số tin nhắn trong một Queue

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

Tôi thực sự chạy ví dụ này và vì lý do nào đó, số lượng tin nhắn quảng cáo cho thấy 400 khi tôi có 5000 sự cố trên hàng đợi –

+0

bạn nói như thế nào bạn có 5000 tin nhắn trong một hàng đợi. – sunleo

+0

Tôi nhìn thấy nó một cách vật lý trên bàn điều khiển ActiveMQ –

5

Sử dụng 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(); 
     } 
    }); 
} 
Các vấn đề liên quan