弹簧集成 - JMS 输出适配器 - 使用消息转换器将对象作为字符串发送



>问题:JMS 出站通道适配器上的消息转换器正在将我取消马歇尔的对象作为字节而不是 JMS 文本消息发送。是否可以将消息转换器设置为显式告诉它作为 JMS 文本消息而不是字节发送?我想在拆分器和出站之间放置一个转换器步骤,它只会返回一个字符串,但似乎效率低下。

参考文档:联合监测系统支持

法典:

<?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"/>

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

你有什么问题?

如果您想StringMarshallingMessageConverter有一个选项:

/**
 * 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;
}

最新更新