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

  • Ensure that a file is written before continue processing

    Hi,

    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
    Hi,

    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.notNull(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()) {
    inputFile.delete();
    }

    // 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>();
    newMsgHeaders.put("source","original-directory");
    outputMessage = MessageBuilder.withPayload(outputFile).copyHeaders (newMsgHeaders).build();

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

    Hth,
    - Sjoerd

    Comment


    • #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.

      Comment


      • #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.

        Comment


        • #5
          Please provide feedback on these two issues in JIRA:
          http://jira.springframework.org/browse/INT-606
          http://jira.springframework.org/browse/INT-666

          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.

          Thanks,
          Mark

          Comment

          Working...
          X