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

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

Introduction

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

Prototype

Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException;

Source Link

Document

Convert a Java object to a Message.

Usage

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.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:amqp.spring.camel.component.SpringAMQPMessage.java

public Message toAMQPMessage(MessageConverter msgConverter) {
    MessageProperties properties = new MessageProperties();
    properties.setMessageId(this.getMessageId());

    Message amqpMessage;//from www  .  j a v  a  2 s  .  c om
    if (this.getBody() != null) {
        amqpMessage = msgConverter.toMessage(this.getBody(), properties);

        if (LOG.isTraceEnabled()) {
            String asText = new String(amqpMessage.getBody());
            LOG.trace("Translating To AMQP Message: " + asText);
        }
    } else {
        amqpMessage = new Message(new byte[] {}, properties);
    }

    return new HeadersPostProcessor(this).postProcessMessage(amqpMessage);
}

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

@Override
protected Message createMessage(Object object, MessageProperties messageProperties) {
    String contentType = messageProperties.getContentType();
    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.toMessage(object, messageProperties);
}

From source file:org.springframework.amqp.rabbit.AsyncRabbitTemplate.java

@Override
public <C> RabbitConverterFuture<C> convertSendAndReceive(String exchange, String routingKey, Object object,
        MessagePostProcessor messagePostProcessor) {
    CorrelationData correlationData = null;
    if (this.enableConfirms) {
        correlationData = new CorrelationData(null);
    }/*from  w  ww  .java 2s  .com*/
    CorrelationMessagePostProcessor<C> correlationPostProcessor = new CorrelationMessagePostProcessor<C>(
            messagePostProcessor, correlationData);
    if (this.container != null) {
        this.template.convertAndSend(exchange, routingKey, object, correlationPostProcessor, correlationData);
    } else {
        MessageConverter converter = this.template.getMessageConverter();
        if (converter == null) {
            throw new AmqpIllegalStateException(
                    "No 'messageConverter' specified. Check configuration of RabbitTemplate.");
        }
        Message message = converter.toMessage(object, new MessageProperties());
        correlationPostProcessor.postProcessMessage(message);
        ChannelHolder channelHolder = this.directReplyToContainer.getChannelHolder();
        correlationPostProcessor.getFuture().setChannelHolder(channelHolder);
        sendDirect(channelHolder.getChannel(), exchange, routingKey, message, correlationData);
    }
    RabbitConverterFuture<C> future = correlationPostProcessor.getFuture();
    future.startTimer();
    return future;
}

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

/**
 * Build a Rabbit message to be sent as response based on the given result object.
 * @param channel the Rabbit Channel to operate on
 * @param result the content of the message, as returned from the listener method
 * @return the Rabbit <code>Message</code> (never <code>null</code>)
 * @throws Exception if thrown by Rabbit API methods
 * @see #setMessageConverter// www . j  a  v  a  2s  . co m
 */
protected Message buildMessage(Channel channel, Object result) throws Exception {
    MessageConverter converter = getMessageConverter();
    if (converter != null && !(result instanceof Message)) {
        return converter.toMessage(result, new MessageProperties());
    } else {
        if (!(result instanceof Message)) {
            throw new MessageConversionException(
                    "No MessageConverter specified - cannot handle message [" + result + "]");
        }
        return (Message) result;
    }
}

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

/**
 * Build a Rabbit message to be sent as response based on the given result object.
 * @param session the Rabbit Channel to operate on
 * @param result the content of the message, as returned from the listener method
 * @return the Rabbit <code>Message</code> (never <code>null</code>)
 * @throws Exception if thrown by Rabbit API methods
 * @see #setMessageConverter//from   w w w.  java 2  s  . co m
 */
protected Message buildMessage(Channel session, Object result) throws Exception {
    MessageConverter converter = getMessageConverter();
    if (converter != null) {
        return converter.toMessage(result, new MessageProperties());
    } else {
        if (!(result instanceof Message)) {
            throw new MessageConversionException(
                    "No MessageConverter specified - cannot handle message [" + result + "]");
        }
        return (Message) result;
    }
}