JMS出站通道适配器转换为byte []

时间:2014-09-15 19:00:10

标签: spring spring-integration

我使用下面的配置发送带有Object有效负载的JMS消息(比如说HelloWorld对象),但是当我使用Message driven adapater接收消息时,有效负载被转换为byte []。

有没有办法防止这种情况发生?我看到出站适配器最终调用SimpleMessageConverter方法`createMessageForSerializable - > session.createObjectMessage(对象)

<si:chain input-channel="errorChannelIn"> 
  <si:router 
       expression="headers.jms_redelivered.equals(T(java.lang.Boolean).FALSE) ? 'errorQueueChannel' : 'channel2Name' " 
       apply-sequence="true"> 
     <si:mapping value="errorQueueChannel" channel="errorChannel"/> 
     <si:mapping value="channel2Name" channel="channel2"/> 
  </si:router> 
</si:chain> 

<si-jms:outbound-channel-adapter id="errorQueueMessageSender"
                                 channel="errorChannel"
                                 connection-factory="connectionFactory" session-transacted="true" destination="errorQueue"
        />

但是,当我使用JmsTemplate发送消息时,它完美地发送消息作为messageObject,消息驱动的适配器将Object作为有效负载。下面的代码段。

知道我哪里错了。

jmsTemplate.send(new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createObjectMessage(messageObject);
        }
    });

1 个答案:

答案 0 :(得分:0)

我想你的意思是extract-payload="false"上的<si-jms:outbound-channel-adapter>

在这种情况下,<si-jms:message-driven-channel-adapter>个代表会以最终的org.springframework.jms.support.converter.SimpleMessageConverter工作:

else if (message instanceof ObjectMessage) {
    return extractSerializableFromMessage((ObjectMessage) message);
}

然后您反序列化Message<?>

<强>更新

对不起,现在还不清楚是怎么回事。刚刚使用我们的Samples进行了测试。在那里,您可以找到服务器/客户端配置:outboundChannelAdapter.xmlinboundChannelAdapter.xml。我在GatewayDemoTest添加了一个测试用例:

private final static String[] configFilesChannelAdapterDemo = {
        "/META-INF/spring/integration/common.xml",
        "/META-INF/spring/integration/inboundChannelAdapter.xml",
        "/META-INF/spring/integration/outboundChannelAdapter.xml"
};

@Test
public void testAdapterDemo() throws InterruptedException {

    System.setProperty("spring.profiles.active", "testCase");

    final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(configFilesChannelAdapterDemo);

    final MessageChannel stdinToJmsoutChannel = applicationContext.getBean("stdinToJmsoutChannel", MessageChannel.class);

    Foo foo = new Foo("bar");

    stdinToJmsoutChannel.send(MessageBuilder.withPayload(foo).build());

    final QueueChannel queueChannel = applicationContext.getBean("queueChannel", QueueChannel.class);

    Message<?> reply = queueChannel.receive(20000);
    Assert.assertNotNull(reply);
    Object out = reply.getPayload();

    Assert.assertThat(out, Matchers.instanceOf(Foo.class));

    Assert.assertEquals(foo, out);

    applicationContext.close();
}

public static class Foo implements Serializable {

    private final String bar;

    public Foo(String bar) {
        this.bar = bar;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Foo foo = (Foo) o;

        return !(bar != null ? !bar.equals(foo.bar) : foo.bar != null);

    }

    @Override
    public int hashCode() {
        return bar != null ? bar.hashCode() : 0;
    }

}

如您所见,我将一些Serializable对象添加到队列中,<jms:message-driven-channel-adapter>为我收到完全相同的反序列化对象。

也许您的问题出现在可能使用<jms:outbound-channel-adapter>的{​​{1}}之前的某个地方?

您是否介意在<payload-serializing-transformer>之前重新检查payload,或者甚至将断点添加到<jms:message-driven-channel-adapter>