Spring Integration - 包括错误中的有效负载

时间:2015-05-26 10:33:58

标签: spring spring-integration

我试图将inputMessage有效负载包含在错误中,但它似乎永远不会被传递,并且丰富似乎也无法正常工作。

我有一个目录轮询服务:

<file:inbound-channel-adapter
        id="incomingFiles"
        auto-startup="true"
        auto-create-directory="false"
        channel="myFiles"
        filename-pattern="${directory.local.pattern}"
        directory="file:${directory.local}">
    <int:poller id="poller" fixed-delay="${directory.local.poll}"/>
</file:inbound-channel-adapter>

然后通过SFTP上传这些文件:

<sftp:outbound-channel-adapter
        id="sftpOutboundAdapter"
        channel="myFiles"
        charset="UTF-8"
        remote-directory="${directory.remote}"
        session-factory="sftpSessionFactory">
    <sftp:request-handler-advice-chain>
        <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
            <property name="onSuccessExpression" value="payload.delete()"/>
            <property name="successChannel" ref="successChannel"/>
            <property name="onFailureExpression" value="payload.renameTo(payload.absolutePath + '.error')"/>
            <property name="failureChannel" ref="failChannel" />
        </bean>
    </sftp:request-handler-advice-chain>
</sftp:outbound-channel-adapter>

在成功频道上,我记录并发送并通过successChannel发送,其他服务选择并发送电子邮件,这100%工作:

    <!-- Log Channel -->
    <int:logging-channel-adapter id="logChannel" level="INFO"/>

    <!-- Success Endpoint -->
    <int:channel id="successChannel">
        <int:interceptors>
            <int:wire-tap channel="successLogChannel"/>
        </int:interceptors>
    </int:channel>

    <!-- Success Logging -->
    <int:channel id="successLogChannel"/>
    <int:transformer
            input-channel="successLogChannel"
            output-channel="logChannel"
            expression="'Successfully transferred ' + inputMessage.payload.absolutePath"/>

    <!-- Send success email -->
    <int:chain input-channel="successChannel">
        <int:header-enricher>
            <int:header name="to" value="${success.email.to}"/>
            <int:header name="from" value="${success.email.from}" />
            <int:header name="subject" value="${success.email.subject}"/>
            <int:header name="filename" expression="inputMessage.payload.absolutePath"/>
        </int:header-enricher>
        <int:service-activator ref="Email" method="sendSuccessMail"/>
    </int:chain>
  

org.springframework.integration.handler.LoggingHandler - 成功   转移/tmp/test-file.dat

现在当我为错误尝试完全相同的事情时,表达式inputMessage.payload.filename无法评估。

    <!-- Fail Endpoint -->
    <int:channel id="failChannel">
        <int:interceptors>
            <int:wire-tap channel="failLogChannel"/>
        </int:interceptors>
    </int:channel>

    <!-- Fail Logging -->
    <int:channel id="failLogChannel"/>
    <int:transformer
            input-channel="failLogChannel"
            output-channel="logChannel"
            expression="'Failed to transfer ' + inputMessage.payload.absolutePath"/>

    <!-- Send fail email -->
    <int:chain input-channel="failChannel">
        <int:header-enricher>
            <int:header name="to" value="${fail.email.to}"/>
            <int:header name="from" value="${fail.email.from}" />
            <int:header name="subject" value="${fail.email.subject}"/>
            <int:header name="filename" expression="inputMessage.payload.absolutePath"/>
        </int:header-enricher>
        <int:service-activator ref="Email" method="sendFailMail"/>
    </int:chain>
  

错误org.springframework.integration.handler.LoggingHandler -   org.springframework.integration.transformer.MessageTransformationException:   org.springframework.integration.MessageHandlingException:Expression   评估失败:&#39;无法转移&#39; +   inputMessage.payload.absolutePath

我尝试添加一个标头扩充程序,但是当出现错误时,似乎有效负载永远不会超出SFTP出站通道:

    <!-- Fail Endpoint -->
    <int:channel id="failChannel"/>
    <int:chain input-channel="failChannel" output-channel="failChannelEnriched">
        <int:header-enricher>
            <int:header name="to" value="${fail.email.to}"/>
            <int:header name="from" value="${fail.email.from}"/>
            <int:header name="subject" value="${fail.email.subject}"/>
            <int:header name="filename" expression="inputMessage.payload.absolutePath"/>
        </int:header-enricher>
    </int:chain>


    <int:channel id="failChannelEnriched">
        <int:interceptors>
            <int:wire-tap channel="failLogChannel"/>
        </int:interceptors>
    </int:channel>

    <!-- Fail Logging -->
    <int:channel id="failLogChannel"/>
    <int:transformer
            input-channel="failLogChannel"
            output-channel="logChannel"
            expression="'Failed to transfer ' + headers['filename']"/>

    <!-- Send fail email -->
    <int:chain input-channel="failChannelEnriched">
        <int:service-activator ref="Email" method="sendFailMail"/>
    </int:chain>
  

错误org.springframework.integration.handler.LoggingHandler -   org.springframework.integration.transformer.MessageTransformationException:   org.springframework.integration.MessagingException:失败了   转换邮件标题

     

...

     

引起:   org.springframework.expression.spel.SpelEvaluationException:   EL1008E:(pos 0):字段或属性&#39; inputMessage&#39;无法找到   类型&#39; org.springframework.integration.message.ErrorMessage&#39;

     

at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:246)

查看spring文档,我可以看到org.springframework.integration.message.ErrorMessage确实接受了标题,但不知怎的,它只是没有被设置。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

为了成功,会向频道发送特殊AdviceMessage;这具有inputMessage属性(有效负载是表达式求值的结果)。

对于错误情况,正常ErrorMessage被发送到频道;它的有效载荷是 具有正常属性的MessagingExceptioncause(发生的例外情况)和failedMessage

实际例外是MessageHandlingExpressionEvaluatingAdviceException,其中包含表达式评估结果的附加属性evaluationResult

因此,您需要为失败案例payload.failedMessage.payload.absolutePath使用不同的表达式。