簡體   English   中英

使用Spring Integration for Inbound Adapter時如何檢查SFTP連接是否成功

[英]How to check whether the SFTP connection is successful or not when using Spring Integration for Inbound Adapter

我正在使用Spring Integration將xml文件從SFTP傳輸到本地。 我使用了Spring Community提供的代碼。

主類文件

public class SFTPMain {
  public static void main(String[] args) {
    ConfigurableApplicationContext context =
        new ClassPathXmlApplicationContext("/SftpInboundReceiveSample-context.xml", SftpInboundReceiveSample.class);
    SftpInboundReceiveSample sftp = context.getBean("sftpService", SftpInboundReceiveSample.class);
    sftp.setContext(context);
    try {
        sftp.receiveFile();
    } catch (Exception e) {
        System.out.println(e);
     } finally {
         context.close();
    }
}
}

服務等級

@Component("sftpService")
public class SftpInboundReceiveSample {

public ConfigurableApplicationContext getContext() {
    return context;
}
public void setContext(ConfigurableApplicationContext context) {
    this.context = context;
}

ConfigurableApplicationContext context;

public void receiveFile(){
    try {
        PollableChannel localFileChannel = context.getBean("receiveChannel", PollableChannel.class);
        SourcePollingChannelAdapter adapter = context.getBean(SourcePollingChannelAdapter.class);       
        adapter.start();
        System.out.println("Adapter started...");
        Message<?> received = localFileChannel.receive();
        System.out.println("Received first file message: " + received);
        MessageHeaders header= received.getHeaders();
        header.getTimestamp();

    }
    finally {
    }
}
}

XML文件

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

<import resource="SftpSampleCommon.xml" />

<bean id="sftpSessionFactory"
    class="org.springframework.integration.file.remote.session.CachingSessionFactory">
    <constructor-arg ref="defaultSftpSessionFactory" />
</bean>

<bean id="defaultSftpSessionFactory"
        class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="${sftp.host}"/>
    <property name="password" value="${sftp.password}"/>
    <property name="port" value="${sftp.port}"/>
    <property name="user" value="${sftp.username}"/>
</bean>

<int-sftp:inbound-channel-adapter id="sftpInbondAdapter"
    session-factory="sftpSessionFactory" channel="receiveChannel"
    filename-pattern="*" remote-directory="/loblawln"
    local-directory="D:/local-dir"
    auto-create-local-directory="true" temporary-file-suffix=".writing"
    delete-remote-files="false">
    <int:poller fixed-rate="1000" max-messages-per-poll="1" />
</int-sftp:inbound-channel-adapter>

<int:channel id="receiveChannel">
    <int:queue />
</int:channel>
</beans>

還想知道輪詢是否是強制性的,因為我將通過CRON作業使用適配器,該適配器每天僅對SFTP服務器執行一次ping操作。

首先, <poller>支持cron 因此,我認為沒有理由引入一些額外的CRON job

從另一方面來看, <poller>也具有error-channel選項。 這樣一來,您可以捕獲輪詢流程中的任何問題,包括SFTP連接錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM