如何将使用消息侦听器收到的JMS消息转换为域对象 [英] How to convert a JMS message received using Message Listener to domain object

查看:265
本文介绍了如何将使用消息侦听器收到的JMS消息转换为域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:

  • 春季3.1.1
  • ActiveMQ 5.6.0

我有两个JMS应用程序:

I have two JMS apps:

  • 应用程序A使用JmsTemplate通过jmsTemplate.convertAndSend(msg);发送域对象;
  • 应用B使用消息监听器并注册了消息转换器

转换收到的消息后,将提取空值.

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?

域对象

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;
}

}

应用A

@Component
public class MessageSender {

    @Autowired
    private JmsTemplate jmsTemplate;

    public MessageSender() {

    }

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

应用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 {

        ....
    }
}

推荐答案

解决方案是:

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

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

    return myDomainObj;
}

这篇关于如何将使用消息侦听器收到的JMS消息转换为域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆