Spring 使用sftp出站网关下载文件后,使用下载的文件名调用bean方法

Spring 使用sftp出站网关下载文件后,使用下载的文件名调用bean方法,spring,spring-integration,spring-el,spring-integration-sftp,Spring,Spring Integration,Spring El,Spring Integration Sftp,我正在使用int sftp:出站网关下载远程文件。文件下载正在运行。我需要在文件下载后调用另一个方法,以获得成功和失败。在该方法中,我需要状态success或failure以及请求下载的文件名。然后,通过这种方法,我将根据状态启动一个下载后流程,例如:将文件移动到不同的位置、通知用户、发送电子邮件等。 <int:gateway id="downloadGateway" service-interface="com.rizwan.test.sftp_outbound_gateway.Dow

我正在使用int sftp:出站网关下载远程文件。文件下载正在运行。我需要在文件下载后调用另一个方法,以获得成功和失败。在该方法中,我需要状态success或failure以及请求下载的文件名。然后,通过这种方法,我将根据状态启动一个下载后流程,例如:将文件移动到不同的位置、通知用户、发送电子邮件等。

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

<bean id="myAfterAdvice" class="org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor">
    <constructor-arg>
        <bean class="com.rizwan.test.sftp_outbound_gateway.MyAfterReturningAdvice">
        </bean>
    </constructor-arg>
</bean>

<int-sftp:outbound-gateway id="gatewayGet"
    local-directory="C:\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">
    <int-sftp:request-handler-advice-chain>
        <ref bean="myAfterAdvice" />
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>


public class MyAfterReturningAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        //update db, send email, notify user.
    }
}
我使用AfterReturningAdviceInterceptor调用MyAfterReturningAdvice中定义的方法,该方法实现AfterReturningAdvice接口。使用此方法,我可以启动下载后流程。它确实执行,并且我确实在GenericMessage的负载中获取文件名。我的问题是,我们是否有更好的方法来实现这个流程。

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

<bean id="myAfterAdvice" class="org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor">
    <constructor-arg>
        <bean class="com.rizwan.test.sftp_outbound_gateway.MyAfterReturningAdvice">
        </bean>
    </constructor-arg>
</bean>

<int-sftp:outbound-gateway id="gatewayGet"
    local-directory="C:\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">
    <int-sftp:request-handler-advice-chain>
        <ref bean="myAfterAdvice" />
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>


public class MyAfterReturningAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        //update db, send email, notify user.
    }
}
我尝试使用ExpressionEvaluationRequestHandlerAdvice的onSuccessExpression,但从中我无法调用其他方法。我所能做的就是操纵inputMessageGenericMessage实例

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

<bean id="myAfterAdvice" class="org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor">
    <constructor-arg>
        <bean class="com.rizwan.test.sftp_outbound_gateway.MyAfterReturningAdvice">
        </bean>
    </constructor-arg>
</bean>

<int-sftp:outbound-gateway id="gatewayGet"
    local-directory="C:\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">
    <int-sftp:request-handler-advice-chain>
        <ref bean="myAfterAdvice" />
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>


public class MyAfterReturningAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        //update db, send email, notify user.
    }
}
在未来的sprint中,我将比较下载文件的校验和与预期校验和,如果校验和不匹配,则重新下载文件固定次数。一旦校验和匹配,我需要再次调用后下载流。如果下载失败,即使在最后重试,然后我需要调用另一个流发送电子邮件,更新数据库,通知用户失败,等等。 我问这个问题只是为了确保我当前的实现符合总体需求。

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

<bean id="myAfterAdvice" class="org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor">
    <constructor-arg>
        <bean class="com.rizwan.test.sftp_outbound_gateway.MyAfterReturningAdvice">
        </bean>
    </constructor-arg>
</bean>

<int-sftp:outbound-gateway id="gatewayGet"
    local-directory="C:\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">
    <int-sftp:request-handler-advice-chain>
        <ref bean="myAfterAdvice" />
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>


public class MyAfterReturningAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        //update db, send email, notify user.
    }
}
ExpressionEvaluationRequestHandlerAdvice.onSuccessExpression是您的最佳选择。它的EvaluationContext是BeanFactory感知的,因此您完全可以从该表达式调用任何bean。作为根对象提供的消息是获取有关下载文件的信息的最佳候选

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

<bean id="myAfterAdvice" class="org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor">
    <constructor-arg>
        <bean class="com.rizwan.test.sftp_outbound_gateway.MyAfterReturningAdvice">
        </bean>
    </constructor-arg>
</bean>

<int-sftp:outbound-gateway id="gatewayGet"
    local-directory="C:\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">
    <int-sftp:request-handler-advice-chain>
        <ref bean="myAfterAdvice" />
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>


public class MyAfterReturningAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        //update db, send email, notify user.
    }
}
所以,这就是你可以做的:

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

<bean id="myAfterAdvice" class="org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor">
    <constructor-arg>
        <bean class="com.rizwan.test.sftp_outbound_gateway.MyAfterReturningAdvice">
        </bean>
    </constructor-arg>
</bean>

<int-sftp:outbound-gateway id="gatewayGet"
    local-directory="C:\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">
    <int-sftp:request-handler-advice-chain>
        <ref bean="myAfterAdvice" />
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>


public class MyAfterReturningAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        //update db, send email, notify user.
    }
}
<bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
    <property name="onSuccessExpressionString" value="@myBean.myMethod(#root)"/>
</bean>
您可以对onFailureExpression执行相同的操作

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

<bean id="myAfterAdvice" class="org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor">
    <constructor-arg>
        <bean class="com.rizwan.test.sftp_outbound_gateway.MyAfterReturningAdvice">
        </bean>
    </constructor-arg>
</bean>

<int-sftp:outbound-gateway id="gatewayGet"
    local-directory="C:\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">
    <int-sftp:request-handler-advice-chain>
        <ref bean="myAfterAdvice" />
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>


public class MyAfterReturningAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        //update db, send email, notify user.
    }
}

另一方面,您甚至不需要担心从表达式访问bean。ExpressionEvaluationRequestHandlerAdvice具有successChannel和failureChannel选项。因此,带有结果的消息可以发送到那里,您的bean中的一些人可以在该频道上处理消息。

谢谢Artem,我在onSuccessExpression中使用的正确语法中被困了很长时间。非常感谢。
<int:gateway id="downloadGateway" service-interface="com.rizwan.test.sftp_outbound_gateway.DownloadRemoteFileGateway"
    default-request-channel="toGet"/>

<bean id="myAfterAdvice" class="org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor">
    <constructor-arg>
        <bean class="com.rizwan.test.sftp_outbound_gateway.MyAfterReturningAdvice">
        </bean>
    </constructor-arg>
</bean>

<int-sftp:outbound-gateway id="gatewayGet"
    local-directory="C:\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">
    <int-sftp:request-handler-advice-chain>
        <ref bean="myAfterAdvice" />
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-gateway>


public class MyAfterReturningAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        //update db, send email, notify user.
    }
}