Results 1 to 4 of 4

Thread: Message Converters on OutboundChannelAdapter

  1. #1
    Join Date
    Apr 2009
    Posts
    17

    Question Message Converters on OutboundChannelAdapter

    I have a quick question on the message converter options on a JMS Outbound Channel Adapter.

    I'm putting a Message<Document> into the channel. Is there any way of specifying how you want the Document rendered, i.e. as a TextMessage or BytesMessage?

    As I understand it, the default SimpleMessageConverter does this:

    if payload is a String, create a TextMessage
    else if payload is a byte[] create a BytesMessage
    else if payload is a Map create a MapMessage
    else serialise as a ObjectMessage

    It looks like its sending an ObjectMessage, which is not what I want. Ideally, I'd like the message converter to allow me to specify the JMS Message type and render accordingly, or at least detect Document payloads (maybe default to TextMessage?).

    Is there any plan to add this functionality or am I resigned to implementing my own MessageConverter?

    (the other option is to do a transform from Message<Document> to Message<String> prior to the JMS send but I find that a bit clunky)

    Thanks

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    For now, you're pretty much limited to the two options you mentioned. Extending SimpleMessageConverter and adding a check for Document would be pretty trivial (then supply message-converter="yourInstance"). Adding a Transformer upstream so that the payload is a String might even be simpler, especially if you can just rely on a one-line SpEL expression to convert from the Document to a String.

    Your question does make me think of one possible feature request here (would have to be in a future Spring version). If you set a "targetType" in a MessageConverter impl that has awareness of a Spring ConversionService, it could attempt to convert to that type prior to the conversion (so, e.g. if targetType=String and you pass a Document, then a registered DocumentToStringConverter would be used and the ultimate JMS Message would be a TextMessage).

    Now that I described that, there is one other option on the Spring Integration side... You could add a <channel> that has a "datatype" attribute with a value of "java.lang.String". In fact that could be the channel that the JMS outbound-channel-adapter is using for its input. Then, if you pass anything to that channel that the integration ConversionService can convert to a String, then all Messages arriving at the outbound-channel-adapter would be Strings (or else conversion exceptions would be generated).

    Hopefully that last option sounds good to you. Let me know.

  3. #3
    Join Date
    Apr 2009
    Posts
    17

    Default

    Quote Originally Posted by Mark Fisher View Post
    Now that I described that, there is one other option on the Spring Integration side... You could add a <channel> that has a "datatype" attribute with a value of "java.lang.String". In fact that could be the channel that the JMS outbound-channel-adapter is using for its input. Then, if you pass anything to that channel that the integration ConversionService can convert to a String, then all Messages arriving at the outbound-channel-adapter would be Strings (or else conversion exceptions would be generated).

    Hopefully that last option sounds good to you. Let me know.
    This works well.

    I've implemented and defined two simple Converters, one DocumentToString and one DocumentToByteArray:



    <si:converter ref="documentToStringConverter"/>
    <bean id="documentToStringConverter" class="com.mypackage.converter.DocumentToStringCon verter"/>
    <si:converter ref="documentToByteArrayConverter"/>
    <bean id="documentToByteArrayConverter" class="com.mypackage.converter.DocumentToByteArray Converter"/>


    Now I can set the channel to either datatype=java.lang.String or datatype=byte[] and it works exactly as expected:

    <si:channel id="publish" datatype="java.lang.String"/>
    or
    <si:channel id="publish" datatype="byte[]"/>


    I think this is a lot neater solution that specifying transformers or putting a targetType onto the MessageConverter. Now my developers can choose which JMS Message type (Text or Byte) by simply defining as the datatype on the channel. Very nice.

  4. #4
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    Glad that you think that's the most elegant option. I think I agree

    Cheers,
    Mark

Posting Permissions

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