ItemReaderbatchSize 설정에 따라 다음 데이터가 조회된다.MongoCursorItemReader의 최적화
read() 메서드 호출 시 내부 버퍼를 활용해 비어있을 때만 DB에 요청batchSize만큼의 도큐먼트를 확보MongoCursorItemReader
│
├────── MongoTemplate
│ └─ (MongoDB 작업의 핵심 엔진)
│
├────── Query (org.springframework.data.mongodb.core.query.Query)
│ └─ (MongoDB 쿼리 조건 정의)
│
└────── Cursor
└─ (MongoDB 데이터를 순차적으로 읽어오는 스트리밍 객체)
MongoTemplate
MongoTemplate으로 데이터를 스트리밍으로 조회한다.Query
doOpen()에 최초로 쿼리를 생성, 커서를 얻는다.Cursor
read() 메서드가 호출될 때마다 데이터를 순차적으로 반환MongoCursorItemReader 처리 절차
read() 호출Document 반환read() 호출Document 요청 → Document 일괄 전송 (batchSize 단위)Document 반환@Bean
@StepScope
public MongoCursorItemReader<SecurityLog> securityLogReader(
@Value("#{jobParameters['searchDate']}") LocalDate searchDate
) {
Date startOfDay = Date.from(searchDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
Date endOfDay = Date.from(searchDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
return new MongoCursorItemReaderBuilder<SecurityLog>()
.name("securityLogReader")
.template(mongoTemplate)
.collection("security_logs")
.jsonQuery("""
{
"label": "PENDING_ANALYSIS",
"timestamp": {
"$gte": ?0,
"$lt": ?1
}
}
""")
.parameterValues(List.of(startOfDay, endOfDay))
.sorts(Map.of("timestamp", Sort.Direction.ASC))
.targetType(SecurityLog.class)
.batchSize(10)
.build();
}