5

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

}

Chan Chun Weng
  • 876
  • 2
  • 16
  • 32

1 Answers1

1

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.

dk14
  • 22,206
  • 4
  • 51
  • 88
  • which framework do you use to connect with JMS-provider? – dk14 Sep 19 '14 at 05:05
  • I dont really know what framework is but I guess its java jdk? – Chan Chun Weng Sep 19 '14 at 05:15
  • So you're creating some javax.jms.Connection to connect to the broker. Just call connecion.setClientID(uuid) before the connection.start() – dk14 Sep 19 '14 at 05:18
  • connectionFactory.setClientID(uuid) – dk14 Sep 19 '14 at 05:22
  • it doesnt allow me to setClientID() at connectionFactory but at context – Chan Chun Weng Sep 19 '14 at 05:31
  • sorry, yes you have to use a context (and each receiver must have separate context) – dk14 Sep 19 '14 at 05:32
  • I have 2 durablesubscriber now and I add setClientId right after creating the context and I delete the clientId at my glassfish console(connectionfactory) then now everything worked – Chan Chun Weng Sep 19 '14 at 06:00
  • Based on my understanding, if glassfish console already set a clientId, the second consumer wont be able to use it when the first consumer is using the id. If I want more than 1 consumer subscribe and receives message from the topic, the glassfish console should not be set any clientId. Instead, I set it in all consumers's code uniquely then everything will work. Am I right?? – Chan Chun Weng Sep 19 '14 at 06:02
  • yes, you're right - second consumer will receive an exeption for duplicating clientID – dk14 Sep 19 '14 at 06:04
  • Thank you so much for helping me out!! but i can't vote the answer tho. xD – Chan Chun Weng Sep 19 '14 at 06:08
  • Just a step closer to my target :D – Chan Chun Weng Sep 19 '14 at 06:11
  • Some JMS brockers like Solace does not support multiple durable topic subscriptions. It sometimes depend on the brocker you connect. – Harsha Oct 11 '17 at 13:21
  • @Harsha yes, Solace might require more workarounds, like using “Topic subscription on Queues” which in its turn would require to administratively create a durable queue per each subscriber. Not as convenient (becoz of server-side configuration) but somewhat possible – dk14 Oct 11 '17 at 16:58