5
votes

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

}

1

1 Answers

1
votes

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.