* fifth Batch 상태 저장 금지 close #6
This commit is contained in:
@@ -6,6 +6,7 @@ import org.apache.poi.ss.usermodel.Sheet;
|
|||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
import org.springframework.batch.item.Chunk;
|
import org.springframework.batch.item.Chunk;
|
||||||
|
import org.springframework.batch.item.ExecutionContext;
|
||||||
import org.springframework.batch.item.ItemStreamException;
|
import org.springframework.batch.item.ItemStreamException;
|
||||||
import org.springframework.batch.item.ItemStreamWriter;
|
import org.springframework.batch.item.ItemStreamWriter;
|
||||||
|
|
||||||
@@ -17,16 +18,16 @@ public class ExcelRowWriter implements ItemStreamWriter<BeforeEntity> {
|
|||||||
private final String filePath;
|
private final String filePath;
|
||||||
private Workbook workbook;
|
private Workbook workbook;
|
||||||
private Sheet sheet;
|
private Sheet sheet;
|
||||||
private int rowIndex = 0;
|
private int currentRowNumber;
|
||||||
|
|
||||||
public ExcelRowWriter(String filePath) throws IOException {
|
public ExcelRowWriter(String filePath) throws IOException {
|
||||||
|
|
||||||
this.filePath = filePath;
|
this.filePath = filePath;
|
||||||
initialize();
|
currentRowNumber = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initialize() throws ItemStreamException {
|
@Override
|
||||||
|
public void open(ExecutionContext executionContext) throws ItemStreamException {
|
||||||
workbook = new XSSFWorkbook();
|
workbook = new XSSFWorkbook();
|
||||||
sheet = workbook.createSheet("Sheet1");
|
sheet = workbook.createSheet("Sheet1");
|
||||||
}
|
}
|
||||||
@@ -34,7 +35,7 @@ public class ExcelRowWriter implements ItemStreamWriter<BeforeEntity> {
|
|||||||
@Override
|
@Override
|
||||||
public void write(Chunk<? extends BeforeEntity> chunk) {
|
public void write(Chunk<? extends BeforeEntity> chunk) {
|
||||||
for (BeforeEntity entity : chunk) {
|
for (BeforeEntity entity : chunk) {
|
||||||
Row row = sheet.createRow(rowIndex++);
|
Row row = sheet.createRow(currentRowNumber++);
|
||||||
row.createCell(0).setCellValue(entity.getUsername());
|
row.createCell(0).setCellValue(entity.getUsername());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,10 +50,9 @@ public class ExcelRowWriter implements ItemStreamWriter<BeforeEntity> {
|
|||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
workbook.close();
|
workbook.close();
|
||||||
rowIndex = 0;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ItemStreamException(e);
|
throw new ItemStreamException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,13 +58,18 @@ public class FifthBatch {
|
|||||||
@Bean
|
@Bean
|
||||||
public RepositoryItemReader<BeforeEntity> fifthBeforeReader() {
|
public RepositoryItemReader<BeforeEntity> fifthBeforeReader() {
|
||||||
|
|
||||||
return new RepositoryItemReaderBuilder<BeforeEntity>()
|
RepositoryItemReader<BeforeEntity> reader = new RepositoryItemReaderBuilder<BeforeEntity>()
|
||||||
.name("beforeReader")
|
.name("beforeReader")
|
||||||
.pageSize(10)
|
.pageSize(10)
|
||||||
.methodName("findAll")
|
.methodName("findAll")
|
||||||
.repository(beforeRepository)
|
.repository(beforeRepository)
|
||||||
.sorts(Map.of("id", Sort.Direction.ASC))
|
.sorts(Map.of("id", Sort.Direction.ASC))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
// 전체 데이터 셋에서 어디까지 수행 했는지의 값을 저장하지 않음
|
||||||
|
reader.setSaveState(false);
|
||||||
|
|
||||||
|
return reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
Reference in New Issue
Block a user