version,當取得資源時,不上鎖,但在 version欄位標記,最後commit上去時先看看version是否一致,若不一致,表示這期間有人動過資料,此時就不更新,反之則更新。AtLeastFor、AtMostFor 來限制這個排程鎖占用的最大時間跟最小時間。@Scheduled(cron = "0 */1 * * * ?")
@SchedulerLock(
name = "expiredOrderSchedulingLock",
lockAtMostFor = "50s",
lockAtLeastFor = "10s"
)
public void expiredOrderScheduling(){
// ...省略
}
i、BATCH_SIZE 來計算當前batch。for (int i=0; i<orderList.size(); i+=BATCH_SIZE){
List<Order> ordersubList = new ArrayList<>(orderList.subList(i, Math.min(i+BATCH_SIZE, orderList.size())));
try {
canceledCount += orderCancelService.expiredOrderScheduling(ordersubList);
}catch (Exception e){
log.error("Failed to cancel batch {}", (i / BATCH_SIZE)+1, e);
}
}
本篇文章出自《每天學Java直到今年結束》Day224,大家可以到我的網站上查看~