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;
}
}
Author by
ulquiorra
Updated on June 04, 2022Comments
-
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 over 9 yearsThank 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 over 9 yearsUpdate your custom writter and add the resource property with the corresponding setter
-
ulquiorra over 9 yearsThanks 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 over 9 yearsSeems you are missing the scope="step" in the bean definition
-
ulquiorra over 9 yearsIt 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 over 9 yearsI've updated the response with the writer, inject the property
resource
instead offlatFileItemWriter.resource
-
ulquiorra over 9 yearsThank you very much , i really appreciate your help and patience . I will test this by monday :)
-
ulquiorra over 9 yearsIt 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 almost 7 yearstried above it is throwing java.lang.IllegalStateException: Cannot bind to partial key %{paramBean[outputFileName]} exception...