简体   繁体   English

Spring SFTP出站网关:如何在Java Config中关闭GET后的会话?

[英]Spring SFTP Outbound Gateway: How to close the session after GET in Java Config?

I have written a piece of code which uses Spring SFTP Outbound gateway and performs a GET operation. 我编写了一段使用Spring SFTP出站网关并执行GET操作的代码。 The whole configuration is in JAVA (no XML). 整个配置是在JAVA(没有XML)。 I have made a caching session factory which allows a maximum of 10 sessions. 我已经建立了一个缓存会话工厂,最多允许10个会话。 Due to which after multiple GET request when it exceeds 10, GET request start failing. 由于多次GET请求超过10后,GET请求开始失败。

I read the docs and it was written to close the session after operation but i'm unable to figure out as to how to close this session in JAVA Configuration? 我阅读了文档,它是为了在操作后关闭会话而写的,但是我无法弄清楚如何在JAVA配置中关闭这个会话?

@org.springframework.integration.annotation.MessagingGateway
public interface FileOperationGateway {
    @Gateway(requestChannel = "sftpChannelDownload")
    InputStream downloadFromSftp(Message<Boolean> message);

}



@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(SFTP_HOST);
    factory.setPort(SFTP_PORT);
    factory.setUser(SFTP_USERNAME);
    factory.setPassword(SFTP_PASSWORD);
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(factory);
}

/**
 * Bean for Caching the session 
 * 
 */

@Bean
@Autowired
public CachingSessionFactory<LsEntry> cachingSessionFactory(SessionFactory<LsEntry> sftpSessionFactory) {
    CachingSessionFactory<LsEntry> cachingSessionFactory = new CachingSessionFactory<>(sftpSessionFactory, 10);
    cachingSessionFactory.setSessionWaitTimeout(SFTP_SESSION_TIMEOUT);
    return cachingSessionFactory;
}

/**
 * Bean for Remote File Template 
 * 
 * @return
 * @throws Exception 
 */

@Bean
@Autowired
public RemoteFileTemplate<LsEntry> remoteFileTemplateDesigner(CachingSessionFactory<LsEntry> csf) throws Exception {
    ExpressionParser expressionParser = new SpelExpressionParser();
    Expression expression = expressionParser.parseExpression("'" + SFTP_LOCATION + "'");
    SftpRemoteFileTemplate rft = new SftpRemoteFileTemplate(csf);
    rft.setRemoteDirectoryExpression(expression);
    rft.setRemoteFileSeparator("/");
    rft.setFileNameGenerator((msg) -> {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Instant instant = timestamp.toInstant();
        String fileNameFromHeader = msg.getHeaders().get(FileOperationConstants.FILE_HEADER_KEY).toString();
        String newFileName;
        if (fileNameFromHeader.lastIndexOf("/") != -1) {
            newFileName = fileNameFromHeader.substring(fileNameFromHeader.lastIndexOf("/"));
        } else if (fileNameFromHeader.lastIndexOf("\\") != -1) {
            newFileName = fileNameFromHeader.substring(fileNameFromHeader.lastIndexOf("\\"));
        } else
            newFileName = fileNameFromHeader;

        String fileNameOnly = newFileName.substring(0, newFileName.lastIndexOf("."));
        String fileType = newFileName.substring(newFileName.lastIndexOf(".") + 1);
        return (fileNameOnly + "__" + instant.toString() + "." + fileType);
    });
    rft.afterPropertiesSet();
    return rft;
}

@Bean
@Autowired
@ServiceActivator(inputChannel = "sftpChannelDownload")
public SftpOutboundGatewaySpec downloadHandler(RemoteFileTemplate<LsEntry> rft) {
    SftpOutboundGatewaySpec sogs =  Sftp.outboundGateway(rft, FileOperationConstants.FILE_DOWNLOAD_COMMAND,
            FileOperationConstants.FILE_DOWNLOAD_EXPRESSION);
    sogs.options(Option.STREAM);
    return sogs;
}

******UPDATE:****** ****** UPDATE:******

I created a new class with @messageEndpoint and placed the closeable session code in it. 我用@messageEndpoint创建了一个新类,并在其中放置了可关闭的会话代码。 I then called this handler from my service class (where i was consuming the stream)This worked: 然后我从我的服务类(我正在使用流)中调用此处理程序。这有效:

    @MessageEndpoint
public class FileOperationCloseSessionMessageHandler {

    @ServiceActivator(inputChannel = "sftpCloseSession")
    public void closeSession(Message<Boolean> msg) throws IOException {

        Closeable closeable = new IntegrationMessageHeaderAccessor(msg).getCloseableResource();
        if (closeable != null) {
            closeable.close();
        }
    }
}

Placed this line in @MessagingGateway annotated class 将此行放在@MessagingGateway注释类中

@Gateway(requestChannel = "sftpCloseSession")
void closeSession(Message<InputStream> msg);

And then called the gateway method from service class: 然后从服务类调用网关方法:

Message<InputStream> msg = msgGateway.downloadFromSftp(message);
    InputStream is = msg.getPayload();
    msgGateway.closeSession(msg);

sogs.options(Option.STREAM); sogs.options(Option.STREAM);

When you stream the file, you are responsible for closing the session after you have finished streaming. 流式传输文件时,您负责在完成流式传输后关闭会话。 This is explained in the documentation . 这在文档中有解释。

When consuming remote files as streams, the user is responsible for closing the Session after the stream is consumed. 将远程文件作为流使用时,用户负责在使用流后关闭会话。 For convenience, the Session is provided in the IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE header, a convenience method is provided on the IntegrationMessageHeaderAccessor: 为方便起见,Session在IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE标头中提供,IntegrationMessageHeaderAccessor上提供了一种便捷方法:

Closeable closeable = new IntegrationMessageHeaderAccessor(message).getCloseableResource();
if (closeable != null) {
    closeable.close();
}

Framework components such as the File Splitter and Stream Transformer will automatically close the session after the data is transferred. File Splitter和Stream Transformer等框架组件将在传输数据后自动关闭会话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件/文件从远程目录移动到另一个 Spring 集成 SFTP 出站网关 Java 配置/Java DSL - Moving file/files from remote directory to another Spring Integration SFTP Outbound Gateway Java Config/Java DSL 如何从 Spring 的组件中调用配置中的 SFTP 出站网关操作 - How to call SFTP Outbound Gateway operations in Configuration from Component in Spring 如何在Spring集成中使用JAVA配置创建http outbound-gateway? - How can I create http outbound-gateway with JAVA config in Spring integration? 如何使用多个sftp出站网关 - How to use multiple sftp outbound-gateway Spring Integeration SFTP出站网关mget -R问题 - Spring Integeration SFTP outbound gateway mget -R issue 设置 Spring Integration SFTP 出站网关时出现 UnsatisfiedDependencyException - UnsatisfiedDependencyException when setting up a Spring Integration SFTP outbound gateway 什么是与tcp-outbound-gateway等效的Java配置? - What is the java config equivalent to tcp-outbound-gateway? Spring 使用入站适配器消息处理程序中的出站网关的 Sftp 获取文件 - Spring Sftp fetch file using outbound gateway within inbound adapter message handler Spring Integration Java DSL - 如何调用int-http:outbound-gateway? - Spring Integration Java DSL - How to invoke int-http:outbound-gateway? 在第一个“找不到文件”问题之后,Sftp出站网关多个删除请求被卡住了 - Sftp Outbound Gateway multiple delete request getting stuck after first “File not found” issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM