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

时间:2018-01-31 17:24:10

标签: java spring spring-integration spring-integration-sftp

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

1 个答案:

答案 0 :(得分:1)

我认为您的需求绝对可以通过AOP建议来完成。更多关于它们的链,确实RequestHandlerRetryAdvice应该首先开始重试循环。我建议将下一条建议用作ExpressionEvaluatingRequestHandlerAdvice及其onSuccessExpressionpropagateOnSuccessEvaluationFailures = true组合的onSuccessExpression。这样您就可以在RequestHandlerRetryAdvice中执行校验和验证,如果它不匹配,则抛出异常。该异常将被前一个堆栈RequestSecurityToken rst = new RequestSecurityToken { RequestType = RequestTypes.Issue, OnBehalfOf(new SecurityToken()), //what do I put here? AppliesTo = new EndpointReference(relyingPartyId), Issuer = new EndpointReference(issuerId), TokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0" }; 捕获,并且将执行重试逻辑。

关于此事,请参阅他们的JavaDocs和Reference Manual

我们还有一些Sample project可以更好地了解事情。