Java 如何使用ActiveMQ中的一个使用者使用N条消息
以下是我的消费者:Java 如何使用ActiveMQ中的一个使用者使用N条消息,java,activemq,Java,Activemq,以下是我的消费者: public static void main(String[] args) throws JMSException { // Getting JMS connection from the server ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); Connection connection = co
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条消息。现在,每个消费者消费一条消息。我希望每个消费者都能使用10条信息。
对此,我应该做哪些更改?队列的本质是您有一个生产者和一个消费者。您应该使用此主题。请详细说明您的答案好吗?您使用的是队列。队列有一个生产者和一个消费者。即使当其中一个使用者从队列中获取消息时,您有10个并发使用者-此消息将从队列中删除,而其他使用者无法获取它。所以队列是一对一的。还有一些话题——一对多。订阅该主题的所有使用者都将收到制作人发送到此主题的消息。