Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top