bind a custom object to a JMS MapMessage

后端 未结 2 471
深忆病人
深忆病人 2021-01-14 10:48

Is there a standard way for me to add my own custom object to a Map and then have it properly marshalled in a MapMessage? Currently I get the Invalid Object Type message.

相关标签:
2条回答
  • 2021-01-14 11:13

    A JMS map message's map only supports primitives and strings (and their arrays) as values. From the javadoc:

    The names are String objects, and the values are primitive data types in the Java programming language.

    You would be better off using an ObjectMessage and write your serialized objects to a map and then send the map as the payload of the ObjectMessage. That way, you can still have the name/value map access style but without the limitation of types.

    0 讨论(0)
  • 2021-01-14 11:21

    With JsmTemplate in Spring (2.5, 3.1), if you want to send a Map through jmsTemplate.convertAndSend() where the Map contains a non-primitive object, you could cast the Map as Serializable and call send(MessageCreator) . This way:

    //...some previous code here
    
    final Map myMap = createYourSerializableMapHere();
    
    jmsTemplate.send(new MessageCreator(){
    
        @Override
        public Message createMessage(Session session) throws JMSException {
            ObjectMessage objectMessage = session.createObjectMessage((Serializable) myMap);
    
            return objectMessage;
        }
    });
    

    This way jmsTemplate will work with the Map as Serializable and will send an ObjectMessage over the wire.

    Note that the listener consuming messages will have to be able to read the ObjectMessage and then cast it as Map again. Be aware that you have to had the corresponding classes at both sides of the wire, and of course, the objects inside the Map has to be Serializable!

    0 讨论(0)
提交回复
热议问题