我已下载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();
}
但它不起作用。在使用getJMSMessageID()
打印消息ID时设置消息ID后,它会打印随机值。
如何在队列消息中添加消息ID?
答案 0 :(得分:6)
根据规范,用户无法设置JMSMessageID值。它是特定于JMS提供者。
When a message is sent, JMSMessageID is ignored. When the send method returns
it contains a provider-assigned value.
答案 1 :(得分:-1)
您可以为每条消息设置参数:
message.setStringProperty("property_name",property_val);
这样你就可以在生产者和消费者之间传递参数。