-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargeExcelReadBatchConfig.java
More file actions
69 lines (60 loc) · 2.46 KB
/
LargeExcelReadBatchConfig.java
File metadata and controls
69 lines (60 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.io.example.config;
import com.io.example.dto.StudentDto;
import com.io.example.reader.StreamingExcelItemReader;
import com.io.example.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;
import java.time.LocalDate;
@Configuration
@RequiredArgsConstructor
public class LargeExcelReadBatchConfig {
private final StudentService studentService;
@Bean
@StepScope
public StreamingExcelItemReader<StudentDto> largeExcelReader(
@Value("#{jobParameters['filePath']}") String filePath
) {
return new StreamingExcelItemReader<>(
new ClassPathResource(filePath),
row -> new StudentDto(
row.getCell(0).getStringCellValue(),
row.getCell(1).getStringCellValue(),
LocalDate.parse(row.getCell(2).getStringCellValue())
)
);
}
@Bean
public ItemProcessor<StudentDto, StudentDto> largeExcelProcessor() {
return student -> student;
}
@Bean
public ItemWriter<StudentDto> largeExcelWriter() {
return items -> items.forEach(studentService::print);
}
@Bean
public Step largeExcelStep(
JobRepository jobRepository,
PlatformTransactionManager transactionManager,
StreamingExcelItemReader<StudentDto> largeExcelReader,
ItemProcessor<StudentDto, StudentDto> largeExcelProcessor,
ItemWriter<StudentDto> largeExcelWriter,
@Value("${spring.batch.chunk-size}") int chunkSize
) {
return new StepBuilder("largeExcelStep", jobRepository)
.<StudentDto, StudentDto>chunk(chunkSize, transactionManager)
.reader(largeExcelReader)
.processor(largeExcelProcessor)
.writer(largeExcelWriter)
.build();
}
}