我现在开始 JMS 一个星期了。我使用 Netbeans、maven 和 glassfish 创建了 JMS。
我有一个生产者和一个持久消费者,我想在同一主题中添加另一个持久消费者(而不是队列)。有可能这样做吗?因为我希望所有消费者都消费生产者发送的所有消息,无论消费者是否离线。
有什么建议吗?谢谢
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);
}
}
}