ActiveMQ MapMessage接收带有空表的消息

时间:2013-10-23 09:51:08

标签: java jms activemq

我正在使用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-queue,transactionId = TX:ID:LT006632-52708-1382520875674-11:1:1,expiration = 0,timestamp = 1382520891291,arrival = 0,brokerInTime = 0,brokerOutTime = 0,correlationId = null,replyTo = null ,persistent = true,type = null,priority = 4,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,destination = queue:// my-queue,transactionId = TX:ID:LT006632-52708-1382520875674-11:1:1,expiration = 0,timestamp = 1382520891291,arrival = 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项目。 我想知道它的原因是什么以及如何解决这个问题。谢谢!

2 个答案:

答案 0 :(得分:3)

我们使用ActiveMQ 5.8.0,但是在接收到#n;' theTable'之后没有问题":记录消息条目。

但是在解析地图信息并再次记录之后,它就会打印出表格'条目!

似乎你不得不以某种方式触摸'消息或访问其内容以打印' theTable'条目。这至少对我们有用。

答案 1 :(得分:3)

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

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

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

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