Java 使用共享资源时拆分文件并处理每个部分

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"

我正在使用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"/>
问题分为两部分

  • 如何在读取第一个主文件后使用拆分器接管。因此,每个报表的处理都可以作为分割对象的一部分来完成
  • 查找报告路径的服务应在所有拆分对象之间使用公共哈希映射。如果此哈希映射中存在基于报告类型的值,则它将从此映射中检索。否则,应执行单独的API调用,以使用报告类型检索报告路径。从该API调用接收的报告类型和值(报告)将存储在映射中。Map的重要性在于避免进行不必要的API调用
  • 一,。
    与并行处理项目不同,
    总是有一个技巧用于
    ,就像下游的
    执行器通道一样,因此在分解项目的迭代过程中,我们在发送前一个项目后立即移动到下一个项目

    此外,为了获得更好的吞吐量,
    拆分器
    支持流媒体的
    迭代器

    我本来打算为您的任务推荐
    文件拆分器
    ,但我想您不是按行拆分,而是按其他标识符拆分。也许您的内容只是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)" />