使用AsyncItemProcessor和AsyncItemWriter

时间:2018-08-31 13:07:55

标签: spring spring-boot asynchronous spring-integration spring-batch

当我尝试使用AsyncItemProcessor和AsyncItemWriter时,编写者似乎什么也不做。处理器处理完所有记录并将其放入块后,编写器将不执行任何操作,连接将超时。下面是我的配置。

@Configuration
@EnableAutoConfiguration
public class EmployeeJobConfiguration {

    @Autowired
    private EmployeeService employeeService;

    /**
     * Default Constructor
     */
    public EmployeeJobConfiguration() {
        super();
    }

    @Bean
    public Job employeeJob() throws Exception {
        return jobBuilderFactory.get("employeeJob")
                .start(employeeJobStep1())
                .listener(executionListener())
                .build();
    }


    @SuppressWarnings("unchecked")
    @Bean
    public Step employeeJobStep1() throws Exception {
        return ((SimpleStepBuilder<Employee, EmployeeResult>) stepBuilderFactory.get("step1")
                .allowStartIfComplete(allowRestart)
                .<Employee, Future<EmployeeResult>>chunk(chunkSize)
                .reader(employeeJobReader())
                .processor(asyncItemProcessor())
                .writer(asyncItemWriter())
                .build();
    }

    @StepScope
    @Bean
    public ItemReader<Employee> employeeJobReader(){
        RepositoryItemReader<Employee> reader = new RepositoryItemReader<>();
        reader.setRepository(employeeService.getRepository());
        reader.setPageSize(chunkSize);
        reader.setMethodName("findAll");
        reader.setSort(getSortMap(ASC, "employeeID"));
        return reader;
    }


    @Bean
    public ItemProcessor<Employee, Future<EmployeeResult>> asyncItemProcessor() throws Exception{
        AsyncItemProcessor<Employee, EmployeeResult> asyncItemProcessor = new AsyncItemProcessor<>();
        asyncItemProcessor.setDelegate(employeeProcessor());
        asyncItemProcessor.setTaskExecutor(getAsyncExecutor());
        asyncItemProcessor.afterPropertiesSet();
        return asyncItemProcessor;
    }


     /**
     * Job Processor Method
     * @return
     */
    @Bean
    public ItemProcessor<Employee, EmployeeResult> employeeProcessor() {
        return new EmployeeProcessor();
    }

    @Bean(name = "asyncExecutor")
    public TaskExecutor getAsyncExecutor()
    {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(10);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("AsyncExecutor-");
        return executor;
    }



    @Bean
    public ItemWriter<Future<EmployeeResult>> asyncItemWriter() throws Exception{
        AsyncItemWriter<EmployeeResult> asyncItemWriter = new AsyncItemWriter<>();
        asyncItemWriter.setDelegate(employeeJobWriter());
        asyncItemWriter.afterPropertiesSet();
        return asyncItemWriter;
    }

    @Bean
    public ItemWriter<EmployeeResult> employeeJobWriter() {
        return new EmployeeJobWriter();
    }

我遇到以下异常。

2018-08-31 14:00:36错误[AsyncExecutor-1] cpcibeEmployeeErrorHandler-异常[EclipseLink-4002](Eclipse Persistence Services-2.6.4.v20160829-44060b6):org.eclipse.persistence.exceptions。 DatabaseException

内部异常:java.sql.SQLTransientConnectionException:springHikariCP-连接不可用,请求在30000ms之后超时。 错误代码:0 查询:ReadObjectQuery(referenceClass = ProcessError) org.eclipse.persistence.exceptions.DatabaseException: 内部异常:java.sql.SQLTransientConnectionException:springHikariCP-连接不可用,请求在30000ms后超时。 错误代码:0

1 个答案:

答案 0 :(得分:1)

使用FutureAsyncItemProcessor时不必处理AsyncItemWriter类型。这些组件将透明地处理异步处理/写入,因此,作为最终用户,您可以将它们用作常规处理器/写入器,而不必处理低级并发构造。

例如:

@Bean
public ItemProcessor<Employee, EmployeeResult> asyncItemProcessor() throws Exception{
    AsyncItemProcessor<Employee, EmployeeResult> asyncItemProcessor = new AsyncItemProcessor<>();
    asyncItemProcessor.setDelegate(employeeProcessor());
    asyncItemProcessor.setTaskExecutor(getAsyncExecutor());
    return asyncItemProcessor;
}

@Bean
public ItemWriter<EmployeeResult> asyncItemWriter() throws Exception{
    AsyncItemWriter<EmployeeResult> asyncItemWriter = new AsyncItemWriter<>();
    asyncItemWriter.setDelegate(employeeJobWriter());
    return asyncItemWriter;
}

@Bean
public Step employeeJobStep1() throws Exception {
    return stepBuilderFactory.get("step1")
            .allowStartIfComplete(allowRestart)
            .<Employee, EmployeeResult>chunk(chunkSize)
            .reader(employeeJobReader())
            .processor(asyncItemProcessor())
            .writer(asyncItemWriter())
            .build();
}