我已经下载了ActiveMQ 5.8.0版本,并编写了用于创建队列的示例程序。我成功地向队列发送了一条样本消息。
之后,我尝试将消息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();
}
但它没有起作用。在设置消息ID后,当我使用getJMSMessageID()
打印它时,它会打印出随机值。
如何将消息ID添加到队列消息中?
message.setStringProperty(id)
,然后使用消息选择器来检索消息。 - Aniket Thakur