Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMS multiple durable subscription to one topic

I started JMS for a week now. I created JMS using Netbeans,maven and glassfish.

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);
    }
}

}

like image 391
Chan Chun Weng Avatar asked Nov 10 '22 01:11

Chan Chun Weng


1 Answers

You can set different clientID for different durable consumers. 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). You can set clientID in your JmsContext.

like image 72
dk14 Avatar answered Nov 14 '22 23:11

dk14