spring-batch - 使用 MessageChannelPartitionHandler 发送分区消息后,Master 步骤可以像 Slave 一样吗
问题描述
我使用使用 MessageChannelPartitionHandler 的 Spring Batch 创建了一个小的概念证明(POC)。这个想法是将代码部署在生产中的多个节点上。我的问题是除了使用像 ActiveMQ 这样的消息传递中间件将分区消息发送到从属节点之外,主节点还可以完成从属节点的工作吗?主人是否仅限于发送消息?
@Bean
public Step masterStep() throws Exception {
return stepBuilderFactory.get("masterStep")
.partitioner(slaveStep().getName(), partitioner())
.step(slaveStep())
.partitionHandler(partitionHandler(null))
.taskExecutor(taskExecutor())
.gridSize(GRID_SIZE)
.build();
}
@Bean
public Step slaveStep() {
return stepBuilderFactory.get("slaveStep")
.<WorkItem, ReportData>chunk(CHUNK_SIZE)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
@Bean
@Profile("master")
public Job processingBatchJob() throws Exception {
return
jobBuilderFactory.get("processingBatchJob").listener(jobListener)
.start(masterStep())
.build();
}
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setMaxPoolSize(20);
taskExecutor.setCorePoolSize(20);
taskExecutor.setQueueCapacity(100);
taskExecutor.afterPropertiesSet();
taskExecutor.setThreadNamePrefix("xtrac");
return taskExecutor;
}
@Bean
public PartitionHandler partitionHandler(MessagingTemplate
messagingTemplate) throws Exception
{
MessageChannelPartitionHandler partitionHandler = new
MessageChannelPartitionHandler();
partitionHandler.setStepName("slaveStep");
partitionHandler.setGridSize(GRID_SIZE);
partitionHandler.setMessagingOperations(this.messageTemplate);
partitionHandler.setPollInterval(5000l);
partitionHandler.setJobExplorer(this.jobExplorer);
partitionHandler.afterPropertiesSet();
return partitionHandler;
}
解决方案
是的,主服务器可以在远程分区设置中充当工作人员,并且不限于仅向工作人员发送消息和从工作人员返回聚合结果。我什至建议这样做,这样主节点上的资源就不会浪费在等待工作人员完成他们的工作。
推荐阅读
- python - 为什么这个 for 循环会抛出 TypeError?
- java - 升级到 'com.google.android.gms:play-services-ads:18.1.0' 后的 Android 编译错误
- algorithm - 李超树 vs 凸壳技巧。应该首选哪个以及何时?
- swift - 以编程方式锚定视图组件会产生错误
- c# - int.MinValue 和 int.MaxValue 之间的随机数,包括
- javascript - 访问数组内的对象数据
- c - 在 C 中,有没有办法用一个格式化的打印语句打印一个未知大小的数组?
- c++ - QGraphicsScene 项目在两次(x2)位置绘制
- python - 无法将opencv图像拆分为RGB
- ajax - Erro 500 ajax-upload.php 上传头像