gpt4 book ai didi

java - 使用Spring Integration for Inbound Adapter时如何检查SFTP连接是否成功

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:20 28 4
gpt4 key购买 nike

我正在使用 Spring Integration 将 xml 文件从 SFTP 传输到本地。我使用了Spring社区提供的代码。

主类文件

public class SFTPMain {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("/SftpInboundReceiveSample-context.xml", SftpInboundReceiveSample.class);
SftpInboundReceiveSample sftp = context.getBean("sftpService", SftpInboundReceiveSample.class);
sftp.setContext(context);
try {
sftp.receiveFile();
} catch (Exception e) {
System.out.println(e);
} finally {
context.close();
}
}
}

服务等级

@Component("sftpService")
public class SftpInboundReceiveSample {

public ConfigurableApplicationContext getContext() {
return context;
}
public void setContext(ConfigurableApplicationContext context) {
this.context = context;
}

ConfigurableApplicationContext context;

public void receiveFile(){
try {
PollableChannel localFileChannel = context.getBean("receiveChannel", PollableChannel.class);
SourcePollingChannelAdapter adapter = context.getBean(SourcePollingChannelAdapter.class);
adapter.start();
System.out.println("Adapter started...");
Message<?> received = localFileChannel.receive();
System.out.println("Received first file message: " + received);
MessageHeaders header= received.getHeaders();
header.getTimestamp();

}
finally {
}
}
}

XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/sftp
http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd">

<import resource="SftpSampleCommon.xml" />

<bean id="sftpSessionFactory"
class="org.springframework.integration.file.remote.session.CachingSessionFactory">
<constructor-arg ref="defaultSftpSessionFactory" />
</bean>

<bean id="defaultSftpSessionFactory"
class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${sftp.host}"/>
<property name="password" value="${sftp.password}"/>
<property name="port" value="${sftp.port}"/>
<property name="user" value="${sftp.username}"/>
</bean>

<int-sftp:inbound-channel-adapter id="sftpInbondAdapter"
session-factory="sftpSessionFactory" channel="receiveChannel"
filename-pattern="*" remote-directory="/loblawln"
local-directory="D:/local-dir"
auto-create-local-directory="true" temporary-file-suffix=".writing"
delete-remote-files="false">
<int:poller fixed-rate="1000" max-messages-per-poll="1" />
</int-sftp:inbound-channel-adapter>

<int:channel id="receiveChannel">
<int:queue />
</int:channel>
</beans>

还想知道轮询是否是强制性的,因为我将通过 CRON 作业使用适配器,每天只对 SFTP 服务器执行一次 ping 操作。

最佳答案

首先<poller>支持cron以及。所以,我不认为有理由引入一些额外的CRON job .

从另一面来看<poller>error-channel选项,也是。这样您就可以捕获轮询流程中的任何问题,包括 SFTP 连接错误。

关于java - 使用Spring Integration for Inbound Adapter时如何检查SFTP连接是否成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408245/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com