Results 1 to 5 of 5

Thread: Renaming files one at a time with MultiResourceItemReader

  1. #1
    Join Date
    Aug 2008
    Posts
    3

    Default Renaming files one at a time with MultiResourceItemReader

    Hi all,

    Do anybody know how to rename files one at a time when using a MultiResourceItemReader. I mean, I want to process all the files in a specific directory but I want to rename each file when it has been processed.

    Directory content example:
    File1.xml
    File2.xml
    File3.xml

    After the first file has been processed:
    File1.xml.processed
    File2.xml
    File3.xml

    Is it possible to do that?

    Thanks for your help!

  2. #2
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    The only way I can think to do that would be to flag the files after the step is complete, using a StepExecutionListener. I can't think of a good way to do that while the step is still executing though.

  3. #3

    Default

    Hi Lucas

    So you mean in the afterStepexecution method, we flag the files ? And how do we access the file names from the stepexecution ? Is it by injecting the resources into that class ?

    And also, because we are using MultiResourceReader, how can we reliably flag which file that has been processed ? Do you mean just flag all the files within that directory in the afterStepexecution method?.

  4. #4
    Join Date
    Feb 2008
    Posts
    488

    Default

    ballistic_realm,

    The way I've done it in the past is this:

    Create a wrapper class for your FlatFileItemReader. The wrapper can be used by itself, or set as the delegate of the MultiResourceItemReader.

    You'll have to store the resource in the wrapper:

    Code:
    public void setResource(Resource resource) {
    	this.resource = resource;
    	this.delegateReader.setResource(resource);
    }
    The wrapper's read() will do the following:

    Code:
    public Object read() throws Exception {
    	Object o = this.delegateReader.read();
    	if (o == null) {
    		//
    		// After the last item is read
    		//
    		this.renameResource(this.resource);
    	}
    	return o;
    }
    If you want to do the renaming afterwards, then you'll store the resource to a list in read(), implement StepExecutionListener, and perform the rename in afterStep().

    If you want to do the renaming as a separate step, then you'll store the resource to a list in the stepExecution.getJobExecution().getExecutionContex t() in afterStep(). That way you can access it in a later step.
    Last edited by DHGarrette; Jan 15th, 2009 at 11:22 AM.

  5. #5

    Default

    Hi DHGarrette

    your solution works perfectly

    thanks for the input

    regards

    ballistic_realm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •