2012-06-21 34 views
0

以下是我的消費:如何使用消息的N多一個消費者在ActiveMQ中

public static void main(String[] args) throws JMSException { 
     // Getting JMS connection from the server 
     ConnectionFactory connectionFactory 
      = new ActiveMQConnectionFactory(url); 
     Connection connection = connectionFactory.createConnection(); 
     connection.start(); 

     // Creating session for seding messages 
     Session session = connection.createSession(false, 
      Session.AUTO_ACKNOWLEDGE); 

     // Getting the queue 'TESTQUEUE' 
     Destination destination = session.createQueue(subject); 

     // MessageConsumer is used for receiving (consuming) messages 
     MessageConsumer consumer = session.createConsumer(destination); 

     // Here we receive the message. 
     // By default this call is blocking, which means it will wait 
     // for a message to arrive on the queue. 
     Message message = consumer.receive(); 
     System.out.println(message); 


    // There are many types of Message and TextMessage 
     // is just one of them. Producer sent us a TextMessage 
     // so we must cast to it to get access to its .getText() 
     // method. 
     if(message instanceof ObjectMessage){ 
      ObjectMessage objectMessage = (ObjectMessage)message; 
      System.out.println(" Received Message : '"+objectMessage.getObject()+" '"); 
     } 

     connection.close(); 
    } 

有隊列中的10條消息。
Rightnow,每條消費者消費1條消息。我想要10個消息被每個消費者消費。
我應該爲此做些什麼?

回答

1

隊列的性質是你有一個生產者和一個消費者。你應該爲此使用主題。

+0

你能否詳細說明你的答案? –

+1

您正在使用隊列。隊列有一個生產者和一個消費者。即使你有10個併發消費者,當其中一個消費者從隊列中取出消息時 - 這個消息將從隊列中刪除,而其他消費者不會得到它。所以隊列是一對一的。還有一些話題 - 這是一對一的話題。所有訂閱該主題的消費者都會收到生產者發送給該主題的消息。 – alexey28

相關問題