pass job parameters to custom writer Spring batch

ⅰ亾dé卋堺 提交于 2021-02-08 03:46:24

问题


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> 

回答1:


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;
    }
}



回答2:


You can define a HashMap and use this HashMap instead of jobParameter.

<bean id="paramBean" class="java.util.HashMap"/>

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

Write the setter method in ItemWriter and set the values in the HashMap.

private HashMap paramBean;
public void setParamBean(HashMap paramBean) {
        this.paramBean= paramBean;
    }
paramBean.set(<key>,<value>);


来源:https://stackoverflow.com/questions/27064832/pass-job-parameters-to-custom-writer-spring-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!