Results 1 to 3 of 3

Thread: getting the resource name from a custom item writer

  1. #1
    Join Date
    Jan 2009
    Location
    PH, SG
    Posts
    3

    Default getting the resource name from a custom item writer

    Hello,

    I am new to Spring Batch. My issue may sound so simple.

    How do I get the resoure in my custom FlatFileItemWriter? FlatFileItemWriter doesn't have a method that returns the resource. I need to get the resource to do further processing to the output file (appending a footer) in the afterStep() method. Calling the write(<footer>) in the afterStep() doesn't work so I'll just write my own code to append a footer in the file. In this case, I would need to get the resource.

    Any help or suggestion I'll appreaciate.

    Thanks.

  2. #2
    Join Date
    Jun 2008
    Location
    NYC, NY, US
    Posts
    29

    Default

    I don't know if it's the best way to do it, but one can certainly inject the resource name into the writer from the spring config. You can also inject the resource name into a composite writer and have that invoke your FileFileWriter and whatever handles the footer record append.

    A final option is to squirrel the information away in the contribution of the step if you're reading from multiple resources.

    I could have swore I saw something about handling footer records, specifically, in the batch docs, but I could be wrong.

    Hope that helps.

  3. #3
    Join Date
    Feb 2008
    Posts
    488

    Default

    One way to "add" a getter is to extend the FFIW and have a resource property on the subclass. Then, the subclass's set method can pass the given resource up to the superclass. This way the same Resource object exists in both places.

    Code:
    public class MyFFIW extends FlatFileItemWriter {
        private Resource resource;
        
        public getResource(){
            return this.resource; 
        }
        
        public setResource(Resource resource){
            this.resource = resource;
            super.setResource(resource);
        }
    }

Posting Permissions

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