1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.springbatch.common;
17
18 import org.springframework.batch.core.annotation.BeforeChunk;
19 import org.springframework.batch.core.annotation.BeforeStep;
20 import org.springframework.batch.core.step.StepExecution;
21 import org.springframework.batch.infrastructure.item.Chunk;
22 import org.springframework.batch.infrastructure.item.ExecutionContext;
23 import org.springframework.batch.infrastructure.item.ItemProcessor;
24 import org.springframework.stereotype.Component;
25
26 @Component
27 public class PersonProcessor implements ItemProcessor<PersonRecord, PersonRecord> {
28
29 private ExecutionContext executionContext;
30
31 @Override
32 public PersonRecord process(PersonRecord person) {
33 incrementRowCount();
34
35 return new PersonRecord(person.id(), person.firstName().toUpperCase(), person.lastName().toUpperCase());
36 }
37
38 @BeforeStep
39 public void beforeStep(StepExecution stepExecution) {
40 executionContext = stepExecution.getExecutionContext();
41 }
42
43 @BeforeChunk
44 public void beforeChunk(Chunk<PersonRecord> chunk) {
45 incrementChunkCount();
46 }
47
48 private void incrementRowCount() {
49 executionContext.putInt("row_count",
50 executionContext.getInt("row_count", 0) + 1);
51 }
52
53 private void incrementChunkCount() {
54 executionContext.putInt("chunk_count",
55 executionContext.getInt("chunk_count", 0) + 1);
56 }
57 }