viruskimera
viruskimera

Reputation: 207

Spring FTP Inbound Channel Adapter log every certain time to the FTP

quick question So in the FTP inbound channel adapter how to log for example every 10 minutes to the remote FTP, is the poller fixed rate what does this? the poller is for polling but it keeps logged into the remote server?

I have this:

@Bean
    @InboundChannelAdapter(value = "stream", poller = @Poller(fixedRate = "1000"))
    public MessageSource<InputStream> ftpMessageSource() {

        FtpStreamingMessageSource messageSource = new FtpStreamingMessageSource(template(), null);

        messageSource.setRemoteDirectory(remotedirectory);
        messageSource.setFilter(filter());

        return messageSource;
    }

or the poller METADATA trigger:

 @Bean(name = PollerMetadata.DEFAULT_POLLER)
        public PollerMetadata defaultPoller() {
            PollerMetadata pollerMetadata = new PollerMetadata();
            pollerMetadata.setTrigger(new PeriodicTrigger(5000));
            return pollerMetadata;
        }

or how to log every 10 minutes and then poll all new files, setting a Thread.sleep() ?

_______EDIT___

public static void main(String[] args) {
        SpringApplication.run(FtpinboundApp.class, args);
    }

    @Bean
    public SessionFactory<FTPFile> ftpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost(remotehost);
        sf.setPort(remoteport);
        sf.setUsername(remoteuser);
        sf.setPassword(remotepassword);
        return new CachingSessionFactory<FTPFile>(sf);
    }

    @Bean
    @ServiceActivator(inputChannel = "data", adviceChain = "after")
    public MessageHandler handler() {
        return new MessageHandler() {

            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                try {
                    httpposthgfiles.getHGFilesfromRestful(message.getPayload().toString());
                    httppost990.get990fromRestful(message.getPayload().toString());

                } catch (IOException e) {
                    logger.error(e);
                } catch (Exception e) {
                    logger.error(e);
                }
            }
        };
    }

    @Bean
    public ExpressionEvaluatingRequestHandlerAdvice after() {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        advice.setOnSuccessExpression("@template.remove(headers['file_remoteDirectory'] + headers['file_remoteFile'])");
        advice.setPropagateEvaluationFailures(true);
        return advice;
    }

    @Bean
    @InboundChannelAdapter(value = "stream", poller = @Poller(fixedRate = "1000"))
    public MessageSource<InputStream> ftpMessageSource() {

        FtpStreamingMessageSource messageSource = new FtpStreamingMessageSource(template(), null);

        messageSource.setRemoteDirectory(remotedirectory);
        messageSource.setFilter(filter());

        return messageSource;
    }

    public FileListFilter<FTPFile> filter() {
        CompositeFileListFilter<FTPFile> filter = new CompositeFileListFilter<>();
        filter.addFilter(new FtpSimplePatternFileListFilter("xxxx_aaa204*"));
        filter.addFilter(acceptOnceFilter());
        return filter;
    }

    @Bean
    public FtpPersistentAcceptOnceFileListFilter acceptOnceFilter() {
        FtpPersistentAcceptOnceFileListFilter filter = new FtpPersistentAcceptOnceFileListFilter(meta(), "xxxx_aaa204");
        filter.setFlushOnUpdate(true);
        return filter;
    }

    @Bean
    public ConcurrentMetadataStore meta() {
        PropertiesPersistingMetadataStore meta = new PropertiesPersistingMetadataStore();
        meta.setBaseDirectory("/tmp/foo");
        meta.setFileName("ftpStream.properties");
        return meta;
    }

    @Bean
    @Transformer(inputChannel = "stream", outputChannel = "data")
    public org.springframework.integration.transformer.Transformer transformer() {
        return new StreamTransformer("UTF-8");
    }

    @Bean
    public FtpRemoteFileTemplate template() {
        return new FtpRemoteFileTemplate(ftpSessionFactory());
    }

    @Bean(name = PollerMetadata.DEFAULT_POLLER)
    public PollerMetadata defaultPoller() {
        PollerMetadata pollerMetadata = new PollerMetadata();
        pollerMetadata.setTrigger(new PeriodicTrigger(5000));
        return pollerMetadata;
    }

Upvotes: 0

Views: 679

Answers (1)

Gary Russell
Gary Russell

Reputation: 174664

It will only stay logged in if you use a CachingSessionFactory.

It's better not to sleep and tie up a thread like that, but use the task scheduler (which is what the poller does).

new PeriodicTrigger(600_000) will schedule a task to log in and check for files once every 10 minutes.

Upvotes: 1

Related Questions