How to convert a JMS message received using Messag

2019-04-29 22:14发布

问题:

I am using:

  • Spring 3.1.1
  • ActiveMQ 5.6.0

I have two JMS apps:

  • App A uses JmsTemplate to send a domain object using jmsTemplate.convertAndSend(msg);
  • App B uses Message Listener and a Message Converter is registered

When the the received message is converted, null values are extracted.

I know that this must be fairly simple but I am getting null values and I haven't find an example to see what I am doing wrong.

Can some one explain please how this works?

Domain Object

public class MyDomainObj implements Serializable {

private static final long serialVersionUID = -5411260096045103654L;
private String name;
private String msg;

public MyDomainObj() {

}

public MyDomainObj(String name, String msg) {
    this.name = name;
    this.msg = msg;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

}

App A

@Component
public class MessageSender {

    @Autowired
    private JmsTemplate jmsTemplate;

    public MessageSender() {

    }

    public void sendMessage(MyDomainObj msg) {
        jmsTemplate.convertAndSend(msg);
    }
}

App B

@Component
public class MyReceiverConverter implements MessageConverter {

    @Override
    public Object fromMessage(Message msg) throws JMSException,
            MessageConversionException {

        MyDomainObj myDomainObj = new MyDomainObj(msg.getStringProperty("name"), msg.getStringProperty("msg"));


        return myDomainObj;
    }

    @Override
    public Message toMessage(Object msg, Session session) throws JMSException,
            MessageConversionException {

        ....
    }
}

回答1:

Solution is:

@Override
public Object fromMessage(Message msg) throws JMSException,
        MessageConversionException {

    MyDomainObj myDomainObj = (MyDomainObj)((ObjectMessage)msg).getObject();

    return myDomainObj;
}


标签: spring jms