dtutlamjasblef7982 2013-02-18 12:25
浏览 75
已采纳

JSON,Stomp和ActiveMQ

Ok, so here's the deal. I was reading this tutorial and the documentation from the ActiveMQ website.

On the documentation we have, under Message Transformations, this sentence:

The transformation message header on SEND and SUBSCRIBE messages could be used to instruct ActiveMQ to transform messages from text to the format of your desire. Currently, ActiveMQ comes with a transformer that can transform XML/JSON text to Java objects

and

ActiveMQ uses XStream for its transformation needs. Since it's the optional dependency you have to add it to broker's classpath by putting the appropriate JAR into the lib/ folder. Additionally, if you plan to use JSON transformations you have to add Jettison JSON parser to the classpath.

So I did this, I've annotated my pojo with XStreamAlias:

@XStreamAlias("ProvaPojo")
public class ProvaPojo implements Serializable {

    private static final long serialVersionUID = 1687248536279612587L;

    @XStreamAlias("fieldOne")
    private int fieldOne;

    @XStreamAlias("fieldTwo")
    private String fieldTwo;

    public int getFieldOne() {
    return fieldOne;
    }

    public void setFieldOne(int fieldOne) {
    this.fieldOne = fieldOne;
    }

    public String getFieldTwo() {
    return fieldTwo;
    }

    public void setFieldTwo(String fieldTwo) {
    this.fieldTwo = fieldTwo;
    }

}

And this is my PHP code (original example is here):

$body = array(
        'ProvaPojo' => array(
            array('fieldOne' =>  14),
            array('fieldTwo' => 'Stringa')
        )
);

$header = array();
$header['transformation'] = 'jms-map-json';
$mapMessage = new StompMessageMap($body, $header);
$con->send($queue, $mapMessage);

And this is my listener

@Override
protected void _onMessage(Message message) {
if (!(message instanceof ObjectMessage))
    throw new IllegalStateException();
ObjectMessage objectMessage = (ObjectMessage) message;
try {
    if (!(objectMessage.getObject() instanceof BlacklisterRecipientBean))
    throw new IllegalArgumentException(
            "The message content is not an instance of "
                    + BlacklisterRecipientBean.class.getSimpleName());
    BlacklisterRecipientBean recipient = (BlacklisterRecipientBean) objectMessage
        .getObject();
    log.info("_onMessage(Message)");
    IncomingBlacklistAddJMSTrigger trigger = new IncomingBlacklistAddJMSTrigger();
    trigger.setArgs(recipient);
    notifyObservers(trigger);
} catch (JMSException e) {
    log.error("_onMessage(Message) - exception ignored", e);
}

}

But I'm always getting an ActiveMQTextMessage. What am I missing?

  • 写回答

1条回答 默认 最新

  • doushi1510 2013-06-06 08:56
    关注

    So apperently it was a problem of my activeMQ version. Once switched to 5.8.0 everything worked like a charm.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求会做聚类,TCN的朋友有偿线上指导。以下是目前遇到的问题
  • ¥100 无网格伽辽金方法研究裂纹扩展的程序
  • ¥15 错误于library(org.Hs.eg.db): 不存在叫‘org.Hs.eg.db’这个名称的程序包,如何解决?
  • ¥60 求一个图片处理程序,要求将图像大小跟现实生活中的大小按比例联系起来的
  • ¥50 求一位精通京东相关开发的专家
  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活