【问题标题】:How to pass JobParameters while creating spring beans using java config如何在使用 java config 创建 spring bean 时传递 JobParameters
【发布时间】:2018-11-08 12:12:51
【问题描述】:

我最初在 [Getting "Scope 'step' is not active for the current thread" while creating spring batch beans 提出了一个问题,根据Spring batch scope issue while using spring boot 提供的建议,我尝试替换 @StepScope 注释,而是在配置中定义 StepScope bean,如下所示

@Bean
@Qualifier("stepScope")
public org.springframework.batch.core.scope.StepScope stepScope() {

    final org.springframework.batch.core.scope.StepScope stepScope = new org.springframework.batch.core.scope.StepScope();
    stepScope.setAutoProxy(true);
    return stepScope;
}

通过此更改,我无法在创建 bean 时传递作业参数,因为它正在抛出

在“org.springframework.beans.factory.config.BeanExpressionContext”类型的对象上找不到“jobParameters”

我的配置是这样的

@Configuration
@EnableBatchProcessing
public class MyConfig{

@Bean
@Qualifier("partitionJob")
public Job partitionJob() throws Exception {

    return jobBuilderFactory
            .get("partitionJob")
            .incrementer(new RunIdIncrementer())
            .start(partitionStep(null))
             .build();
}

@Bean
@StepScope
public Step partitionStep(
        @Value("#{jobParameters[gridSize]}") String gridSize)
        throws Exception {

    return stepBuilderFactory
            .get("partitionStep")
            .partitioner("slaveStep", partitioner())
            .gridSize(gridZize)
            .step(slaveStep(chunkSize))
            .taskExecutor(threadPoolTaskExecutor()).build();
}

@Bean
@StepScope
public Step slaveStep(int chunkSize) throws Exception {

    return stepBuilderFactory
            .get("slaveStep")
            ......
            ........
}

我读到,如果需要像我的示例一样访问作业参数,则应使用 @StepScope 注释 bean。但如上所述,我遇到了例外情况。

【问题讨论】:

  • 我尝试了一种解决方法,将 gridsize 作为 java 系统属性 -DgridSize=10 传递并使用 @Value 注释注入。仅当在读取器/写入器/处理器上使用时,可能在 stepScope 注释和 JobParameter 注入一起工作。不在台阶上。
  • 您是否尝试在 xml 配置中模仿这一点以进行测试?我认为你不应该在 xml 中得到这个错误。我知道您可能需要在 Spring Boot 上完成这项工作,但以防万一这值得一试。
  • 还没有,可能会尝试xml配置,而不是java配置,看看它是如何工作的。不过感谢您的建议。
  • 您是否尝试过改用@JobScope?

标签: spring spring-batch


【解决方案1】:

这是传递作业参数的方式

  GenericApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");     
        JobParameters jobParameters = new JobParametersBuilder()
                                                               .addString("paramter1", "Test")  
                                                               .toJobParameters();

        Job job = (Job) context.getBean("myJob");
        JobExecution execution = jobLauncher.run(job, jobParameters);

访问作业参数

    Bean
@StepScope
public Step partitionStep(
        @Value("#{jobParameters['paramter1']}") String gridSize)
        throws Exception {

    return stepBuilderFactory
            .get("partitionStep")
            .partitioner("slaveStep", partitioner())
            .gridSize(gridZize)
            .step(slaveStep(chunkSize))
            .taskExecutor(threadPoolTaskExecutor()).build();
}

【讨论】:

  • 正如我在原始帖子中提到的(请参阅本文开头的链接),如果我在步骤本身上设置 StepScope 注释,我确实会收到异常“范围在当前线程中不活动”。但它工作正常,如果我在读者/作家上使用 stepscope。我不确定 StepScope 是否不是 step 本身的有效注释。
猜你喜欢
  • 2013-07-07
  • 2013-03-24
  • 2014-04-05
  • 2019-01-19
  • 2020-04-17
  • 1970-01-01
  • 2018-08-14
  • 2016-01-19
  • 1970-01-01
相关资源
最近更新 更多