ActiveMQ JMSXGroupId属性无效

时间:2019-05-22 12:29:25

标签: java activemq

我想通过使用JMSXGroupId属性对消息进行分组。但是消费者无需分组即可获取所有队列消息

我有一个本地ActiveMQ实例(版本5.15.9,Windows 10,Java 8)。
我已经编写了DoSend Java类来发送文本消息,并编写了DoRead Java类来检索文本消息

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class DoSend {
    public static void main(String[] args) throws JMSException {
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");

        Connection connection = null;
        try {
            connection = factory.createConnection("admin", "admin");
            connection.start();

            Session session = null;
            try {
                session = connection.createSession(true, Session.SESSION_TRANSACTED);

                MessageProducer producer = null;
                try {
                    Destination destination = session.createQueue("TEST");
                    producer = session.createProducer(destination);

                    int index = 0;
                    while (true) {
                        index += 1;

                        TextMessage message = session.createTextMessage(String.valueOf(index));

                        message.setStringProperty("JMSXGroupID", String.valueOf(index % 3));
                        producer.send(destination, message);
                        session.commit();

                        Thread.sleep(1000);
                    }
                } finally {
                    if (producer != null) {
                        producer.close();
                    }
                }

            } catch (Throwable th) {
                th.printStackTrace();

                if (session != null) {
                    session.rollback();
                }

                throw new RuntimeException(th);
            } finally {
                if (session != null) {
                    session.close();
                }
            }
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
}

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class DoRead {
    public static void main(String[] args) throws JMSException {
        final ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");

        System.out.println("START " + Thread.currentThread());
        try {
            Connection connection = null;
            try {
                connection = factory.createConnection("admin", "admin");
                connection.start();

                Session session = null;
                try {
                    session = connection.createSession(true, Session.SESSION_TRANSACTED);

                    Destination destination = session.createQueue("TEST");
                    MessageConsumer consumer = null;
                    try {
                        consumer = session.createConsumer(destination);

                        TextMessage message;
                        while ((message = (TextMessage) consumer.receive(10000)) != null) {
                            System.out.println(Thread.currentThread() + ": message.index = " + message.getText());
                            System.out.println(Thread.currentThread() + ": message.JMSXGroupID = " + message.getStringProperty("JMSXGroupID"));
                            session.commit();
                        }
                    } finally {
                        if (consumer != null) {
                            consumer.close();
                        }
                    }
                } finally {
                    if (session != null) {
                        session.close();
                    }
                }
            } finally {
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            System.out.println("FINISH " + Thread.currentThread());
        }
    }
}

我希望任何使用者都可以检索具有相同JMSXGroupId的消息,但现在第一个使用者检索所有消息,而其他使用者则什么也不检索。

1 个答案:

答案 0 :(得分:0)

此标头的目的是将单个接收者与组绑定在一起。因此,只有一个接收者将获得所有具有相同组ID的消息。通常,您还希望添加JMSXGroupSeq标头以指示组中的消息ID。 此标头的一般用途是在一个节点(单个接收器)上执行一些聚合逻辑。

有关详细信息,请参见ActiveMq page,或参见JBOSS page