Spring集成 - JMS输出适配器 - 使用消息转换器将对象作为String发送

时间:2017-01-09 19:09:37

标签: spring-integration

问题: JMS出站通道适配器上的消息转换器正在将对象I unMarshall作为字节而不是JMS TextMessage发送。是否可以将消息转换器设置为明确告诉它作为JMS TextMessage发送而不是字节?我想在分离器和出站之间放置一个变换器步骤,它只会返回一个字符串,但看起来效率很低。

参考文档: JMS Support

代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:jms="http://www.springframework.org/schema/integration/jms"
    xmlns:stream="http://www.springframework.org/schema/integration/stream"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/integration
            http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/integration/jms
            http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
            http://www.springframework.org/schema/integration/stream
            http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jee
            http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/oxm
            http://www.springframework.org/schema/oxm/spring-oxm.xsd">


        <int:chain id="fooChain" input-channel="requestChannel">    

            <!-- Performs processing action - Returns List<Foo> objects -->
            <int:service-activator ref="fooListener" method="processFoo"/>

            <int:splitter />

            <!-- Covert messages and send to mq queue -->       
            <jms:outbound-channel-adapter 
                id="fooRequestsChannel"
                destination="externalAppRequestQueue"
                message-converter="fooMessageConverter">
            </jms:outbound-channel-adapter>

        </int:chain> 


        <bean id="oxmFooMessageConverter" class="org.springframework.jms.support.converter.MarshallingMessageConverter">
              <property name="marshaller" ref="fooMarshaller" />
              <property name="unmarshaller" ref="fooMarshaller" />
               <!-- SOLUTION ADDED HERE: START --> 
              <property name="targetType" value="TEXT"/>
               <!-- SOLUTION ADDED HERE: END-->
        </bean>


         <oxm:jaxb2-marshaller id="fooMarshaller">  
            <oxm:class-to-be-bound name="com.test.foo" />
         </oxm:jaxb2-marshaller>

    </beans>

解决方案:

将以下属性添加到上面的oxmFooMessageConverter。 JMS消息将作为TEXT字符串发送到队列而不是字节。以上示例已更新。

<property name="targetType" value="TEXT"/>

1 个答案:

答案 0 :(得分:0)

M-M-M问题是什么?你已经这样做了:message-converter="fooMessageConverter"

你有什么问题?

如果您需要MarshallingMessageConverterString有一个选项:

/**
 * Specify whether {@link #toMessage(Object, Session)} should marshal to
 * a {@link BytesMessage} or a {@link TextMessage}.
 * <p>The default is {@link MessageType#BYTES}, i.e. this converter marshals
 * to a {@link BytesMessage}. Note that the default version of this converter
 * supports {@link MessageType#BYTES} and {@link MessageType#TEXT} only.
 * @see MessageType#BYTES
 * @see MessageType#TEXT
 */
public void setTargetType(MessageType targetType) {
    Assert.notNull(targetType, "MessageType must not be null");
    this.targetType = targetType;
}