2012-06-21 33 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

相关问题