I am currently using Spring Batch. I created Reader
, Writer
and a Processor
. The Reader is a basic Custom ListItemReader
.
public class CustomListItemReader<T> implements ItemReader<T> {
private List<T> list;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
log.debug("Set list of size {}", list.size());
if (AopUtils.isAopProxy(list)) {
this.list = list;
} else {
this.list = new ArrayList<T>(list);
}
}
@Override
public synchronized T read() {
log.info("Inside custom list item reader");
if (list != null && !list.isEmpty()) {
log.info("Inside read not empty");
T remove = list.remove(0);
while (remove == null && !list.isEmpty()) {
remove = list.remove(0);
}
return remove;
}
return null;
}
}
I tried testing Spring batch with and without a taskExecutor
. Without the taskExecutor
the
Inside custom list item reader
log gets printed twice. I get that, it is printed once for the actual job and once to check whether any inputs exists or not. When the reader returns null
, the job completes and stops. That's fine , but when I do the same with a taskExecutor
with a configuration as shown below
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setMaxPoolSize(1);
taskExecutor.setCorePoolSize(1);
taskExecutor.setQueueCapacity(1);
taskExecutor.afterPropertiesSet();
return taskExecutor;
}
and I even set the throttle-limit
to 1. I assumed that the above taskExecutor
mimics the single thread scenario. And since the there is only one active thread and throttle-limit
= 1 , the log would get printed twice, same as in the previous configuration. But the message gets logged thrice.
Why is there an extra log printed? Hows does the task count get increased by 1?
Also, just for the sake of experimenting I kept the throttle-limit
to 20 and the corePoolSize
, maxPoolSize
and queueCapacity
to 1 . The job doesn't end at all.
and I get an exception:
java.util.concurrent.RejectedExecutionException: Task com.esewa.settlementswitch.transaction.cooperative.BatchConfig$ClientSettlementTaskDecorator$$Lambda$1111/698696362@4d3e6424 rejected from org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor$1@60e29dbd[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0]
I know that the job was Rejected because the pool size is 1 and the queue is also full and no new tasks can be submitted. But the question is why did so many tasks start ?