Results 1 to 2 of 2

Thread: Receiving Bytes but expected String - Clarification Please.

  1. #1

    Default Receiving Bytes but expected String - Clarification Please.

    Hello,

    Java 1.7u2
    Spring Integration 2.1.0.RC2
    Spring AMQP 1.0

    I'm wiring up a AMQP inbound-channel-adaptor to a cachingConnectionFactory, like so:

    Code:
        <rabbit:connection-factory id="cachingConnectionFactory" host="${rabbitmq.host}" username="${rabbitmq.username}" password="${rabbitmq.password}" />
    Code:
        <int-amqp:inbound-channel-adapter channel="source" queue-names="messageQueue" connection-factory="cachingConnectionFactory" />
    
        <int:channel id="source"/>
    I'm sending (using the RabbitMQ web management interface) a message that is an XML fragment to the messageQueue. Spring Integration is throwing an exception since the Body of the Message is being set (by RabbitMQ?) to be a Byte[].

    Reading the Spring AMQP reference manual:

    The default implementation of the MessageConverter strategy is called SimpleMessageConverter. This is the converter that will be used by an instance of RabbitTemplate if you do not explicitly configure an alternative. It handles text-based content, serialized Java objects, and simple byte arrays.
    I understand that if I set the content_type to be TEXT/PLAIN then a conversion from the Byte[] to a String will occur.

    Since this messageQueue will *always* contain XML, is there a way I can just tell the ChannelAdaptor to *always* convert the body to a String, even *if* the content_type is not set?

    Thank you.

    -=bootlaces=-

  2. #2

    Default

    Hi,

    Okay, I wrote a bit of code to do the work for me. Please find it below :-)

    Code:
    import java.io.UnsupportedEncodingException;
    
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.support.converter.MessageConversionException;
    import org.springframework.amqp.support.converter.SimpleMessageConverter;
    import org.springframework.stereotype.Component;
    
    /**
     * Convert ByteArrays to String.
     */
    @Component("byteArrayToStringConverter")
    public class ByteArrayToStringConverter extends SimpleMessageConverter {
    
        private static final String DEFAULT_CHARSET = "UTF-8";
    
        @Override
        public Object fromMessage(final Message message) throws MessageConversionException {
            final Object content = super.fromMessage(message);
            try {
                if (content instanceof byte[]) {
                    return new String((byte[])content, DEFAULT_CHARSET);
                }
            } catch (final UnsupportedEncodingException e) {
                throw new MessageConversionException("failed to convert text-based Message content", e);
            }
            return content;
        }
    
    }
    Then in my XML configuration of the inbound channel adaptor:

    Code:
    <int-amqp:inbound-channel-adapter channel="source" queue-names="messageQueue" connection-factory="cachingConnectionFactory" message-converter="byteArrayToStringConverter" />
    -=bootlaces=-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •