处理集成流程中的方法

时间:2018-12-20 06:41:37

标签: spring-integration

我创建了一个使用Spring MVC通过网页添加ftp服务器的应用程序。当我添加包含详细信息的服务器时,它将开始在服务器上轮询特定csv文件的目录流,一旦该文件存在,它将把它拉到我的本地文件夹,然后创建一个方法来读取该csv文件并生成一个新的csv将发送回服务器,现在已完成,我的问题是,当我添加新的ftp服务器进行轮询并在第二台服务器上找到一个csv文件时,应用程序将该文件拉到本地它应该这样做,但是两个csv文件都再次触发了处理方法,而它只应针对第二个服务器,因为已经为第一台服务器触发了,所以当我添加第二个服务器时,又为服务器1生成了另一个文件然后发送回ftp,将第二个服务器的新文件发送到第二个ftp,最后我将两个文件发送给第一个服务,一个文件发送给第二个服务器,以下是该应用的编码和输出consol,请协助查明我的问题。

@Configuration
@EnableIntegration
@ComponentScan
public class FTIntegration {

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 = 5000;
public static final int MAX_MESSAGES_PER_POLL = 100;


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

private static final String OUTBOUND_CHANNEL = "outboundChannel";

/* pulling the server config from postgres DB*/

private final BranchRepository branchRepository;

@Autowired
private CSVToCSVNoQ csvToCSVNoQ;

@Value("${app.temp-dir}")
private String localTempPath;

public FTIntegration(BranchRepository branchRepository) {
    this.branchRepository = branchRepository;
}

@Bean
public Branch myBranch(){
    return new Branch();
}

/**
 * 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)
            .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 PropertiesPersistingMetadataStore store() {
    PropertiesPersistingMetadataStore store = new PropertiesPersistingMetadataStore();
    return store;
}
 public IntegrationFlow fileInboundFlowFromFTPServer(Branch myBranch) throws IOException {

    final FtpInboundChannelAdapterSpec sourceSpecFtp = Ftp.inboundAdapter(createNewFtpSessionFactory(myBranch))
            .preserveTimestamp(true)
          //.patternFilter("*.csv")
            .maxFetchSize(MAX_MESSAGES_PER_POLL)
            .remoteDirectory(myBranch.getFolderPath())
            .regexFilter("FEFOexport"+myBranch.getBranchCode()+".csv")
            .deleteRemoteFiles(true)
            .localDirectory(new File(myBranch.getBranchCode()))
            .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> stockInboundPoller = endpointConfigurer -> endpointConfigurer
            .id("stockInboundPoller")
            .autoStartup(true)
            .poller(poller());

    IntegrationFlow flow = IntegrationFlows
            .from(sourceSpecFtp, stockInboundPoller)

            .transform(File.class, p ->{
                // log step
                LOG1.info("flow=stockInboundFlowFromAFT, message=incoming file: " + p);
                return p;
            })
            .channel(CHANNEL_INTERMEDIATE_STAGE)
            .handle(m -> {
                try {
                    this.csvToCSVNoQ.writeCSVfinal("test", myBranch.getBranchCode() + "/final" + myBranch.getBranchCode() + ".csv", myBranch.getBranchCode() + "/FEFOexport" + myBranch.getBranchCode() + ".csv");
                    LOG1.info("Writing final file .csv " + m);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            })
            //.handle(m -> this.csvToCSVNoQ.writeCSVfinal(m.getPayload(),m.getHeaders().get("csv", "FEFOexport"+myBranch.getBranchCode()+".csv")))
            .get();

    return flow;
}

@Bean
public IntegrationFlow stockIntermediateStageChannel() {
    IntegrationFlow flow = IntegrationFlows
            .from(CHANNEL_INTERMEDIATE_STAGE)
            .transform(p -> {
                //log step
                LOG1.info("flow=stockIntermediateStageChannel, message=rename file: " + p);

                return p;
            })
            //TODO
            .channel(new NullChannel())
            .get();

    return flow;

}

/*
* Creating the outbound adaptor to send files from local to FTP server
*
* */

public IntegrationFlow localToFtpFlow(Branch myBranch){
    return IntegrationFlows.from(Files.inboundAdapter(new File(myBranch.getBranchCode()))

                .filter(new ChainFileListFilter<File>()
                        .addFilter(new RegexPatternFileListFilter("final" + myBranch.getBranchCode() +".csv"))
                        .addFilter(new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "foo"))),
                        e -> e.poller(Pollers.fixedDelay(10_000)))
            .transform( p ->{
                LOG1.info("Sending file " + p + " to FTP branch " + myBranch.getBranchCode());

                return p;
            })


            .log()
            .handle(Ftp.outboundAdapter(createNewFtpSessionFactory(myBranch),FileExistsMode.REPLACE)
                    .useTemporaryFileName(true)
                    .autoCreateDirectory(false)
                    .remoteDirectory(myBranch.getFolderPath()))
            .get();
}


public DefaultFtpSessionFactory createNewFtpSessionFactory(Branch branch){
    final DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
    factory.setHost(branch.getHost());
    factory.setUsername(branch.getUsern());
    factory.setPort(branch.getFtpPort());
    factory.setPassword(branch.getPassword());
    return factory;
}

控制台输出:

Saved Branch : BEY
Hibernate: select branch0_._id as _id1_0_0_, branch0_.branch_code as branch_c2_0_0_, branch0_.folder_path as folder_p3_0_0_, branch0_.ftp_port as ftp_port4_0_0_, branch0_.host as host5_0_0_, branch0_.password as password6_0_0_, branch0_.usern as usern7_0_0_ from branch branch0_ where branch0_._id=?
2018-12-20 07:58:39.218  INFO 6668 --- [nio-8081-exec-5] o.s.integration.channel.DirectChannel    : Channel 'application.intermediateChannel' has 2 subscriber(s).
2018-12-20 07:58:39.218  INFO 6668 --- [nio-8081-exec-5] o.s.i.endpoint.EventDrivenConsumer       : started 1.org.springframework.integration.config.ConsumerEndpointFactoryBean#1
2018-12-20 07:58:39.218  INFO 6668 --- [nio-8081-exec-5] o.s.i.endpoint.EventDrivenConsumer       : Adding {transformer} as a subscriber to the '1.channel#0' channel
2018-12-20 07:58:39.218  INFO 6668 --- [nio-8081-exec-5] o.s.integration.channel.DirectChannel    : Channel 'application.1.channel#0' has 1 subscriber(s).
2018-12-20 07:58:39.218  INFO 6668 --- [nio-8081-exec-5] o.s.i.endpoint.EventDrivenConsumer       : started 1.org.springframework.integration.config.ConsumerEndpointFactoryBean#0
2018-12-20 07:58:39.229  INFO 6668 --- [nio-8081-exec-5] o.s.i.e.SourcePollingChannelAdapter      : started stockInboundPoller
2018-12-20 07:58:39.417  INFO 6668 --- [nio-8081-exec-5] o.s.i.endpoint.EventDrivenConsumer       : Adding {message-handler} as a subscriber to the '1o.channel#2' channel
2018-12-20 07:58:39.417  INFO 6668 --- [nio-8081-exec-5] o.s.integration.channel.DirectChannel    : Channel 'application.1o.channel#2' has 1 subscriber(s).
2018-12-20 07:58:39.418  INFO 6668 --- [nio-8081-exec-5] o.s.i.endpoint.EventDrivenConsumer       : started 1o.org.springframework.integration.config.ConsumerEndpointFactoryBean#1
2018-12-20 07:58:39.418  INFO 6668 --- [nio-8081-exec-5] o.s.i.endpoint.EventDrivenConsumer       : Adding {transformer} as a subscriber to the '1o.channel#0' channel
2018-12-20 07:58:39.418  INFO 6668 --- [nio-8081-exec-5] o.s.integration.channel.DirectChannel    : Channel 'application.1o.channel#0' has 1 subscriber(s).
2018-12-20 07:58:39.418  INFO 6668 --- [nio-8081-exec-5] o.s.i.endpoint.EventDrivenConsumer       : started 1o.org.springframework.integration.config.ConsumerEndpointFactoryBean#0
2018-12-20 07:58:39.419  INFO 6668 --- [nio-8081-exec-5] o.s.i.e.SourcePollingChannelAdapter      : started 1o.org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0
2018-12-20 07:59:41.165  INFO 6668 --- [sk-scheduler-10] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred from: /ftp/erbranch/EDMS/FEFO/FEFOexportBEY.csv
2018-12-20 07:59:49.446  INFO 6668 --- [ask-scheduler-1] o.s.i.file.FileReadingMessageSource      : Created message: [GenericMessage [payload=BEY\finalBEY.csv, headers={file_originalFile=BEY\finalBEY.csv, id=7f0cccb7-a070-bd4c-d468-977a265ceb2e, file_name=finalBEY.csv, file_relativePath=finalBEY.csv, timestamp=1545285589446}]]
2018-12-20 07:59:49.448  INFO 6668 --- [ask-scheduler-1] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=BEY\finalBEY.csv, headers={file_originalFile=BEY\finalBEY.csv, id=d857130a-d4a0-eaeb-19ea-f819924d94e2, file_name=finalBEY.csv, file_relativePath=finalBEY.csv, timestamp=1545285589447}]
2018-12-20 07:59:50.488  INFO 6668 --- [ask-scheduler-1] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred to: /ftp/erbranch/EDMS/FEFO/finalBEY.csv.writing
2018-12-20 07:59:50.899  INFO 6668 --- [ask-scheduler-1] o.s.integration.ftp.session.FtpSession   : File has been successfully renamed from: /ftp/erbranch/EDMS/FEFO/finalBEY.csv.writing to /ftp/erbranch/EDMS/FEFO/finalBEY.csv
Hibernate: select branch0_._id as _id1_0_, branch0_.branch_code as branch_c2_0_, branch0_.folder_path as folder_p3_0_, branch0_.ftp_port as ftp_port4_0_, branch0_.host as host5_0_, branch0_.password as password6_0_, branch0_.usern as usern7_0_ from branch branch0_
Hibernate: insert into branch (branch_code, folder_path, ftp_port, host, password, usern) values (?, ?, ?, ?, ?, ?)
Hibernate: select currval('branch__id_seq')
Saved Branch : JNB
Hibernate: select branch0_._id as _id1_0_0_, branch0_.branch_code as branch_c2_0_0_, branch0_.folder_path as folder_p3_0_0_, branch0_.ftp_port as ftp_port4_0_0_, branch0_.host as host5_0_0_, branch0_.password as password6_0_0_, branch0_.usern as usern7_0_0_ from branch branch0_ where branch0_._id=?
2018-12-20 08:02:24.966  INFO 6668 --- [nio-8081-exec-8] o.s.integration.channel.DirectChannel    : Channel 'application.intermediateChannel' has 3 subscriber(s).
2018-12-20 08:02:24.966  INFO 6668 --- [nio-8081-exec-8] o.s.i.endpoint.EventDrivenConsumer       : started 2.org.springframework.integration.config.ConsumerEndpointFactoryBean#1
2018-12-20 08:02:24.966  INFO 6668 --- [nio-8081-exec-8] o.s.i.endpoint.EventDrivenConsumer       : Adding {transformer} as a subscriber to the '2.channel#0' channel
2018-12-20 08:02:24.966  INFO 6668 --- [nio-8081-exec-8] o.s.integration.channel.DirectChannel    : Channel 'application.2.channel#0' has 1 subscriber(s).
2018-12-20 08:02:24.966  INFO 6668 --- [nio-8081-exec-8] o.s.i.endpoint.EventDrivenConsumer       : started 2.org.springframework.integration.config.ConsumerEndpointFactoryBean#0
2018-12-20 08:02:24.966  INFO 6668 --- [nio-8081-exec-8] o.s.i.e.SourcePollingChannelAdapter      : started stockInboundPoller
2018-12-20 08:02:24.992  INFO 6668 --- [nio-8081-exec-8] o.s.i.endpoint.EventDrivenConsumer       : Adding {message-handler} as a subscriber to the '2o.channel#2' channel
2018-12-20 08:02:24.992  INFO 6668 --- [nio-8081-exec-8] o.s.integration.channel.DirectChannel    : Channel 'application.2o.channel#2' has 1 subscriber(s).
2018-12-20 08:02:24.992  INFO 6668 --- [nio-8081-exec-8] o.s.i.endpoint.EventDrivenConsumer       : started 2o.org.springframework.integration.config.ConsumerEndpointFactoryBean#1
2018-12-20 08:02:24.992  INFO 6668 --- [nio-8081-exec-8] o.s.i.endpoint.EventDrivenConsumer       : Adding {transformer} as a subscriber to the '2o.channel#0' channel
2018-12-20 08:02:24.992  INFO 6668 --- [nio-8081-exec-8] o.s.integration.channel.DirectChannel    : Channel 'application.2o.channel#0' has 1 subscriber(s).
2018-12-20 08:02:24.992  INFO 6668 --- [nio-8081-exec-8] o.s.i.endpoint.EventDrivenConsumer       : started 2o.org.springframework.integration.config.ConsumerEndpointFactoryBean#0
2018-12-20 08:02:24.992  INFO 6668 --- [nio-8081-exec-8] o.s.i.e.SourcePollingChannelAdapter      : started 2o.org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0
Hibernate: select branch0_._id as _id1_0_, branch0_.branch_code as branch_c2_0_, branch0_.folder_path as folder_p3_0_, branch0_.ftp_port as ftp_port4_0_, branch0_.host as host5_0_, branch0_.password as password6_0_, branch0_.usern as usern7_0_ from branch branch0_
2018-12-20 08:03:00.225  INFO 6668 --- [ask-scheduler-8] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred from: /ftp/erbranch/EDMS/FEFO/FEFOexportJNB.csv
2018-12-20 08:03:00.929  INFO 6668 --- [ask-scheduler-5] o.s.i.file.FileReadingMessageSource      : Created message: [GenericMessage [payload=BEY\finalBEY.csv, headers={file_originalFile=BEY\finalBEY.csv, id=6ed554eb-f553-0293-f042-d633155357c0, file_name=finalBEY.csv, file_relativePath=finalBEY.csv, timestamp=1545285780929}]]
2018-12-20 08:03:00.930  INFO 6668 --- [ask-scheduler-5] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=BEY\finalBEY.csv, headers={file_originalFile=BEY\finalBEY.csv, id=b2d76ac5-fb85-1313-a37e-8849714a545e, file_name=finalBEY.csv, file_relativePath=finalBEY.csv, timestamp=1545285780930}]
2018-12-20 08:03:01.958  INFO 6668 --- [ask-scheduler-5] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred to: /ftp/erbranch/EDMS/FEFO/finalBEY.csv.writing
2018-12-20 08:03:02.373  INFO 6668 --- [ask-scheduler-5] o.s.integration.ftp.session.FtpSession   : File has been successfully renamed from: /ftp/erbranch/EDMS/FEFO/finalBEY.csv.writing to /ftp/erbranch/EDMS/FEFO/finalBEY.csv
2018-12-20 08:03:05.033  INFO 6668 --- [ask-scheduler-7] o.s.i.file.FileReadingMessageSource      : Created message: [GenericMessage [payload=JNB\finalJNB.csv, headers={file_originalFile=JNB\finalJNB.csv, id=4514e132-5684-9e82-28e7-f75c5c3dcf91, file_name=finalJNB.csv, file_relativePath=finalJNB.csv, timestamp=1545285785033}]]
2018-12-20 08:03:05.034  INFO 6668 --- [ask-scheduler-7] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=JNB\finalJNB.csv, headers={file_originalFile=JNB\finalJNB.csv, id=59e62375-f1da-461d-ee61-d105ac3159a0, file_name=finalJNB.csv, file_relativePath=finalJNB.csv, timestamp=1545285785034}]
2018-12-20 08:03:07.530  INFO 6668 --- [ask-scheduler-7] o.s.integration.ftp.session.FtpSession   : File has been successfully transferred to: /ftp/erbranch/EDMS/FEFO/finalJNB.csv.writing
2018-12-20 08:03:08.539  INFO 6668 --- [ask-scheduler-7] o.s.integration.ftp.session.FtpSession   : File has been successfully renamed from: /ftp/erbranch/EDMS/FEFO/finalJNB.csv.writing to /ftp/erbranch/EDMS/FEFO/finalJNB.csv

1 个答案:

答案 0 :(得分:0)

.addFilter(new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "foo"))),

SimpleMetadataStore()处于内存中-为每个流使用新存储意味着每次它都开始为空-您需要在所有流中使用共享的元数据存储。

此外,如果您需要防止服务器重新启动时出现重复,则需要使用持久性共享存储,例如Redis,JDBC等。