Spring integration-sftp:出站通道适配器-动态sftpSessionFactory



如何基于文件使sftpSessionFactory动态?例如,如果文件名以A开头,则该文件必须放置在特定的SFTP位置,因为B、C、D类型的文件有自己的SFTP地址。所以我有4个不同的主机/用户/密码/端口,例如,我选择了4个,但我可能有20多种类型的文件。我无法在application.properties文件或integration.xml文件中硬编码host/user/password/port值。

在spring启动服务器时,我以Map的形式从配置服务器获取所有这些sftp配置详细信息。

Map<String, SftpValues> values = getAllSftpValues(); // this connects to a config server to fetch all type of sftp details.
SftpValues sftpValues = values.get("A");

CCD_ 1==>我应该能够将这些值动态设置到DefaultSftpSessionFactory中,并将sftpSessionFactory传递到每个文件的出站通道适配器。

<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="localhost"/>
<property name="user" value="user01"/>
<property name="password" value="abc123"/>
<property name="port" value="990"/>
</bean>
<int:channel id="sftpChannel"/>
<file:inbound-channel-adapter directory="#{T(System).getProperty('java.io.tmpdir')}" id="fileInbound"
channel="sftpChannel" filename-pattern="*.xml">
<int:poller fixed-rate="1000" max-messages-per-poll="100"/>
</file:inbound-channel-adapter>
<int-sftp:outbound-channel-adapter id="sftpOutboundAdapter" session-factory="sftpSessionFactory"
channel="sftpChannel" charset="UTF-8" remote-directory="/"
remote-file-separator="/"/>

动态设置到DefaultSftpSessionFactory

这是不对的。您不能更改会话工厂。您可以使用DelegatingSessionFactory方法。

您可以为某组委托配置它:public DelegatingSessionFactory(Map<Object, SessionFactory<F>> factories, SessionFactory<F> defaultFactory) {。然后,在发送到<int-sftp:outbound-channel-adapter>之前调用其public Message<?> setThreadKey(Message<?> message, Object key) {,然后在发送到该sftpOutboundAdapter之后或作为该publish-subscribe-channel的第二订户调用该public Message<?> clearThreadKey(Message<?> message) {

请参阅文档中的更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/ftp.html#ftp-dsf

最新更新