bind a custom object to a JMS MapMessage

2019-02-18 22:31发布

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. I noticed that WebSphere has a solution but I am looking for something that is not bound to a particular AS, if there is no such method, maybe something supported by JBoss would work.

How to do it in WebSphere: http://publib.boulder.ibm.com/infocenter/dmndhelp/v6rxmx/index.jsp?topic=/com.ibm.websphere.wesb.doc/ref/rwesb_jmscustombindings.html

标签: java jboss jms
2条回答
【Aperson】
2楼-- · 2019-02-18 23:28

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.

查看更多
▲ chillily
3楼-- · 2019-02-18 23:33

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!

查看更多
登录 后发表回答