我正在使用 ActiveMQ 5.9.0 发送和接收 JMS。首先,我在队列中发送 MapMessage:
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageProducer producer = session.createProducer(destination);
MapMessage message = session.createMapMessage();
message.setDouble(key, value);
producer.send(message);
session.commit();
session.close();
connection.close();
System.out.println(message);
它打印出来的是:
ActiveMQMapMessage {commandId = 0, responseRequired = false, messageId = ID:LT006632-52708-1382520875674-11:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = null, destination = queue://my-队列,transactionId = TX:ID:LT006632-52708-1382520875674-11:1:1,过期 = 0,时间戳 = 1382520891291,到达 = 0,brokerInTime = 0,brokerOutTime = 0,correlationId = null,replyTo = null,持久 = true,type = null,priority = 4,groupID = null,groupSequence = 0,targetConsumerId = null,compressed = false,userID = null,content = org.apache.activemq.util.ByteSequence@33cf4,marshalledProperties = null,dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = false, readOnlyBody = false, droppable = false,jmsXGroupFirstForConsumer = false} ActiveMQMapMessage{ theTable = {entry 6=922.0} }
在消息中使用正确的 theTable。
它以这种方式在另一个项目中接收消息:
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
Connection connection = connectionFactory.createConnection();
connection.start();
final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
MapMessage message = (MapMessage) msg;
// TODO something....
try {
System.out.println(message.getJMSType());
session.commit();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread.sleep(30000);
session.close();
connection.close();
但是在收到消息时,它会打印出来:
ActiveMQMapMessage {commandId = 6, responseRequired = false, messageId = ID:LT006632-52708-1382520875674-11:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:LT006632-52708-1382520875674-11: 1:1:1,目的地 = queue://my-queue,transactionId = TX:ID:LT006632-52708-1382520875674-11:1:1,过期 = 0,时间戳 = 1382520891291,到达 = 0,brokerInTime = 1382520891291, brokerOutTime = 1382520891298,correlationId = null,replyTo = null,persistent = true,type = null,priority = 4,groupID = null,groupSequence = 0,targetConsumerId = null,compressed = false,userID = null,content = org.apache。 activemq.util.ByteSequence@186db54, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false,jmsXGroupFirstForConsumer = false} ActiveMQMapMessage{ theTable = {} }
带有一个空的 theTable 项。我想知道它的原因是什么以及如何解决这个问题。谢谢!