JMS multiple long term 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 reliable consumer and I wanted to add another durable consumer to the same theme (not a queue). Can this be done? because I want all consumers to consume all the message sent by the producer, regardless of whether the users are offline or not.
Any advice? Thanks to
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);
}
}
}
+3
source to share
1 answer
You can set different clientIDs for different trusted consumers. The JMS broker uses a combination of subscriptionName and clientId to identify a unique client (so if your subscriber has a unique client ID, they can receive their own messages). You can set client id in your JmsContext.
+1
source to share