传递作业参数到自定义编写器Spring批处理

时间:2014-11-21 15:31:48

标签: spring parameters spring-batch

我有一个带有FlatFileItemWriter的自定义编写器,我想传递一个在主类中定义的作业参数(输出文件) 我怎么处理这个? 非常感谢你

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

    }

启动

            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

配置作业xml

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

2 个答案:

答案 0 :(得分:8)

您必须使用步骤范围作业范围声明bean,以便根据作业参数对属性进行后期绑定:

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

默认情况下,这些范围不可用,您需要通过使用batch命名空间或定义以下bean来包含它们:

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

更新

这是完整的作家:

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

答案 1 :(得分:0)

您可以定义HashMap并使用此HashMap代替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>

ItemWriter中写下setter方法,并在HashMap中设置值。

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