我是Spring集成的新手。我正在研究解决方案,但在使用入站文件适配器(FileReadingMessageSource)时,我遇到了特定问题。 我必须从不同目录读取文件并处理它们,并将这些文件保存在不同的目录中。据我所知,目录名称在流程开始时是固定的。 有人可以帮助我更改不同请求的目录名称。春季集成文件阅读
我尝试了以下方法。首先,我不确定这是否是正确的方法,尽管它只适用于一个目录。我认为Poller正在等待更多的文件,并且再也没有回来阅读另一个目录。
@SpringBootApplication
@EnableIntegration
@IntegrationComponentScan
public class SiSampleFileProcessor {
@Autowired
MyFileProcessor myFileProcessor;
@Value("${si.outdir}")
String outDir;
@Autowired
Environment env;
public static void main(String[] args) throws IOException {
ConfigurableApplicationContext ctx = new SpringApplication(SiSampleFileProcessor.class).run(args);
FileProcessingService gateway = ctx.getBean(FileProcessingService.class);
boolean process = true;
while (process) {
System.out.println("Please enter the input Directory: ");
String inDir = new Scanner(System.in).nextLine();
if (inDir.isEmpty() || inDir.equals("exit")) {
process=false;
} else {
System.out.println("Processing... " + inDir);
gateway.processFilesin(inDir);
}
}
ctx.close();
}
@MessagingGateway(defaultRequestChannel="requestChannel")
public interface FileProcessingService {
String processFilesin(String inputDir);
}
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {
return Pollers.fixedDelay(1000).get();
}
@Bean
public MessageChannel requestChannel() {
return new DirectChannel();
}
@ServiceActivator(inputChannel = "requestChannel")
@Bean
GenericHandler<String> fileReader() {
return new GenericHandler<String>() {
@Override
public Object handle(String p, Map<String, Object> map) {
FileReadingMessageSource fileSource = new FileReadingMessageSource();
fileSource.setDirectory(new File(p));
Message<File> msg;
while((msg = fileSource.receive()) != null) {
fileInChannel().send(msg);
}
return null; // Not sure what to return!
}
};
}
@Bean
public MessageChannel fileInChannel() {
return MessageChannels.queue("fileIn").get();
}
@Bean
public IntegrationFlow fileProcessingFlow() {
return IntegrationFlows.from(fileInChannel())
.handle(myFileProcessor)
.handle(Files.outboundAdapter(new File(outDir)).autoCreateDirectory(true).get())
.get();
}
}
编辑:根据Gary的响应更换了一些方法,如
@MessagingGateway(defaultRequestChannel="requestChannel")
public interface FileProcessingService {
boolean processFilesin(String inputDir);
}
@ServiceActivator(inputChannel = "requestChannel")
public boolean fileReader(String inDir) {
FileReadingMessageSource fileSource = new FileReadingMessageSource();
fileSource.setDirectory(new File(inDir));
fileSource.afterPropertiesSet();
fileSource.start();
Message<File> msg;
while ((msg = fileSource.receive()) != null) {
fileInChannel().send(msg);
}
fileSource.stop();
System.out.println("Sent all files in directory: " + inDir);
return true;
}
现在按预期工作。
谢谢你,作为工作预期。我已经用你的建议编辑了这篇文章。但需要澄清。对于类似的需求(即阅读不同的目录),如果源代码是S3,我们该怎么办? – pkm
@pkm mount s3作为一个文件系统并监听它的变化,或者你可以使用带有触发器的aws微服务流在s3上放置/发布,并且你对它采取适当的行动 –
[spring-integration-aws](https:// github.com/spring-projects/spring-integration-aws)项目提供了一个消息源,但它更复杂一点,它将远程目录与本地同步,然后对本地目录使用“FileReadingMessageSource”。您应该使用'S3RemoteFileTemplate'代替。 –