2

I have a issue here. When I am using this as my code (below) in spring batch configuration, my code ends successfully.

@Bean(name = "myStep")
public Step myStep() {
    int cores = Runtime.getRuntime().availableProcessors(); 
    int maxAndQueueSize = cores * 2;
    return stepBuilderFactory.get("myStep").<A, B> chunk(CHUNKS)
            .reader(myItemReader(entityManagerFactory)).processor(myProcessor())
            .writer(myWriter()).listener(myListener()).throttleLimit(maxAndQueueSize).allowStartIfComplete(true).build();
}

But when I modify this code (for async writing) by adding taskExecutor(asyncTaskExecutor()), my aim is fulfilled but code is still running in the eclipse. Seems its a thread closing related issue. Please help how can I close my code gracefully?

@Bean(name = "myStep")
public Step myStep() {
    int cores = Runtime.getRuntime().availableProcessors(); 
    int maxAndQueueSize = cores * 2;
    return stepBuilderFactory.get("myStep").<A, B> chunk(CHUNKS)
            .reader(myItemReader(entityManagerFactory)).processor(myProcessor())
            .writer(myWriter()).listener(myListener()).taskExecutor(asyncTaskExecutor()).throttleLimit(maxAndQueueSize).allowStartIfComplete(true).build();
}

This is the asyncTaskExecutor()

@Bean(name = "asyncTaskExecutor")
public AsyncTaskExecutor asyncTaskExecutor() {
    int cores = Runtime.getRuntime().availableProcessors(); 
    int maxAndQueueSize = cores * 2;
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(cores);
    executor.setMaxPoolSize(maxAndQueueSize);
    executor.setQueueCapacity(maxAndQueueSize);
    executor.setThreadNamePrefix("asyncExecutor-");
    executor.initialize();
    return executor;
}
Shashank
  • 712
  • 15
  • 33

0 Answers0