gpt4 book ai didi

java - Spring FTP 入站 channel 适配器每隔一定时间将日志记录到 FTP

转载 作者:行者123 更新时间:2023-12-01 09:15:50 24 4
gpt4 key购买 nike

快速提问那么在 FTP 入站 channel 适配器中如何每 10 分钟记录一次到远程 FTP,轮询器固定速率是做什么的?轮询器用于轮询但它一直登录到远程服务器?

我有这个:

@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;
}

或轮询器 METADATA 触发器:

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

或者如何每 10 分钟记录一次,然后轮询所有新文件,设置 Thread.sleep() ?

_______编辑___

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;
}

最佳答案

只有当您使用CachingSessionFactory时,它才会保持登录状态。

最好不要像这样 hibernate 并占用线程,而是使用任务调度程序(这就是轮询器的作用)。

new periodicTrigger(600_000) 将安排一个任务每 10 分钟登录一次并检查文件。

关于java - Spring FTP 入站 channel 适配器每隔一定时间将日志记录到 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40555253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com