pass job parameters to custom writer Spring batch

19,854

You have to declare the bean with either step scope or job scope so you can have late binding of a property based on the job parameter:

<bean id="personWriter" class="com.dev.writer.PersonItemWriter" scope="step">
    <property name="resource" value="#{jobParameters[outputFileName]}" />
</bean>

These scopes are not available by default, you need to include them either by either using the batch namespace or defining the following bean:

<bean class="org.springframework.batch.core.scope.StepScope" />

Update:

Here's the complete writer:

public class PersonItemWriter implements ItemWriter<Person> {

    FlatFileItemWriter<String> flatFileItemWriter = new FlatFileItemWriter<String>();
    private Resource resource;

    @Override
    public void write(List<? extends Person> personList) throws Exception {

            flatFileItemWriter.setResource(resource);// how the pass the job parameter file here
            PassThroughLineAggregator<String> aggregator = new PassThroughLineAggregator<String();
            flatFileItemWriter.setLineAggregator(aggregator);
            aggregator.aggregate("test"); // do not save in output file
    }

    public FlatFileItemWriter<String> getFlatFileItemWriter() {
        return flatFileItemWriter;
    }

    public void setFlatFileItemWriter(FlatFileItemWriter<String> flatFileItemWriter) {
        this.flatFileItemWriter = flatFileItemWriter;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }
}
Share:
19,854
ulquiorra
Author by

ulquiorra

Updated on June 04, 2022

Comments

  • ulquiorra
    ulquiorra almost 2 years

    I have a custom writer with a FlatFileItemWriter and i want to pass a job parameter( a output file) defined in the main class How can i deal with this ? Thank you very much

    CustomWriter

     public class PersonItemWriter implements ItemWriter<Person> {
    
       private FlatFileItemWriter<String> flatFileItemWriter = new FlatFileItemWriter<String>();
       private Resource resource;
    
        @Override
        public void write(List<? extends Person> personList) throws Exception {
    
                flatFileItemWriter.setResource(new FileSystemResource(resource.getFile()));
                PassThroughLineAggregator<String> aggregator = new PassThroughLineAggregator<String();
                flatFileItemWriter.setLineAggregator(aggregator); 
                flatFileItemWriter.open(new ExecutionContext());             
                flatFileItemWriter.write(Arrays.asList(aggregator.aggregate("test")));
                flatFileItemWriter.close();
    }
    
    
       public void setResource(Resource resource) {
           this.resource = resource;
       }
    
        }
    

    Launcher

                JobLauncher jobLauncher = (JobLauncher) applicationContext.getBean("jobLauncher");
                Job job = (Job) applicationContext.getBean("personJob");
                /* Parameters sent to job */
                JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    
                jobParametersBuilder.addString("outputFileName", "file:" + personFile); // pass this to the itemWriter
    

    configuration job xml

        <bean id="personWriter" class="com.dev.writer.PersonItemWriter"  scope="step>
        <property name="resource" value="#{jobParameters[outputFileName]}" />
    </bean> 
    
  • ulquiorra
    ulquiorra over 9 years
    Thank you very much but your answer works for a default writer not my customized one because of the resource property ( i got the error "Invalid property 'resource' of bean class [com.dev.writer.PersonItemWriter]: Bean property 'resource' is not writable... ")
  • Sergi Almar
    Sergi Almar over 9 years
    Update your custom writter and add the resource property with the corresponding setter
  • ulquiorra
    ulquiorra over 9 years
    Thanks i have updated my code but it does not save into the outputFile. When i check in debug mode , the flatFileItemWriter properties contains a resource of type URLResource which contains("file:C:/temps/outPutFolder.txt"). Do i miss something ? Thank you very much
  • Sergi Almar
    Sergi Almar over 9 years
    Seems you are missing the scope="step" in the bean definition
  • ulquiorra
    ulquiorra over 9 years
    It was in my code. I just forget to put it here sorry . but i still have the issue . probably because the file is considered as URLResource ? Thank you
  • Sergi Almar
    Sergi Almar over 9 years
    I've updated the response with the writer, inject the property resource instead of flatFileItemWriter.resource
  • ulquiorra
    ulquiorra over 9 years
    Thank you very much , i really appreciate your help and patience . I will test this by monday :)
  • ulquiorra
    ulquiorra over 9 years
    It works thanks . However i needed to add flatFileItemWriter.open() and flatFileItemWriter.write() in order to write in my file. I updated my code. Thank you very much :)
  • user1912935
    user1912935 almost 7 years
    tried above it is throwing java.lang.IllegalStateException: Cannot bind to partial key %{paramBean[outputFileName]} exception...