如何在不破坏自动配置的情况下自定义 Spring Batch 默认配置

问题描述 投票:0回答:1

如何在不破坏Spring Boot在

DefaultBatchConfiguration
中提供的自动配置的情况下自定义Spring Batch的
SpringBootBatchConfiguration

我在 Spring Boot 应用程序中使用 Spring Batch 作业,配置如下:

@Configuration
public class MyJobConfiguration {

    @Bean
    public Tasklet myTasklet() {
        return (contribution, chunkContext) -> {
            System.out.println("execute myTasklet");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Step myStep(JobRepository jobRepository, Tasklet myTasklet, PlatformTransactionManager transactionManager) {
        return new StepBuilder("myStep", jobRepository)
                .tasklet(myTasklet, transactionManager)
                .build();
    }

    @Bean
    public Job myJob(JobRepository jobRepository, Step myStep) {
        return new JobBuilder("myJob", jobRepository)
                .start(myStep)
                .build();
    }
}

我用

@SpringBatchTest
测试工作,如下所示:

@SpringBatchTest
@ContextConfiguration(classes = MyJobConfiguration.class)
@EnableAutoConfiguration
class MyJobConfigurationTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Autowired
    private JobRepositoryTestUtils jobRepositoryTestUtils;

    @BeforeEach
    public void setup(@Autowired Job jobUnderTest) {
        this.jobLauncherTestUtils.setJob(jobUnderTest); // this is optional if the job is unique
        this.jobRepositoryTestUtils.removeJobExecutions();
    }

    @Test
    public void testMyJob() throws Exception {
        // given
        JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();

        // when
        JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);

        // then
        Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
    }
}

这是非常标准的,并且按预期工作。从注释

@EnableAutoConfiguration
可以看出,它利用了Spring Boot对Spring Batch的自动配置,例如保证数据库模式在内存数据库中初始化以进行测试。

我想自定义批处理配置以允许异步作业执行。我通过以下方式修改了我的配置:

@Configuration
public class MyJobConfiguration extends DefaultBatchConfiguration {

    public static final int MAX_BATCH_TASK_EXECUTOR_POOL_SIZE = 20;

    @Override
    public TaskExecutor getTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setMaxPoolSize(MAX_BATCH_TASK_EXECUTOR_POOL_SIZE);
        threadPoolTaskExecutor.afterPropertiesSet();
        return threadPoolTaskExecutor;
    }

DefaultBatchConfiguration

javadoc
说:

可以通过扩展类和重写 getter 来进行定制。

这就是我选择这种方法的原因。然而,这似乎消除了 Spring Boot 对 Spring Batch 的自动配置。 Spring Batch 测试失败,因为数据库模式初始化不再完成。状况评估报告证实了这一点。

是否可以通过这种方式自定义 Spring Batch 配置?

当依赖 Spring Boot 的 Spring Batch 自动配置时,是否有其他方法来添加自定义?

配置额外的作业启动器而不是自定义默认作业启动器的行为是否更好?

java spring spring-boot spring-batch
1个回答
0
投票

您可以为要自定义的特定组件定义自定义 bean,而不是扩展 DefaultBatchConfiguration。例如,您可以定义自定义 TaskExecutor bean,而无需扩展 DefaultBatchConfiguration。这允许您保留 Spring Boot 提供的其他自动配置的 bean。

@Configuration
public class MyJobConfiguration {

    @Bean
    public TaskExecutor myTaskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setMaxPoolSize(MAX_BATCH_TASK_EXECUTOR_POOL_SIZE);
        taskExecutor.afterPropertiesSet();
        return taskExecutor;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.