Java Spring集成复制文件,复制后通过http读取并发送数据

Java Spring集成复制文件,复制后通过http读取并发送数据,java,spring-integration,Java,Spring Integration,我是spring integration的新手,我想每5秒做几步: 检查目录是否有扩展名为.xml或.json的文件 如果有以前扩展名的文件,我想将单个文件复制到其他目录中(如果有更多文件,我想稍后复制它们,它应该像循环一样工作:获取第一个文件做某事,返回,获取第二个文件等) 将单个文件复制到目录后,我想读取该文件,并将该文件中的数据以字符串格式发送到我的rest应用程序中 我的rest应用程序将处理此数据并给出响应 在最后一步中,我想打印来自http的给定响应 为了达到这个目的,我准备了如下代

我是spring integration的新手,我想每5秒做几步:

  • 检查目录是否有扩展名为.xml或.json的文件
  • 如果有以前扩展名的文件,我想将单个文件复制到其他目录中(如果有更多文件,我想稍后复制它们,它应该像循环一样工作:获取第一个文件做某事,返回,获取第二个文件等)
  • 将单个文件复制到目录后,我想读取该文件,并将该文件中的数据以字符串格式发送到我的rest应用程序中
  • 我的rest应用程序将处理此数据并给出响应
  • 在最后一步中,我想打印来自http的给定响应
  • 为了达到这个目的,我准备了如下代码:

    <file:inbound-channel-adapter id="filesIn" directory="file:${java.io.tmpdir}/spring-integration-samples/input"
                                  filename-regex="^.*\.(xml|json)$">
        <int:poller id="poller" fixed-delay="5000"/>
    </file:inbound-channel-adapter>
    
    <int:service-activator input-channel="filesIn"
                           output-channel="filesOut"
                           ref="handler"/>
    
    <file:outbound-channel-adapter id="filesOut" directory="file:${java.io.tmpdir}/spring-integration-samples/output"
                                   delete-source-files="true"/>
    
    <int:gateway id="requestGateway"
                 service-interface="com.integration.service.RequestService"
                 default-request-channel="requestChannel" >
    </int:gateway>
    
    
    <int:channel id="requestChannel"/>
    
    <int-http:outbound-gateway request-channel="requestChannel"
                               url="http://localhost:8080/import/v1/documents/go"
                               http-method="POST"
                               expected-response-type="org.springframework.http.ResponseEntity"                             >
    
    </int-http:outbound-gateway>
    
    RequestService requestService = (RequestService)context.getBean("requestGateway");
    JSONObject obj = new JSONObject();
    obj.put("docID", "8");
    obj.put("subject", "111111");
    obj.put("content", "ssssss");
    obj.put("type", "ddddd");
    String doc = obj.toString();
    ResponseEntity reply = requestService.echo(doc);
    
    之后,该文件将被复制到其他目录中

    关键是在将文件复制到新目录后,我如何读取该文件并通过HTTPPOST方法发送数据

    请大家给我举一些例子如何做到这一点。我发现这样的事情:

    <file:inbound-channel-adapter id="filesIn" directory="file:${java.io.tmpdir}/spring-integration-samples/input"
                                  filename-regex="^.*\.(xml|json)$">
        <int:poller id="poller" fixed-delay="5000"/>
    </file:inbound-channel-adapter>
    
    <int:service-activator input-channel="filesIn"
                           output-channel="filesOut"
                           ref="handler"/>
    
    <file:outbound-channel-adapter id="filesOut" directory="file:${java.io.tmpdir}/spring-integration-samples/output"
                                   delete-source-files="true"/>
    
    <int:gateway id="requestGateway"
                 service-interface="com.integration.service.RequestService"
                 default-request-channel="requestChannel" >
    </int:gateway>
    
    
    <int:channel id="requestChannel"/>
    
    <int-http:outbound-gateway request-channel="requestChannel"
                               url="http://localhost:8080/import/v1/documents/go"
                               http-method="POST"
                               expected-response-type="org.springframework.http.ResponseEntity"                             >
    
    </int-http:outbound-gateway>
    
    RequestService requestService = (RequestService)context.getBean("requestGateway");
    JSONObject obj = new JSONObject();
    obj.put("docID", "8");
    obj.put("subject", "111111");
    obj.put("content", "ssssss");
    obj.put("type", "ddddd");
    String doc = obj.toString();
    ResponseEntity reply = requestService.echo(doc);
    
    这是我的RequestServce接口:

    public interface RequestService {
        ResponseEntity echo(String request);
    }
    

    我将非常感谢您的帮助。

    我不知道您为什么要先复制文件;只要您不使用任何队列或执行器通道,整个流将在轮询器线程上运行,并且在处理第一个文件之前,您将无法获得下一个文件


    如果出于某种原因必须复制文件,请添加第二个文件入站通道适配器以侦听第二个目录;然后是一个转换器(例如文件到字符串的转换器);然后是出站http网关。

    我又添加了一个入站通道适配器来检查dist目录,然后进行了转换和其他操作。但不,我正在尝试将此代码更改为Java类配置。例如,我如何执行复制任务@GaryRussellIt最好问一个新问题,显示您当前的配置,有人可以帮助将其转换为Java配置。