Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring集成Java DSL SFTP如何在handler中获取远程SFTP服务器信息_Java_Spring_Spring Boot_Spring Integration_Spring Integration Sftp - Fatal编程技术网

Spring集成Java DSL SFTP如何在handler中获取远程SFTP服务器信息

Spring集成Java DSL SFTP如何在handler中获取远程SFTP服务器信息,java,spring,spring-boot,spring-integration,spring-integration-sftp,Java,Spring,Spring Boot,Spring Integration,Spring Integration Sftp,我试图从多个SFTP服务器下载文件,然后处理这些文件。但我无法根据MessageHandler处理的文件获取远程SFTP服务器的信息,例如:IpAddress、remoteDirectory。相反,负载只包含本地加载的文件的信息。下面是我在指南中使用的源代码: sft.java import com.jcraft.jsch.ChannelSftp.LsEntry; import java.io.File; import java.time.Instant; import java.time.Z

我试图从多个SFTP服务器下载文件,然后处理这些文件。但我无法根据MessageHandler处理的文件获取远程SFTP服务器的信息,例如:IpAddress、remoteDirectory。相反,负载只包含本地加载的文件的信息。下面是我在指南中使用的源代码:

sft.java

import com.jcraft.jsch.ChannelSftp.LsEntry;
import java.io.File;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.NullChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.dsl.SourcePollingChannelAdapterSpec;
import org.springframework.integration.expression.FunctionExpression;
import org.springframework.integration.file.remote.aop.RotatingServerAdvice;
import org.springframework.integration.file.remote.session.DelegatingSessionFactory;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.integration.sftp.dsl.Sftp;
import org.springframework.integration.sftp.dsl.SftpInboundChannelAdapterSpec;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Component;

/**
 * flow.
 */
@Configuration
@Component
public class SFTIntegration {
    public static final String TIMEZONE_UTC = "UTC";
    public static final String TIMESTAMP_FORMAT_OF_FILES = "yyyyMMddHHmmssSSS";
    public static final String TEMPORARY_FILE_SUFFIX = ".part";
    public static final int POLLER_FIXED_PERIOD_DELAY = 60000;
    public static final int MAX_MESSAGES_PER_POLL = 100;

    private static final Logger LOG = LoggerFactory.getLogger(SFTIntegration.class);
    private static final String CHANNEL_INTERMEDIATE_STAGE = "intermediateChannel";

    @Autowired
    private ImportHandler importHandler;

    /** database access repository */
    private final SFTPServerConfigRepo SFTPServerConfigRepo;

    @Value("${sftp.local.directory.download:${java.io.tmpdir}/localDownload}")
    private String localTemporaryPath;

    public SFTIntegration(final SFTPServerConfigRepo SFTPServerConfigRepo) {
        this.SFTPServerConfigRepo = SFTPServerConfigRepo;
    }

    /**
     * The default poller with 5s, 100 messages, RotatingServerAdvice and transaction.
     *
     * @return default poller.
     */
    @Bean(name = PollerMetadata.DEFAULT_POLLER)
    public PollerMetadata poller() {
        return Pollers
                .fixedDelay(POLLER_FIXED_PERIOD_DELAY)
                .advice(advice())
                .maxMessagesPerPoll(MAX_MESSAGES_PER_POLL)
                .transactional()
                .get();
    }

    /**
     * The direct channel for the flow.
     *
     * @return MessageChannel
     */
    @Bean
    public MessageChannel stockIntermediateChannel() {
        return new DirectChannel();
    }

    /**
     * Get the files from a remote directory. Add a timestamp to the filename
     * and write them to a local temporary folder.
     *
     * @return IntegrationFlow
     */
    @Bean
    public IntegrationFlow collectionInboundFlowFromSFTPServer() {
        // Source definition
        final SftpInboundChannelAdapterSpec sourceSpec = Sftp.inboundAdapter(delegatingSFtpSessionFactory())

                .preserveTimestamp(true)
                .patternFilter("*.*")
                .deleteRemoteFiles(true)
                .maxFetchSize(MAX_MESSAGES_PER_POLL)
                .remoteDirectory("/")
                .localDirectory(new File(localTemporaryPath))
                .temporaryFileSuffix(TEMPORARY_FILE_SUFFIX)
                .localFilenameExpression(new FunctionExpression<String>(s -> {
                    final int fileTypeSepPos = s.lastIndexOf('.');
                    return
                            DateTimeFormatter
                                    .ofPattern(TIMESTAMP_FORMAT_OF_FILES)
                                    .withZone(ZoneId.of(TIMEZONE_UTC))
                                    .format(Instant.now())
                                    + "_"
                                    + s.substring(0, fileTypeSepPos)
                                    + s.substring(fileTypeSepPos);
                }));

        // Poller definition
        final Consumer<SourcePollingChannelAdapterSpec> collectionInboundPoller = endpointConfigurer -> endpointConfigurer
                .id("collectionInboundPoller")
                .autoStartup(true)
                .poller(poller());

        return IntegrationFlows
                .from(sourceSpec, collectionInboundPoller)
                .transform(File.class, p -> {
                    // log step
                    LOG.info("flow=collectionInboundFlowFromSFTPServer, message=incoming file: " + p);
                    return p;
                })
                .channel(CHANNEL_INTERMEDIATE_STAGE)
                .get();
    }

    @Bean
    public IntegrationFlow collectionIntermediateStageChannel() {
        return IntegrationFlows
                .from(CHANNEL_INTERMEDIATE_STAGE)
                .handle(importHandler)
                .channel(new NullChannel())
                .get();
    }

    public DefaultSftpSessionFactory createNewSftpSessionFactory(final SFTPServerConfig pc) {
        final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(
                false);
        factory.setHost(pc.getServerIp());
        factory.setPort(pc.getPort());
        factory.setUser(pc.getUsername());
        factory.setPassword(pc.getPassword());
        factory.setAllowUnknownKeys(true);
        return factory;
    }

    @Bean
    public DelegatingSessionFactory<LsEntry> delegatingSFtpSessionFactory() {
        final List<SFTPServerConfig> partnerConnections = SFTPServerConfigRepo.findAll();

        if (partnerConnections.isEmpty()) {
            return null;
        }

        final Map<Object, SessionFactory<LsEntry>> factories = new LinkedHashMap<>(10);

        for (SFTPServerConfig pc : partnerConnections) {
            // create a factory for every key containing server type, url and port
            if (factories.get(pc.getKey()) == null) {
                factories.put(pc.getKey(), createNewSftpSessionFactory(pc));
            }
        }

        // use the first SF as the default
        return new DelegatingSessionFactory<>(factories, factories.values().iterator().next());
    }

    @Bean
    public RotatingServerAdvice advice() {
        final List<SFTPServerConfig> sftpConnections = SFTPServerConfigRepo.findAll();

        final List<RotatingServerAdvice.KeyDirectory> keyDirectories = new ArrayList<>();
        for (SFTPServerConfig pc : sftpConnections) {
            keyDirectories
                    .add(new RotatingServerAdvice.KeyDirectory(pc.getKey(), pc.getServerPath()));
        }

        return new RotatingServerAdvice(delegatingSFtpSessionFactory(), keyDirectories, true);
    }
}
import com.jcraft.jsch.ChannelSftp.LsEntry;
导入java.io.File;
导入java.time.Instant;
导入java.time.ZoneId;
导入java.time.format.DateTimeFormatter;
导入java.util.ArrayList;
导入java.util.LinkedHashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.function.Consumer;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.integration.channel.DirectChannel;
导入org.springframework.integration.channel.NullChannel;
导入org.springframework.integration.dsl.IntegrationFlow;
导入org.springframework.integration.dsl.IntegrationFlows;
导入org.springframework.integration.dsl.poller;
导入org.springframework.integration.dsl.SourcePollingChannelAdapterSpec;
导入org.springframework.integration.expression.FunctionExpression;
导入org.springframework.integration.file.remote.aop.RotatingServerAdvice;
导入org.springframework.integration.file.remote.session.DelegatingSessionFactory;
导入org.springframework.integration.file.remote.session.SessionFactory;
导入org.springframework.integration.scheduling.PollerMetadata;
导入org.springframework.integration.sftp.dsl.sftp;
导入org.springframework.integration.sftp.dsl.SftpInboundChannelAdapterSpec;
导入org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
导入org.springframework.messaging.MessageChannel;
导入org.springframework.stereotype.Component;
/**
*流动。
*/
@配置
@组成部分
公共类集成{
公共静态最终字符串时区\u UTC=“UTC”;
_FILES=“yyyymmddhhmmssss”的公共静态最终字符串时间戳\u格式;
公共静态最终字符串临时文件后缀=“.part”;
公共静态最终整数轮询器固定周期延迟=60000;
公共静态最终int MAX_MESSAGES_PER_POLL=100;
私有静态最终记录器LOG=LoggerFactory.getLogger(SFTIntegration.class);
专用静态最终字符串通道\u中间\u阶段=“中间通道”;
@自动连线
私人进口商;
/**数据库访问存储库*/
专用最终SFTPServerConfigRepo SFTPServerConfigRepo;
@值(${sftp.local.directory.download:${java.io.tmpdir}/localDownload}”)
私有字符串localTemporaryPath;
公共SFT集成(最终SFTPServerConfigRepo SFTPServerConfigRepo){
this.SFTPServerConfigRepo=SFTPServerConfigRepo;
}
/**
*带有5s、100条消息、旋转服务器建议和事务的默认轮询器。
*
*@return默认轮询器。
*/
@Bean(name=PollerMetadata.DEFAULT\u POLLER)
公共民意调查机构{
回程轮询器
.fixedDelay(轮询器\固定\周期\延迟)
.advice(advice())
.maxMessagesPerPoll(每个轮询的最大消息数)
.事务性
.get();
}
/**
*流动的直接通道。
*
*@returnmessagechannel
*/
@豆子
公共消息频道stockIntermediateChannel(){
返回新的DirectChannel();
}
/**
*从远程目录获取文件。向文件名添加时间戳
*并将它们写入本地临时文件夹。
*
*@返回积分流
*/
@豆子
公共IntegrationFlow collectionInboundFlowFromSFTPServer(){
//源定义
最终SftpInboundChannelAdapterSpec sourceSpec=Sftp.inboundAdapter(delegatingSFtpSessionFactory())
.保留时间戳(true)
.patternFilter(“***”)
.deleteRemoteFiles(true)
.maxFetchSize(每个轮询的最大消息数)
.remoteDirectory(“/”)
.localDirectory(新文件(localTemporaryPath))
.temporaryFileSuffix(临时文件后缀)
.localFilenameExpression(新函数表达式->{
final int fileTypeSepPos=s.lastIndexOf('.');
回来
日期时间格式化程序
.OF模式(时间戳\u格式\u文件)
.withZone(ZoneId.of(时区协调世界时))
.format(Instant.now())
+ "_"
+s.substring(0,文件类型seppos)
+s.子字符串(fileTypeSepPos);
}));
//轮询器定义
最终使用者集合BoundPoller=endpointConfigurer->endpointConfigurer
.id(“CollectionBoundPoller”)
.AutoStart(真)
.poller(poller());
返回积分流
.from(sourceSpec、CollectionBoundPoller)
.transform(File.class,p->{
//日志步骤
LOG.info(“flow=collectionBoundFlowFromSFTPServer,message=innoming file:”+p);
返回p;
})
.通道(通道中间阶段)
.get();
}
@豆子
公共集成流收集中间阶段通道(){
返回积分流
.从(通道\中间\阶段)
.手柄(导入器)
.channel(新的NullChannel())
葛先生
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;


@Service
public class ImportHandler {

    public void handle(Message<?> message) {
        System.out.println("Hello " + message);
        System.out.println(message.getPayload());
        System.out.println(message.getHeaders());
        //How can I get the information of remote server Ip address, remoteDirectory here where the file comes from
    }
}