1

在我们的应用程序中,有大量文件从远程机器下载到本地机器(运行代码的服务器)。我们选择使用 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() 中获取下载的文件名。

4

1 回答 1

1

我认为你需要的东西肯定可以用 AOP Advices 来完成。更重要的是它们的链,确实RequestHandlerRetryAdvice应该首先开始重试循环。我建议将下一个建议ExpressionEvaluatingRequestHandlerAdvice与其onSuccessExpressionpropagateOnSuccessEvaluationFailures = true组合一起使用。这样,您可以在 中执行校验和验证,onSuccessExpression如果不匹配,则抛出异常。该异常将被堆栈中的前一个捕获RequestHandlerRetryAdvice并执行重试逻辑。

有关此事,请参阅他们的 JavaDocs 和参考手册。

此外,我们还有一些示例项目可以更好地理解事物。

于 2018-01-31T17:37:31.977 回答