此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1! |
上下文持有者建议
从 6.1 版开始,ContextHolderRequestHandlerAdvice
已被引入。
此通知从请求消息中获取一些值,并将其存储在上下文持有者中。
当在目标上完成执行时,该值从上下文中清晰可见MessageHandler
.
思考此建议的最佳方式类似于编程流程,我们将一些值存储到ThreadLocal
,从目标调用获取对它的访问权限,然后清理ThreadLocal
执行后。
这ContextHolderRequestHandlerAdvice
需要以下构造函数参数:Function<Message<?>, Object>
作为价值提供者,Consumer<Object>
作为上下文设置回调和Runnable
作为上下文清理钩子。
下面是一个示例,如何ContextHolderRequestHandlerAdvice
可与o.s.i.file.remote.session.DelegatingSessionFactory
:
@Bean
DelegatingSessionFactory<?> dsf(SessionFactory<?> one, SessionFactory<?> two) {
return new DelegatingSessionFactory<>(Map.of("one", one, "two", two), null);
}
@Bean
ContextHolderRequestHandlerAdvice contextHolderRequestHandlerAdvice(DelegatingSessionFactory<String> dsf) {
return new ContextHolderRequestHandlerAdvice(message -> message.getHeaders().get("FACTORY_KEY"),
dsf::setThreadKey, dsf::clearThreadKey);
}
@ServiceActivator(inputChannel = "in", adviceChain = "contextHolderRequestHandlerAdvice")
FtpOutboundGateway ftpOutboundGateway(DelegatingSessionFactory<?> sessionFactory) {
return new FtpOutboundGateway(sessionFactory, "ls", "payload");
}
向in
带有FACTORY_KEY
header 设置为one
或two
.
这ContextHolderRequestHandlerAdvice
将该标头中的值设置为DelegatingSessionFactory
通过其setThreadKey
.
然后当FtpOutboundGateway
执行ls
命令适当的委托SessionFactory
从DelegatingSessionFactory
根据其ThreadLocal
.
当结果从FtpOutboundGateway
一个ThreadLocal
值DelegatingSessionFactory
根据clearThreadKey()
调用ContextHolderRequestHandlerAdvice
.
有关详细信息,请参阅委托会话工厂。