我正在使用 spring 集成来轮询文件。此单个文件包含多个报告。我想将文件拆分为报告文件并另存为不同的文件。
<int-file:inbound-channel-adapter id="filesIn"
directory="file:${fileInDirectory}"
filename-pattern="*.txt"
prevent-duplicates="true">
<int:poller id="poller" fixed-delay="5000"/>
</int-file:inbound-channel-adapter>
<int:service-activator input-channel="filesIn"
output-channel="filesOut"
ref="handler"/>
<int-file:outbound-channel-adapter id="filesOut"
directory="file:${archiveDirectory}"
delete-source-files="true"/>
在处理程序内部,处理程序内部的处理方法如下。
public List<ReportContent> splitTextToReports(File file){
// split the file
// store the file content text to ReportContent object
// add to a List of ReportContent
}
ReportContent 具有以下字段
reportData(将保存在新文件中的文本)
报告类型
报告日期
每个 ReportContent 都需要进行另一个处理。
- 根据报告类型查找报告路径以保存报告。这是通过服务调用完成的。
- 将报告数据保存在表格中
以下是处理上述方法返回的列表的每个元素的方法。
public void processReportContent (ReportContent reportContent){
// process report content and save the file in the relevant place
}
问题的两个部分。
- 如何在读取第一个主文件后立即使用拆分器接管。这样每个报表的处理都可以完成拆分对象的一部分。
- 查找报告路径的服务应该在所有拆分的对象之间使用一个公共(public)的 HashMap。如果此 HashMap 中存在基于报告类型的值,它将从此映射中检索。否则,应执行单独的 API 调用以使用报告类型检索报告路径。报告类型和从此 API 调用收到的值(报告)将存储在 map 中。 Map 的重要性在于避免进行不必要的 API 调用。
最佳答案
1.
而不是 <int:service-activator input-channel="filesIn"...
我会加一条链
<int:chain id="processor" input-channel="filesIn" output-channel="filesOut">
<int:splitter>
<bean class="...your impl of org.springframework.integration.splitter.AbstractMessageSplitter..."/>
</int:splitter>
</int:chain>
并移动你的splitTextToReports
这个分离器实现的逻辑。因此,在拆分器之后的链中,您将拥有 ReportContent
的平坦流实例。
2。
在拆分器之后的链中添加转换步骤。把你的 processReportContent
逻辑在这里。转换的结果:有效负载中包含您的报告的字符串,以及“文件名”消息 header 变量中的文件名。
你的transformer的API可能是这样的
interface ReportContentTransformer {
Message<?> transform(ReportContent content);
}
链条看起来像
<int:chain id="processor" input-channel="filesIn" output-channel="filesOut">
<int:splitter>
...
</int:splitter>
<int:transformer ref="...ref on your ReportContentTransformer interface implementation bean..." method="transform"/>
</int:chain>
3。
添加到您的 outbound-channel-adapter
属性
filename-generator-expression="headers.get('filename')"
使用来自filename
的文件名文件存储时的变量。
关于java - 使用共享资源拆分文件并处理每个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35752705/