Ami*_*Ami 4 java activemq-classic jms
我已经下载了activemq版本5.8.0并编写了用于创建队列的示例程序.我成功地向队列发送了一条示例消息.
之后,我尝试将消息ID设置为特定消息.消息ID可用于检索特定消息.我试图使用设置消息ID message.setJMSMessageID("1234");.
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();
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.在我使用getJMSMessageID()它打印时设置消息ID后打印随机值.
如何在队列消息中添加消息ID?
根据规范,用户无法设置JMSMessageID值.它是特定于JMS提供者的.
When a message is sent, JMSMessageID is ignored. When the send method returns
it contains a provider-assigned value.
Run Code Online (Sandbox Code Playgroud)