我是 Spring Framework 的新手,确实,我正在学习和使用 Spring Boot。最近,在我正在开发的应用程序中,我让 Quartz Scheduler 工作,现在我想让 Spring Integration 在那里工作:FTP 连接到服务器以写入和读取文件。
我想要的非常简单(正如我在以前的 java 应用程序中能够做到的那样)。我有两个 Quartz Job 计划在每天不同的时间触发:一个从 FTP 服务器读取文件,另一个将文件写入 FTP 服务器。
我将详细介绍我目前所开发的内容。
@SpringBootApplication
@ImportResource("classpath:ws-config.xml")
@EnableIntegration
@EnableScheduling
public class MyApp extends SpringBootServletInitializer {
@Autowired
private Configuration configuration;
//...
@Bean
public DefaultFtpsSessionFactory myFtpsSessionFactory(){
DefaultFtpsSessionFactory sess = new DefaultFtpsSessionFactory();
Ftp ftp = configuration.getFtp();
sess.setHost(ftp.getServer());
sess.setPort(ftp.getPort());
sess.setUsername(ftp.getUsername());
sess.setPassword(ftp.getPassword());
return sess;
}
}
下面这个类我命名为FtpGateway,如下:
@Component
public class FtpGateway {
@Autowired
private DefaultFtpsSessionFactory sess;
public void sendFile(){
// todo
}
public void readFile(){
// todo
}
}
我正在阅读 this文档来学习这样做。 Spring Integration 的 FTP 似乎是事件驱动的,所以我不知道如何在触发器在确切时间触发时执行 Jobs 的 sendFile() 和 readFile()。
文档告诉我 something关于使用入站 channel 适配器(从 FTP 读取文件?)、出站 channel 适配器(将文件写入 FTP?)和出站网关(做什么?):
Spring Integration supports sending and receiving files over FTP/FTPS by providing three client side endpoints: Inbound Channel Adapter, Outbound Channel Adapter, and Outbound Gateway. It also provides convenient namespace-based configuration options for defining these client components.
所以,我还不清楚如何遵循。
请问有人可以给我提示吗?
谢谢!
编辑:
谢谢@M。迪南。首先,我将尝试一个简单的任务:从 FTP 读取文件,轮询器将每 5 秒运行一次。这是我添加的:
@Bean
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(myFtpsSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setPreserveTimestamp(true);
fileSynchronizer.setRemoteDirectory("/Entrada");
fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.csv"));
return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "ftpChannel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> ftpMessageSource() {
FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(inbound);
source.setLocalDirectory(new File(configuracion.getDirFicherosDescargados()));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
return source;
}
@Bean
@ServiceActivator(inputChannel = "ftpChannel")
public MessageHandler handler() {
return new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
Object payload = message.getPayload();
if(payload instanceof File){
File f = (File) payload;
System.out.println(f.getName());
}else{
System.out.println(message.getPayload());
}
}
};
}
然后,当应用程序运行时,我将一个新的 csv 文件引入“Entrada”远程文件夹,但 5 秒后 handler() 方法没有运行......我做错了什么?
最佳答案
请在您的轮询方法上添加 @Scheduled(fixedDelay = 5000)。
https://stackoverflow.com/questions/42107640/
相关文章:
java - 如何使用 Spring Data Pagination 在一页中获取所有结果
spring - 如何在 Spring 中通过 XML 定义 MySql 数据源 bean
spring - 在 Spring 中使用 @PropertyResource 访问多个属性文件
json - 406 Spring MVC Json,根据请求 "accept"headers No
java - 自定义 validator 消息 : Throwing exception in im
hibernate - java.lang.VerifyError : class net. sf.
java - 通过 Spring Config 扫描 Spring Data 存储库?
java - 如何使用modelAttribute在ajax(jquery)中提交spring表单