JMS主题发布/订阅者
问题描述:
目前我已开始使用JMS主题与ActiveMQ。我有创建发布者和持久订阅者通过JAVA代码(下面提到),我也收到订阅方的消息。JMS主题发布/订阅者
Publisher.Java
public static void createConnectionAndSendMessage(String ipAddress)
{
try
{
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://"+ipAddress+":61617");
Connection connection = factory.createConnection();
connection.start();
Session topicSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = topicSession.createTopic("Test-Topic");
MessageProducer producer = topicSession.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
ObjectMessage message = topicSession.createObjectMessage();
TopicTO topicTO = new TopicTO();
topicTO.setId(i);
topicTO.setName("Sample");
message.setStringProperty("s_id", "Sample");
message.setObject((Serializable) topicTO);
producer.send(message);
System.out.println("message sent successfully");
}
}
catch(JMSException e)
{
System.out.println("error :" + e);
}
}
Subscriber.java
public void createConnectionAndReceiveMessage(String clientId, String ipAddress)
{
try
{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://"+ipAddress+":61617");
Connection connection = connectionFactory.createConnection();
connection.setClientID(clientId);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("Test-Topic");
String selector = "s_id = 'Sample'";
System.out.println("selector : '"+selector+"'....");
TopicSubscriber consumer = session.createDurableSubscriber(topic, "Sub1", selector, true);
consumer.setMessageListener(new TopicMessageListener());
}
catch(Exception e)
{
System.out.println("error :" + e);
}
}
我有些疑惑的主题那些如下,
如何可能我检查多少订户积极寻找主题中使用Java JMS的消息?
如何从主题中获得那些活动的持久订阅者列表?
我们是否有任何选项可以删除主题中发布的消息?
在这些背景下帮助我。
在此先感谢。
答
在发布/订阅消息传递模式中,发布者将不知道任何订阅者。发布者将发布消息给代理托管的主题,代理将轮流将这些消息分发给为该主题注册的任何订阅者。如果某个主题没有订阅者,那么该消息将被简单地丢弃。
JMS规范没有定义任何可以获取您要查找的细节的API。这些API将是JMS提供程序特定的,Active MQ是您的情况。此链接可能有用:http://activemq.apache.org/advisory-message.html