Announcement Announcement Module
No announcement yet.
Ensure that a file is written before continue processing Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ensure that a file is written before continue processing


    How can we ensure that a file is written before starting another process ?

    For example if I want to make a copy of a source file at the beginning of the message bus and wait while this copy is done because at the end of the message bus I delete the source file.

  • #2

    Maybe the following snippet of (proof-of-concept) code works for you:

    * Moves payload of the message to a Work In Process (WIP) directory.
    * Then informs the WIP channel that there is work to be done.
    public Message<?> moveFileToWip(Message<?> message) {
    Assert.isInstanceOf(File.class, message.getPayload(), "ohi-filehandler: 'message' is not a file");
    Message<?> outputMessage = null;
    String dir = "d:/temp/spring-integration/wip/";
    // Determine the file name
    // Alternatively: message.getHeaders().get(FileNameGenerator.FILENAM E_PROPERTY_KEY, String.class);
    FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
    String fileName = fileNameGenerator.generateFileName(message);

    // Move the file: copy it first ...
    FileWritingMessageConsumer fileWriter = new FileWritingMessageConsumer(dir + fileName);
    fileWriter.setFileNameGenerator(fileNameGenerator) ;
    // ... and then delete the original
    File inputFile = (File)message.getPayload();
    if (inputFile.exists()) {

    // Prepare the new value for use in the WIP channel
    File outputFile = new File(dir + File.separator + fileName);
    if (!outputFile.exists()) {
    throw new RuntimeException("Moving file to WIP failed: " + dir + File.separator + fileName);

    // Construct new message
    Map<String, Object> newMsgHeaders = new HashMap<String, Object>();
    outputMessage = MessageBuilder.withPayload(outputFile).copyHeaders (newMsgHeaders).build();

    System.out.println("Moved file to wip: " + dir + File.separator + fileName);
    return outputMessage;

    - Sjoerd


    • #3
      Thanks for your reply but I should have specify (sorry...) than I tried to do that only with an XML configuration.

      The issue that I face is that the file output channel adapter is (like?) an endpoint, it seems there is no way to know when its job is done.


      • #4
        On the input side you can use a FileFilter implementation to hold up files that shouldn't go through yet (because another process is still writing).
        The way we thought it would be was that an external writing process would ensure that the file isn't picked up before it is done.

        Then you have a few options:
        - use a Transformer to translate the file into a byte[] or a string and delete afterwards
        - send a File message around and let the writer take care of the copy. You could delete the files from the input based on a source on the output directory in this scenario.

        It might be nice to have an option to delete the original in the FileWritingMessageConsumer. You could create a request in JIRA for that if you want it.


        • #5
          Please provide feedback on these two issues in JIRA:

          The first is the one created based on this thread. The second is for creation of a FileCopyTransformer. If you read the comments on both, you will see that there are some common features. I'm very interested in any thoughts related to the two approaches.