java - ActiveMQ如何用一个消费者消费N条消息

标签 java 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 条消息。
目前,每个消费者消费了 1 条消息。我希望每个消费者消费 10 条消息。
我应该为此做哪些改变?

最佳答案

队列的本质是有一个生产者和一个消费者。您应该为此使用主题。

关于java - ActiveMQ如何用一个消费者消费N条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11135768/

相关文章:

java - 如何获取jdbcTemplate当前使用的Connection对象

java - Adobe Flash Builder 4.7 启动失败,错误代码为 "Failed to create the Java Virtual Machine"

java - 用于持久消息的 kaha 数据库消息存储

Activemq STOMP : detecting and clearing dead nondurable subscribers

jms - ActiveMQ 日志日志大小

java - 消费者池的自动缩放

我的 Java 服务器上的 Java 支持插件

ubuntu - ActiveMQs 不会在彼此之间复制消息

java - 如何描述一对多关系(JPA/HIbernate)

发送包含 JAXB 元素的对象时出现 java.io.NotSerializedException