Spring集成Header Enricher在链中工作

时间:2017-07-31 02:42:30

标签: java spring spring-integration

这是我的春季整合案例:

1)ftp适配器下载pdf文件

2)pdf2TextTransformer使用pdfbox

将pdf转换为文本

3)pdfText2CsvTransformer将文本转换为csv

我在链中添加了一个更丰富的,但是,标题似乎没有传播到myErrorChannel,任何人都可以告诉我为什么?

这是我的调试日志,头文件_originalFile没有传播到myErrorChannel

][Headers={file_originalFile=D:\projects\DMTP\ftp\local2\6000047256 - Copy - Copy.pdf, file_name=6000047256 - Copy - Copy.pdf, id=a091fe5e-83f3-e48c-4be5-927849dbf31a, timestamp=1501472718172}]
11:45:18.180 DEBUG [task-scheduler-2][org.springframework.integration.channel.PublishSubscribeChannel] preSend on channel 'errorChannel', message: [Payload MessageTransformationException content=org.springframework.integration.transformer.MessageTransformationException: org.springframework.messaging.MessageHandlingException: java.lang.StringIndexOutOfBoundsException: String index out of range: -3][Headers={id=251c77f0-30e8-e18c-6980-ebfb289f79a4, timestamp=1501472718180}]
11:45:18.180 DEBUG [task-scheduler-2][org.springframework.integration.handler.ServiceActivatingHandler] ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@56eacc77] received message: [Payload MessageTransformationException content=org.springframework.integration.transformer.MessageTransformationException: org.springframework.messaging.MessageHandlingException: java.lang.StringIndexOutOfBoundsException: String index out of range: -3][Headers={id=251c77f0-30e8-e18c-6980-ebfb289f79a4, timestamp=1501472718180}]
file_originalFile:null
MessageHeaders: {id=251c77f0-30e8-e18c-6980-ebfb289f79a4, timestamp=1501472718180}
MessagePayload: org.springframework.integration.transformer.MessageTransformationException: org.springframework.messaging.MessageHandlingException: java.lang.StringIndexOutOfBoundsException: String index out of range: -3
11:45:18.182 DEBUG [task-scheduler-2][org.springframework.integration.handler.ServiceActivatingHandler] handler 'ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@56eacc77]' produced no reply for request Message: [Payload MessageTransformationException content=org.springframework.integration.transformer.MessageTransformationException: org.springframework.messaging.MessageHandlingException: java.lang.StringIndexOutOfBoundsException: String index out of range: -3][Headers={id=251c77f0-30e8-e18c-6980-ebfb289f79a4, timestamp=1501472718180}]

这是我的配置:         

<bean id="ftpClientFactory" class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
    <property name="host" value="${host}"/>
    <property name="port" value="${availableServerPort}"/>
    <property name="username" value="${userid}"/>
    <property name="password" value="${password}"/>
</bean>

<bean id="cachingSessionFactory" class="org.springframework.integration.file.remote.session.CachingSessionFactory">
    <constructor-arg ref="ftpClientFactory"/>
</bean>
<int:channel id="ftpChannelIn">
    <int:queue capacity="1"/>
</int:channel>

<int-ftp:inbound-channel-adapter id="ftpInbound"
                                 channel="ftpChannelIn"
                                 session-factory="cachingSessionFactory"
                                 filename-pattern="*.pdf"
                                 auto-create-local-directory="true"
                                 delete-remote-files="true"
                                 remote-directory="/in2"
                                 local-directory="D:/projects/DMTP/ftp/local2">
    <!--<int:poller fixed-rate="1000"/>-->
    <int:poller fixed-delay="1"/>
</int-ftp:inbound-channel-adapter>

<int:chain input-channel="ftpChannelIn">
    <int:header-enricher>
        <int:header name="foo" value="bar"/>
    </int:header-enricher>
    <file:file-to-bytes-transformer/>
    <int:transformer ref="pdf2TextTransformer"/>
    <int:transformer ref="pdfText2CsvTransformer"/>
    <int-ftp:outbound-channel-adapter
            remote-directory="/out"
            session-factory="cachingSessionFactory"
            remote-filename-generator="filenameGenerator"/>
</int:chain>


<int:channel id="ftpChannelOut">
</int:channel>
<bean id="filenameGenerator" class="file.DatedDirectoryFactory "/>
<int:poller default="true" fixed-delay="50"/>
<int:channel id="myErrorChannel"/>
<int:service-activator input-channel="errorChannel" ref="errorLogger"/>

2 个答案:

答案 0 :(得分:2)

ErrorMessage是在Poller中构建的,它基于异常:

try {
    getMessagingTemplate().send(errorChannel, getErrorMessageStrategy().buildErrorMessage(t, null));
            sent = true;
}

没有关于原始标题的任何信息。

您的标题,特别是<int:header name="foo" value="bar"/>显示在下游requestMessage。通常,当Integration Component中发生异常时,会抛出MessagingException。 在您的情况下,它是MessageTransformationException。这种异常具有failedMessage属性。这正是你的requestMessage错误中的“有罪”。而且,如您所知,已经在这里您可以访问标题 - MessagingException.getFailedMessage().getHeaders()get("foo")。当然,file_originalFile也可以在这里找到。

答案 1 :(得分:1)

如果您的变形金刚返回Message<?>,那么他们负责复制标题;框架假定。如果您的变换器是POJO,并且仅返回转换后的有效负载(推荐的编程模型),则框架将负责传播标头。

启用调试日志记录以检查消息流;如果你仍然认为它没有工作&#34;,请更详细地解释并编辑问题以显示日志。

顺便说一下,&#34;没有工作&#34;不是对问题的充分描述。