我的意图是从各种源/目录创建 IntegrationFlow bean 实例(首先,可能来自 ftp)。因此,在application.properties
我想定义这样的东西时,入站目录的数量可能会有所不同:
inbound.file.readPath[0]=source1
inbound.file.processedPath[0]=processed1
inbound.file.failedPath[0]=failed1
inbound.file.readPath[1]=source2
inbound.file.processedPath[1]=processed2
inbound.file.failedPath[1]=failed2
我还想维护源的来源(通过标题丰富),因此不能将所有文件放在 spring 之外的一个目录中。
那么有一个 FilePollingFlow 是否可以从上述属性创建这些 bean 实例?我可以想象这样的事情,但我不确定如何将属性传递给 bean 实例以及如何引用索引:
@Configuration
public class FilePollingIntegrationFlow extends AbstractFactoryBean<IntegrationFlow> {
@Autowired
private FilePollingConfiguration config;
@Override
public Class<IntegrationFlow> getObjectType() {
return IntegrationFlow.class;
}
@Override
protected IntegrationFlow createInstance() throws Exception {
return IntegrationFlows
.from(s -> /* FIXME config.getReadPath()? instead of inboundReadDirectory, but how to handle indices? */s.file(inboundReadDirectory).preventDuplicates(true).scanEachPoll(true).patternFilter("*.txt"),
e -> e.poller(Pollers.fixedDelay(inboundPollingPeriod)
.taskExecutor(taskExecutor())
.transactionSynchronizationFactory(transactionSynchronizationFactory())
.transactional(transactionManager())))
.log(LoggingHandler.Level.INFO, getClass().getName(), "'Read inbound file: ' .concat(payload)")
.enrichHeaders(m -> m.headerExpression(FileHeaders.ORIGINAL_FILE, "payload"))
.transform(Transformers.fileToString())
.channel(ApplicationConfiguration.FILE_INBOUND_CHANNEL)
.get();
}
}
@Component
@ConfigurationProperties("inbound")
public class FilePollingConfiguration {
private List<File> files = new ArrayList<>();
public static class File {
private String readPath;
private String processedPath;
private String failedPath;
public String getReadPath() {
return readPath;
}
public void setReadPath(String readPath) {
this.readPath = readPath;
}
public String getProcessedPath() {
return processedPath;
}
public void setProcessedPath(String processedPath) {
this.processedPath = processedPath;
}
public String getFailedPath() {
return failedPath;
}
public void setFailedPath(String failedPath) {
this.failedPath = failedPath;
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("readPath", readPath)
.append("processedPath", processedPath)
.append("failedPath", failedPath)
.toString();
}
public List<File> getFiles() {
return files;
}
public void setFiles(List<File> files) {
this.files = files;
}
}