使用AsyncItemProcessor和AsyncItemWriter扩展Spring批处理

使用AsyncItemProcessor和AsyncItemWriter扩展Spring批处理,spring,multithreading,spring-boot,spring-batch,Spring,Multithreading,Spring Boot,Spring Batch,我正在尝试编写一个解决方案来扩展spring批处理。在spring批处理中,它从MySQL数据库读取数据(600000),然后对其进行处理,并将每个处理行的状态更新为“已完成” 我正在使用AsyncItemProcessor和AsyncItemWriter来扩展spring批处理 问题: 如果我同步运行spring批处理,那么异步运行spring批处理所花费的时间相同或更少。我没有得到使用多线程的好处 package com.example.batchprocessing; 导入javax.s

我正在尝试编写一个解决方案来扩展spring批处理。在spring批处理中,它从MySQL数据库读取数据(600000),然后对其进行处理,并将每个处理行的状态更新为“已完成”


我正在使用
AsyncItemProcessor
AsyncItemWriter
来扩展spring批处理

问题:

如果我同步运行spring批处理,那么异步运行spring批处理所花费的时间相同或更少。我没有得到使用多线程的好处

package com.example.batchprocessing;
导入javax.sql.DataSource;
导入org.springframework.batch.core.Job;
导入org.springframework.batch.core.JobExecutionListener;
导入org.springframework.batch.core.Step;
导入org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
导入org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
导入org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
导入org.springframework.batch.core.launch.support.RunIdIncrementer;
导入org.springframework.batch.integration.async.AsyncItemProcessor;
导入org.springframework.batch.integration.async.AsyncItemWriter;
导入org.springframework.batch.item.ItemProcessor;
导入org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
导入org.springframework.batch.item.database.JdbcBatchItemWriter;
导入org.springframework.batch.item.database.jdbcursoritemreader;
导入org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
导入org.springframework.batch.item.file.FlatFileItemReader;
导入org.springframework.batch.item.file.FlatFileItemWriter;
导入org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
导入org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
导入org.springframework.batch.item.file.mapping.DefaultLineMapper;
导入org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
导入org.springframework.batch.item.file.transform.DelimitedLineAggregator;
导入org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
导入org.springframework.batch.item.support.CompositeItemWriter;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.core.io.ClassPathResource;
导入org.springframework.core.io.FileSystemResource;
导入org.springframework.core.task.SimpleAsyncTaskExecutor;
导入org.springframework.core.task.TaskExecutor;
导入org.springframework.jdbc.core.jdbc模板;
导入org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
导入org.springframework.transaction.annotation.Transactional;
导入java.util.array;
@配置
@启用批处理
公共类批处理配置{
@自动连线
公共建筑商建筑商工厂;
@自动连线
公共StepBuilderFactory StepBuilderFactory;
@豆子
公共JdbcCursorItemReader阅读器(数据源数据源){
JdbcCursorItemReader=新的JdbcCursorItemReader();
reader.setDataSource(数据源);
reader.setSql(“从人中选择*);
setRowMapper(新的UserRowMapper());
返回读取器;
}
@豆子
公共PersonItemProcessor处理器(){
返回新的PersonItemProcessor();
}
@豆子
公共AsyncItemProcessor AsyncItemProcessor()引发异常{
ThreadPoolTaskExecutor executor=新的ThreadPoolTaskExecutor();
执行者。setCorePoolSize(30);
执行器setMaxPoolSize(50);
执行者。设置队列容量(10000);
executor.setThreadNamePrefix(“BatchProcessing-”);
执行人。AfterPropertieSet();
AsyncItemProcessor asyncProcessor=新的AsyncItemProcessor();
asyncProcessor.setDelegate(processor());
asyncProcessor.setTaskExecutor(执行器);
asyncProcessor.AfterPropertieSet();
返回异步处理器;
}
@豆子
公共JdbcBatchItemWriter编写器(数据源数据源){
返回新的JdbcBatchItemWriterBuilder()
.itemSqlParameterSourceProvider(新的BeanPropertyItemSqlParameterSourceProvider())
.sql(“更新人员集状态='completed',其中人员id=:id”)
.dataSource(数据源)
.build();
}
@豆子
公共AsyncItemWriter AsyncItemWriter(){
AsyncItemWriter asyncWriter=新建AsyncItemWriter();
setDelegate(writer(null));
返回异步编写器;
}
@豆子
公共作业导入器作业(作业完成通知侦听器侦听器,步骤1){
返回jobBuilderFactory.get(“importUserJob”)
.incrementer(新的RunIdIncrementer())
.listener(侦听器)
.流程(步骤1)
(完)
.build();
}
@豆子
公共步骤step1(JdbcBatchItemWriter编写器)引发异常{
返回stepBuilderFactory.get(“step1”)
.chunk(10000)
.读卡器(读卡器(空))
//.processor(处理器())
//.作者(作者)
.processor((ItemProcessor)asyncItemProcessor())
.writer(asyncItemWriter())
//.节流限制(30)
.build();
}

}
我同意@5019386,asyncItemProcessor在处理价格昂贵的项目时非常有用。我可以提出的一个建议是将创建ThreadPoolTaskExecutor的代码作为bean移动到单独的块中,这样可以避免创建线程。

AsyncItemProcessor
在处理项目成为瓶颈时非常有用。添加更多线程并不一定意味着事情会进展得更快。事实上,有一个盈亏平衡点可能会更慢(由于线程管理和上下文切换开销)。我看不出您的配置有什么问题,但我无法说明为什么不运行代码,同步执行会占用更多/相同的时间。也就是说,在转向更复杂的异步处理器/编写器方法之前,您是否尝试过多线程步骤?