gpt4 book ai didi

java - 如何验证使用 Spring SFTP 下载的文件的校验和

转载 作者:行者123 更新时间:2023-11-30 06:19:10 26 4
gpt4 key购买 nike

在我们的应用程序中,有大量文件从远程计算机下载到本地计算机(运行代码的服务器)。我们选择使用 Spring SFTP 进行下载。使用下面的代码我可以将文件从远程计算机下载到本地。

<?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/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd">

<import resource="SftpSampleCommon.xml"/>

<int:gateway id="downloadGateway" service-interface="com.rizwan.test.sftp_outbound_gateway.DownloadRemoteFileGateway"
default-request-channel="toGet"/>

<int-sftp:outbound-gateway id="gatewayGet"
local-directory="C:\Users\503017993\Perforce\rizwan.shaikh1_G7LGTPC2E_7419\NGI\DEV\Jetstream_Branches\C360_Falcon2_1_Main\sftp-outbound-gateway"
session-factory="sftpSessionFactory"
request-channel="toGet"
remote-directory="/si.sftp.sample"
command="get"
command-options="-P"
expression="payload"
auto-create-local-directory="true"
session-callback="downloadCallback">
<int-sftp:request-handler-advice-chain>
<int:retry-advice />
</int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>
<!-- reply-channel="toRm" -->

<int:gateway id="deleteGateway" service-interface="com.rizwan.test.sftp_outbound_gateway.DeleteRemoteFileGateway"
default-request-channel="toRm"/>

<int-sftp:outbound-gateway id="gatewayRM"
session-factory="sftpSessionFactory"
expression="payload"
request-channel="toRm"
command="rm">
<int-sftp:request-handler-advice-chain>
<int:retry-advice />
</int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>

</beans>

Java代码

ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:/META-INF/spring-context.xml");
DownloadRemoteFileGateway downloadGateway = ctx.getBean(DownloadRemoteFileGateway.class);
DeleteRemoteFileGateway deleteGateway = ctx.getBean(DeleteRemoteFileGateway.class);
String downloadedFilePath = downloadGateway.downloadRemoteFile("si.sftp.sample/2ftptest");
System.out.println("downloadedFilePath: " + downloadedFilePath);

Boolean status = deleteGateway.deleteRemoteFile("si.sftp.sample/2ftptest");
System.out.println("deletion status: " + status);

以上代码按预期工作。它下载远程文件,然后将其删除。我们已经拥有下载文件的校验和。该校验和是根据远程文件计算的。是否可以建立一种机制来计算文件下载后的校验和。我们需要能够将预期的校验和与收到的文件的校验和进行比较,如果不匹配,则重试固定次数。

我想知道是否可以像下面一样使用 RetryTemplate 。这是未经测试的伪代码。

class Test {

@Autowired
DownloadRemoteFileGateway downloadGateway;

public void init() {
RetryTemplate template = new RetryTemplate();
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
backOffPolicy.setInitialInterval(Long.parseLong(initialInterval));
backOffPolicy.setMaxInterval(Long.parseLong(initialInterval));
template.setRetryPolicy(new SimpleRetryPolicy(Integer.parseInt(maxAttempts), exceptionMap));
template.setBackOffPolicy(backOffPolicy);
}

void foo(){
Object result = template.execute(new RetryCallback() {

@Override
public String doWithRetry(RetryContext retryContext) throws Exception {
//Calculate received file checksum and compare with expected checksum
if(mismatch) {
downloadGateway.downloadRemoteFile(remoteFileName);
}

}, new RecoveryCallback() {
//same logic
});
}//foo
}//Test

我的问题是如何让我的方法 foo() 在文件下载完成后执行。是否也可以在 foo() 中获取下载的文件名。

最佳答案

我认为您所需要的绝对可以通过 AOP Advices 来完成。更重要的是,对于它们的链,实际上 RequestHandlerRetryAdvice 应该首先启动重试循环。我建议将下一个建议用作 ExpressionEvaluatingRequestHandlerAdvice 及其 onSuccessExpressionpropagateOnSuccessEvaluationFailures = true 组合。这样,您就可以在 onSuccessExpression 中执行校验和验证,如果不匹配,则抛出异常。该异常将被堆栈中的前一个 RequestHandlerRetryAdvice 捕获,并执行重试逻辑。

查看他们的 JavaDocs 和 Reference Manual关于此事。

我们还有一些Sample project更好地理解事物。

关于java - 如何验证使用 Spring SFTP 下载的文件的校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48548004/

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