如何为int-sftp:outbound-gateway动态创建会话工厂



我正在尝试使用int-sftp:outbound-gateway实现安全的文件传输。我当前的配置如下所示:

<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
  <constructor-arg name="isSharedSession" value="false" />

<int:chain id="sftpGetRequestEnricherChain" input-channel="sftpGetRequestEnricherChannel" output-channel="sftpGetRequestChannel">
      <int-xml:xpath-header-enricher id="sftpHostEnricher">
         <int-xml:header name="sftp_host" xpath-expression="/fileGetRequest/property[@name='host']/@val" />
         <int-xml:header name="sftp_port" xpath-expression="/fileGetRequest/property[@name='port']/@val" />
         <int-xml:header name="sftp_user" xpath-expression="/fileGetRequest/property[@name='user']/@val" />
         <int-xml:header name="source_directory" xpath-expression="/fileGetRequest/property[@name='sourceDirectory']/@val" />
         <int-xml:header name="source_file_name" xpath-expression="/fileGetRequest/property[@name='sourceFileName']/@val" />
      </int-xml:xpath-header-enricher>
      <int:service-activator ref="refreshSftpSessionFactoryTask" />
   </int:chain>
<bean name="refreshSftpSessionFactoryTask" class="com.mycorp.filetransfer.RefreshSftpSessionFactoryTask" />
<int-sftp:outbound-gateway id="sftpGetOutboundGateway" 
      local-directory="${sftp.local.directory.get}"
      session-factory="sftpSessionFactory"
      request-channel="sftpGetRequestChannel"
      reply-channel="logChannel"
      command="get"
      expression="headers['source_directory'] + '/' + headers['source_file_name']" />

如上所示,每条消息包含主机、端口、用户和文件路径的详细信息。

在RefreshSftpSessionFactoryTask中,我正在设置主机、端口和用户详细信息,并获取密码并在sftpSessionFactory bean中进行设置,如下所示:

AbstractApplicationContext context = new ClassPathXmlApplicationContext(SFTP_CONFIG_FILE, this.getClass());
DefaultSftpSessionFactory sftpSessionFactory = context.getBean("sftpSessionFactory", DefaultSftpSessionFactory.class);
sftpSessionFactory.setHost(host);
sftpSessionFactory.setPort(port);
sftpSessionFactory.setUser(user);
sftpSessionFactory.setPassword(fetchPassword());
context.close();

但是,在文件传输过程中,我得到一个异常,说"主机在sftpSessionFactory中不能为空"。

应该做些什么,以便正确设置DefaultSftpSessionFactory中的属性,并通过int-sftp:outbound-gateway查看这些属性?

你每次都在创建一个新的会话工厂,并完全破坏它;您没有更新现有的。

不是

AbstractApplicationContext context = new ClassPathXmlApplicationContext(SFTP_CONFIG_FILE, this.getClass());
DefaultSftpSessionFactory sftpSessionFactory = context.getBean("sftpSessionFactory", DefaultSftpSessionFactory.class);

只需将setSessionFactory(DefaultSftpSessionFactory factory)添加到RefreshSftpSessionFactoryTask

然后

<bean name="refreshSftpSessionFactoryTask" class="com.mycorp.filetransfer.RefreshSftpSessionFactoryTask">
   <property name="sessionFactory" ref="sftpSessionFactory" />
</bean>

将会话工厂存储在字段中并使用…

this.sftpSessionFactory.setHost(host);
this.sftpSessionFactory.setPort(port);
this.sftpSessionFactory.setUser(user);
this.sftpSessionFactory.setPassword(fetchPassword());

…并且您将更新适配器使用的会话工厂。

但是,对于线程,您需要非常小心。

最新更新