Java 使用共享资源时拆分文件并处理每个部分
我正在使用spring集成来轮询文件。这个文件包含多个报告。我想将文件拆分为报告文件并另存为不同的文件Java 使用共享资源时拆分文件并处理每个部分,java,spring,file,spring-integration,splitter,Java,Spring,File,Spring Integration,Splitter,我正在使用spring集成来轮询文件。这个文件包含多个报告。我想将文件拆分为报告文件并另存为不同的文件 <int-file:inbound-channel-adapter id="filesIn" directory="file:${fileInDirectory}" filename-pattern="*.txt" prevent-duplicates="true"> <int:poller id="poller"
<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"/>
问题分为两部分
与并行处理项目不同,
总是有一个技巧用于
,就像下游的执行器通道一样,因此在分解项目的迭代过程中,我们在发送前一个项目后立即移动到下一个项目
此外,为了获得更好的吞吐量,拆分器
支持流媒体的迭代器
我本来打算为您的任务推荐文件拆分器,但我想您不是按行拆分,而是按其他标识符拆分。也许您的内容只是XML或JSON,这使得确定部分内容非常容易
从这里开始,为您的案例提供一些迭代器实现可能不是那么容易
不过我想这没关系。您已经有了拆分逻辑并构建了列表
关于ConcurrentMap
当下一次调用同一个键将从缓存返回值时,我们来看看对“硬”服务的@Cacheable
Spring支持如何
为此,您可以使用
上的目录表达式
:
文件名也可以采用相同的技术
注意:注意文件名的默认标题:FileHeaders.FILENAME
是的,您的想法解决了这个问题。但我有一个新的疑问。如果你能提供一些想法,我将不胜感激。
public List<ReportContent> splitTextToReports(File file){
// split the file
// store the file content text to ReportContent object
// add to a List of ReportContent
}
public void processReportContent (ReportContent reportContent){
// process report content and save the file in the relevant place
}
<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>
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>
filename-generator-expression="headers.get('filename')"
<int-file:outbound-channel-adapter directory-expression="@reportPathService.getPath(payload)" />