Spring批处理通过分区来提高性能



我需要将现有项目转换为Spring批处理作业,以提高作业的速度。假设我有第一个从数据库中检索数据列表并将其放入监听器的tasklet。因此,下一步可以从@BeforeStep中检索它,并做一些条件以获得另一个列表(10k-20k条记录(,然后为每条记录执行多个业务逻辑

但我被如何在Spring批处理中按分区实现这一步骤所困扰。我在reader中找到了所有使用直接查询并由rangePartitioner中的ExecutionContext注入的教程。但我不能那样跟。



<job id="testJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step1"  next="step2">
<tasklet ref="driver"/>
<listeners>
<listener ref="promotionListener">
</listener>
</listeners>
</step>
<step id="step2"> 
<tasklet >
<chunk reader="bmtbBillGenReqReader"
processor="bmtbBillGenReqProcessor"
writer="bmtbBillGenReqWriter"
commit-interval="1">                       
</chunk>
</tasklet>
</step>
</job>
<bean id="promotionListener"
class="org.springframework.batch.core.listener.ExecutionContextPromotionListener">
<property name="keys">
<util:list>
<value>billGenRequests</value>
</util:list>
</property>
</bean>

请告诉我如何从步骤2实现分区。也许先把step2的新列表存储到csv文件或其他文件中?

您可以实现自己的partitionner,而不是使用RangePartitionner,并在此实现中检索数据,而不是专门的步骤。然后传递每个分区的数据,以便根据您的需要创建。例如

public class FilesPartitioner implements Partitioner {
private JdbcOperations jdbcTemplate;
@Autowired
private DataSource dataSource;
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
Map<String, ExecutionContext> map = new HashMap<>();
List<String> filesname = jdbcTemplate.queryForList(
"SELECT DISTINCT FILENAME FROM MYTABLE", String.class);
for (int i = 0; i < filesname.size(); i++) {
ExecutionContext executionContext = new ExecutionContext();
executionContext.put("data", filesname.get(i));
String key = filesname.get(i);
map.put(key, executionContext);
}
return map;
}
}

并在读取器中相应地注入参数

最新更新