View Javadoc
1   /*
2    *    Copyright 2016-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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  }