[英]JMS Topic Publish/Subscriber
目前我已經開始使用ActiveMQ處理JMS主題。 我通過JAVA代碼創建了Publisher和Durable Subscribers (如下所述),我也收到了訂閱方的消息。
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在主題中主動查找消息?
如何從Topic獲得那些活躍的持久訂閱者列表?
我們是否有任何選項可以刪除主題中的已發布消息?
在這些情況下幫助我。
提前致謝。
在發布/訂閱消息傳遞模式中,發布者將不知道任何訂閱者。 發布者將消息發布到代理上托管的主題,然后代理會將這些消息分發給為該主題注冊的任何訂閱者。 如果主題沒有訂閱者,則將簡單地丟棄該消息。
JMS規范沒有定義任何可以獲取您要查找的詳細信息的API。 在您的情況下,此類API將是特定於JMS提供程序的Active MQ。 此鏈接可能很有用: http : //activemq.apache.org/advisory-message.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.