对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

从版本 6.1 开始,引入了 。 此建议从请求消息中获取一些值,并将其存储在上下文持有者中。 当在目标上完成执行时,该值从上下文中是明确的。 考虑此建议的最佳方式类似于编程流程,我们将一些值存储到 ,从目标调用访问它,然后在执行后清理。 需要以下构造函数参数:a 作为值提供程序、作为上下文集回调和作为上下文清理钩子。ContextHolderRequestHandlerAdviceMessageHandlerThreadLocalThreadLocalContextHolderRequestHandlerAdviceFunction<Message<?>, Object>Consumer<Object>RunnableSpring中文文档

下面是一个示例,如何将 a 与 a 结合使用:ContextHolderRequestHandlerAdviceo.s.i.file.remote.session.DelegatingSessionFactorySpring中文文档

@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");
}

只需将标头设置为 或 向频道发送消息就足够了。 将该标头中的值设置为 via its . 然后,在执行命令时,根据其 . 当 生成结果时,将根据 对 的调用清除 中的值。 有关更多信息,请参见委派会话工厂inFACTORY_KEYonetwoContextHolderRequestHandlerAdviceDelegatingSessionFactorysetThreadKeyFtpOutboundGatewaylsSessionFactoryDelegatingSessionFactoryThreadLocalFtpOutboundGatewayThreadLocalDelegatingSessionFactoryclearThreadKey()ContextHolderRequestHandlerAdviceSpring中文文档