首页 > 解决方案 > 使用 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;
}

标签: spring-batchspring-jms

解决方案


是的,主服务器可以在远程分区设置中充当工作人员,并且不限于仅向工作人员发送消息和从工作人员返回聚合结果。我什至建议这样做,这样主节点上的资源就不会浪费在等待工作人员完成他们的工作。


推荐阅读