[英]Spring Integration - including payload in errors
我试图将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:表达式求值失败:'无法传输'+ 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 :(位置0):在类型为“ org.springframework.integration.message.ErrorMessage”的对象上找不到字段或属性“ inputMessage”
在org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:246)处
查看spring文档,我可以看到org.springframework.integration.message.ErrorMessage
确实接受标头,但不知何故没有设置它。
有什么建议吗?
为了成功,将特殊的AdviceMessage
发送到该通道; 它具有inputMessage
属性(有效负载是表达式求值的结果)。
对于错误情况,将正常的ErrorMessage
发送到通道。 它的有效负载是具有正常属性的MessagingException
: cause
(发生的异常)和failedMessage
。
实际的异常是MessageHandlingExpressionEvaluatingAdviceException
,它具有一个额外的属性evaluationResult
其中包含表达式求值的结果。
因此,您需要针对失败案例的不同表达式payload.failedMessage.payload.absolutePath
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.