此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1spring-doc.cadn.net.cn

上下文持有者建议

从 6.1 版开始,ContextHolderRequestHandlerAdvice已被引入。 此通知从请求消息中获取一些值,并将其存储在上下文持有者中。 当在目标上完成执行时,该值从上下文中清晰可见MessageHandler. 思考此建议的最佳方式类似于编程流程,我们将一些值存储到ThreadLocal,从目标调用获取对它的访问权限,然后清理ThreadLocal执行后。 这ContextHolderRequestHandlerAdvice需要以下构造函数参数:Function<Message<?>, Object>作为价值提供者,Consumer<Object>作为上下文集回调和Runnable作为上下文清理钩子。spring-doc.cadn.net.cn

下面是一个示例,如何ContextHolderRequestHandlerAdvice可与o.s.i.file.remote.session.DelegatingSessionFactory:spring-doc.cadn.net.cn

@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_KEYheader 设置为onetwo. 这ContextHolderRequestHandlerAdvice将该标头中的值设置为DelegatingSessionFactory通过其setThreadKey. 然后当FtpOutboundGateway执行ls命令适当的委托SessionFactoryDelegatingSessionFactory根据其ThreadLocal. 当结果从FtpOutboundGateway一个ThreadLocalDelegatingSessionFactory根据clearThreadKey()调用ContextHolderRequestHandlerAdvice. 有关详细信息,请参阅委托会话工厂。spring-doc.cadn.net.cn