2014-09-19 129 views
3

我现在开始了JMS一周。我使用Netbeans,maven和glassfish创建了JMS。对一个主题的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); 
    } 
} 

}

回答

1

您可以为不同的消费者耐久设定不同clientID。 Jms-broker使用subscriptionName和clientId的组合来识别唯一的客户端(所以如果你的订户有唯一的clientID - 它可以接收自己的消息)。你可以在你的JmsContext中设置clientID。

+0

你能指导我吗? – 2014-09-19 05:00:23

+0

您使用哪种框架与JMS提供程序进行连接? – dk14 2014-09-19 05:05:52

+0

我真的不知道什么框架,但我猜它的java jdk? – 2014-09-19 05:15:09