Example usage for org.springframework.amqp.support.converter MessageConverter fromMessage

List of usage examples for org.springframework.amqp.support.converter MessageConverter fromMessage

Introduction

In this page you can find the example usage for org.springframework.amqp.support.converter MessageConverter fromMessage.

Prototype

Object fromMessage(Message message) throws MessageConversionException;

Source Link

Document

Convert from a Message to a Java object.

Usage

From source file:amqp.spring.camel.component.SpringAMQPMessage.java

public static SpringAMQPMessage fromAMQPMessage(MessageConverter msgConverter,
        org.springframework.amqp.core.Message amqpMessage) {
    if (amqpMessage == null) {
        LOG.debug("Received NULL AMQP Message, returning null");
        return null;
    }//from  w  w w.j  a v  a 2 s  .  com

    SpringAMQPMessage message = new SpringAMQPMessage();

    //Restore the body based on the message converter provided
    if (amqpMessage.getBody() == null || amqpMessage.getBody().length == 0) {
        message.setBody(null);
    } else {
        if (LOG.isTraceEnabled()) {
            String asText = new String(amqpMessage.getBody());
            LOG.trace("Translating From AMQP Message: " + asText);
        }

        message.setBody(msgConverter.fromMessage(amqpMessage));
    }

    //Set & restore headers from AMQP
    message = SpringAMQPHeader.setBasicPropertiesToHeaders(message, amqpMessage);
    message = SpringAMQPHeader.copyHeaders(message, amqpMessage.getMessageProperties().getHeaders());

    return message;
}

From source file:amqp.spring.converter.StringConverterTest.java

@Test
public void testConversion() throws Exception {
    TestObject testObject = new TestObject();
    testObject.setValue("TESTING");

    MessageProperties messageProperties = new MessageProperties();

    MessageConverter converter = new StringConverter();
    Message amqpMessage = converter.toMessage(testObject, messageProperties);
    Object newObject = converter.fromMessage(amqpMessage);

    Assert.assertEquals("TESTING", newObject);
}

From source file:amqp.spring.converter.ContentTypeConverterFactory.java

@Override
public Object fromMessage(Message message) throws MessageConversionException {
    MessageProperties messageProperties = message.getMessageProperties();
    String contentType = messageProperties.getContentType();
    if (messageProperties == null)
        throw new MessageConversionException("Cannot decode a message with no properties!");

    MessageConverter converter = converters.get(contentType);
    if (converter == null) //Try to fall back
        converter = this.fallbackConverter;
    if (converter == null) //Can't even fall back, punt
        throw new MessageConversionException("Cannot find converter for content type of " + contentType);

    return converter.fromMessage(message);
}

From source file:amqp.spring.converter.XStreamConverterTest.java

@Test
public void testConversion() throws Exception {
    TestObject testObject = new TestObject();
    testObject.setValue("TESTING");

    MessageProperties messageProperties = new MessageProperties();

    MessageConverter converter = new XStreamConverter();
    ((XStreamConverter) converter).setEncoding("UTF-8");
    Message amqpMessage = converter.toMessage(testObject, messageProperties);
    Assert.assertEquals("{\"amqp.spring.converter.XStreamConverterTest_-TestObject\":{\"value\":\"TESTING\"}}",
            new String(amqpMessage.getBody()));

    Object newObject = converter.fromMessage(amqpMessage);
    Assert.assertEquals(testObject, newObject);
    Assert.assertEquals("UTF-8", ((XStreamConverter) converter).getEncoding());
}

From source file:org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.java

/**
 * Extract the message body from the given Rabbit message.
 * @param message the Rabbit <code>Message</code>
 * @return the content of the message, to be passed into the listener method as argument
 *///from   w ww . j ava  2s .c o  m
protected Object extractMessage(Message message) {
    MessageConverter converter = getMessageConverter();
    if (converter != null) {
        return converter.fromMessage(message);
    }
    return message;
}

From source file:org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.java

/**
 * Extract the message body from the given Rabbit message.
 * @param message the Rabbit <code>Message</code>
 * @return the content of the message, to be passed into the listener method as argument
 * @throws Exception if thrown by Rabbit API methods
 *///ww  w.  ja v a2s .c  om
protected Object extractMessage(Message message) throws Exception {
    MessageConverter converter = getMessageConverter();
    if (converter != null) {
        return converter.fromMessage(message);
    }
    return message;
}