How to do it...

Let's implement a non-blocking batch process by following these steps:

  1. Create a Spring Boot 2.0 project, ch11-batch-async, that has the same starter POM dependencies with the same MySQL connection pool support and Spring OXM module.
  2. Create a bootstrap class that enables batch processing and task scheduling:
@EnableBatchProcessing 
@SpringBootApplication 
@EnableScheduling 
public class AsyncBatchBootApplication { 
    
 public static void main(String[] args) throws Exception { 
     SpringApplication.run(AsyncBatchBootApplication.class, 
 args); 
 } 
} 
  1. In its srcmain/ esources directory, create application.properties that contain the same configuration as the one in ch11-batch-sync. Just modify some server-related configurations.
  2. Copy logback.xml from the previous project and drop it inside srcmain esources to enable logging.
  3. Then, copy all the packages from the ch11-batch-sync project without any changes.
  4. Next, start the asynchronous batch processing configuration and inject the TaskExecutor that will generate the thread pool into BatchConfig:
@Bean("mvcTaskexecutor") 
     public TaskExecutor getAsyncExecutor() { 
         ConcurrentTaskExecutor executor =  
new ConcurrentTaskExecutor( 
                  Executors.newFixedThreadPool(100)); 
         executor.setTaskDecorator(new TaskDecorator() { 
            @Override 
            public Runnable decorate (Runnable runnable) { 
                return () -> { 
                    long t = System.currentTimeMillis(); 
                    runnable.run(); 
                    System.out.printf("Thread %s has a  
processing time:  %s%n",  
                           Thread.currentThread().getName(),        
                        (System.currentTimeMillis() - t)); 
                }; 
            } 
        }); 
       return executor; 
}
  1. Assign threads to taskletStep() and chunkStep() by passing the TaskExecutor bean to the taskExecutor() method of their respective StepBuildFactory instances.
  2. Save all files. Deploy the application with clean spring-boot:run Maven command. Expect random and messy writes to the output.txt output file.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.226.120