我已經下載了activemq版本5.8.0並編寫了用於創建隊列的示例程序。我成功發送了一條示例消息到隊列。如何在Activemq jms隊列中設置消息ID?
之後,我嘗試將消息ID設置爲特定消息。消息ID可用於檢索特定消息。我試圖使用message.setJMSMessageID("1234");
設置消息ID。
public static void messagestoQueueu(){
// JMS messages are sent and received using a Session. We will
// create here a non-transactional session object. If you want
// to use transactions you should set the first parameter to 'true'
Session session;
try {
// Getting JMS connection from the server and starting it
ConnectionFactory connectionFactory =
new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// Destination represents here our queue 'TESTQUEUE' on the
// JMS server. You don't have to do anything special on the
// server to create it, it will be created automatically.
Destination destination = session.createQueue("test");
// MessageProducer is used for sending messages (as opposed
// to MessageConsumer which is used for receiving them)
MessageProducer producer = session.createProducer(destination);
// We will send a small text message saying 'Hello' in Japanese
//BytesMessage byteMessage = session.create;
TextMessage message = session.createTextMessage();
message.setJMSType("sample");
message.setJMSMessageID("1234");
message.setText("sample");
message.setJMSCorrelationID("choole");
message.setJMSMessageID("choo01");
message.setJMSReplyTo(destination);
producer.send(queue, message);
// Here we are sending the message!
producer.send(message);
System.out.println(message.getJMSMessageID()+" "+message.getJMSCorrelationID());
//System.out.println("Sent message '" + message.getText() + "'");
connection.close();
producer.close();
session.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但它不工作。當我使用getJMSMessageID()
打印時設置了消息ID後,它會打印隨機值。
如何將消息ID添加到隊列消息?
Thanks.Aniket.Is可以設置基於JMS提供者的消息ID?我想給消息id.Is是否有其他可能?你能解釋更多嗎? – Ami
不,客戶端應用程序無法設置JMSMessageID。因此,如果您想共同關聯消息,那麼您可以使用JMSCorrelationID,這是客戶端應用程序特定的。 –
是的。我已經使用了correlationID而不是messageID.But問題是相關性ID不是唯一的。您可以爲單個相關性指定多個消息。現在它創建duplicate.how我可以找到並檢索特定的消息? – Ami