pass job parameters to custom writer Spring batch

ulquiorra

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> 
Sergi Almar

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Spring Batch Writer

分類Dev

Spring Batch Item Writer Listener not Working

分類Dev

Spring Batch Item Writer Listener not Working

分類Dev

Seperate datasource for jobrepository and writer of Spring Batch

分類Dev

Spring Batch : custom ItemReader

分類Dev

Disable transactions in my Spring Batch job

分類Dev

Testing of spring batch job causes unexpected results

分類Dev

Spring Batch Persist Job Meta Data

分類Dev

NoSuchJobException when running a job programmatically in Spring Batch

分類Dev

Spring Batch job execution status in response body

分類Dev

Can I use Spring JdbcTemplate in Spring Batch Job Implementation?

分類Dev

How to pass parameters to a PS script invoked through Start-Job?

分類Dev

Spring batch custom DB schema init

分類Dev

Spring batch return custom process exit code

分類Dev

Pass custom object into Spring Boot Controller

分類Dev

Is there a way to pass parameters to a custom Service Behavior through configuration

分類Dev

無限ループで実行されているSpring Batch Job

分類Dev

プログラムでSpring Batch Jobを実行しますか?

分類Dev

How to access execution context from a Spring Batch Step? Error: No context holder available for job scope

分類Dev

Does every job call in Spring Batch open a new database connection pool?

分類Dev

Deadlock while purging old records of spring batch job tables at scheduled time using shedlock

分類Dev

Spring Batchは常にSpring Bootで始まります:spring.batch.job.enabled = false動作しません

分類Dev

How to pass custom ContractConverter to spring cloud contract docker image

分類Dev

Spring XD: pipe (>) from file source to batch job fails (IllegalArgumentException: Unable to convert provided JSON to Map<String, Object>)

分類Dev

How to update jsp status (from submitted to processing then success )if a spring batch job runing in background that will update a sql result status

分類Dev

Batch: checking commandline parameters in batch file

分類Dev

pass optional parameters to require()

分類Dev

Orchard CMS custom background job

分類Dev

Can you custom name a job?

Related 関連記事

  1. 1

    Spring Batch Writer

  2. 2

    Spring Batch Item Writer Listener not Working

  3. 3

    Spring Batch Item Writer Listener not Working

  4. 4

    Seperate datasource for jobrepository and writer of Spring Batch

  5. 5

    Spring Batch : custom ItemReader

  6. 6

    Disable transactions in my Spring Batch job

  7. 7

    Testing of spring batch job causes unexpected results

  8. 8

    Spring Batch Persist Job Meta Data

  9. 9

    NoSuchJobException when running a job programmatically in Spring Batch

  10. 10

    Spring Batch job execution status in response body

  11. 11

    Can I use Spring JdbcTemplate in Spring Batch Job Implementation?

  12. 12

    How to pass parameters to a PS script invoked through Start-Job?

  13. 13

    Spring batch custom DB schema init

  14. 14

    Spring batch return custom process exit code

  15. 15

    Pass custom object into Spring Boot Controller

  16. 16

    Is there a way to pass parameters to a custom Service Behavior through configuration

  17. 17

    無限ループで実行されているSpring Batch Job

  18. 18

    プログラムでSpring Batch Jobを実行しますか?

  19. 19

    How to access execution context from a Spring Batch Step? Error: No context holder available for job scope

  20. 20

    Does every job call in Spring Batch open a new database connection pool?

  21. 21

    Deadlock while purging old records of spring batch job tables at scheduled time using shedlock

  22. 22

    Spring Batchは常にSpring Bootで始まります:spring.batch.job.enabled = false動作しません

  23. 23

    How to pass custom ContractConverter to spring cloud contract docker image

  24. 24

    Spring XD: pipe (>) from file source to batch job fails (IllegalArgumentException: Unable to convert provided JSON to Map<String, Object>)

  25. 25

    How to update jsp status (from submitted to processing then success )if a spring batch job runing in background that will update a sql result status

  26. 26

    Batch: checking commandline parameters in batch file

  27. 27

    pass optional parameters to require()

  28. 28

    Orchard CMS custom background job

  29. 29

    Can you custom name a job?

ホットタグ

アーカイブ