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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记