1

我正在使用 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 项。我想知道它的原因是什么以及如何解决这个问题。谢谢!

4

2 回答 2

3

我们使用 ActiveMQ 5.8.0,但有相同的“问题”:接收后的日志消息不会打印出“theTable”条目。

但是在解析地图消息并再次记录后,它确实打印了“theTable”条目!

似乎您必须以某种方式“触摸”消息或访问其内容才能打印“theTable”条目。这至少对我们有用。

于 2013-10-23T12:30:45.127 回答
3

上面的答案是正确的。我将详细说明为什么它会以这种方式工作。

MapMessage 映射 (theMap) 的内容被编组为更小的二进制形式进行传输,甚至可以根据 OpenWire 协议的设置进行压缩。地图值仅在访问地图元素时才被延迟解组,这就是日志语句不在接收方显示它们的原因。这样做有很多原因。

在代理方面,您真的不想花很多时间记录所有地图元素,只是为了有一些简单的日志显示通过代理的消息,而且您不想经常打包和解包这个地图可以增加一些开销。

同样的事情也适用于客户端,其中一些消息可以根据消息属性等被忽略,因此您不希望无缘无故地增加解组地图的开销。

于 2013-10-23T20:58:37.487 回答