简体   繁体   English

JMS多个持久订阅一个主题

[英]JMS multiple durable subscription to one topic

I started JMS for a week now. 我现在开始使用JMS一周了。 I created JMS using Netbeans,maven and glassfish. 我使用Netbeans,maven和glassfish创建了JMS。

I have one producer and one durable consumer and I wanted to add another durable consumer to the same topic (not queue). 我有一个生产者一个持久消费者 ,我想在同一主题 (而不是队列)中添加另一个持久消费者。 Is it possible to do so? 有可能这样做吗? because I want all the consumers consume all the message being sent by the producer whether the consumers are offline or not. 因为我希望所有消费者都消费生产者发送的所有消息,无论消费者是否离线。

Any advice? 有什么建议? Thanks 谢谢

public class DurableReceive {

@Resource(lookup = "jms/myDurableConnectionFactory")
private static ConnectionFactory connectionFactory;

@Resource(lookup = "jms/myNewTopic")
private static Topic topic;

public static void main(String[] args) {
    Destination dest = (Destination) topic;
    JMSConsumer consumer;
    boolean messageReceived = false;
    String message;
    System.out.println("Waiting for messages...");

    try (JMSContext context = connectionFactory.createContext();) {
        consumer = context.createDurableConsumer(topic, "Subscriber1");
        while (!messageReceived) {
            message = consumer.receiveBody(String.class);
            if (message != null) {
                System.out.print("Received the following message: " + message);
                System.out.println("(Received date: " + new Date() + ")\n");
            } else {
                messageReceived = true;
            }
        }
    } catch (JMSRuntimeException e) {
        System.err.println("@#$%RuntimeException occurred: " + e.toString());
        System.exit(1);
    }
}

} }

You can set different clientID for different durable consumers. 您可以为不同的持久消费者设置不同的clientID Jms-broker uses combination of subscriptionName and clientId to identify the unique client (so if your subscriber have unique clientID - it can receive own messages). Jms-broker使用subscriptionName和clientId的组合来标识唯一的客户端(因此,如果您的订户具有唯一的clientID - 它可以接收自己的消息)。 You can set clientID in your JmsContext. 您可以在JmsContext中设置clientID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM