【问题标题】:Access job parameters in job bean访问作业 bean 中的作业参数
【发布时间】:2021-01-27 12:15:22
【问题描述】:

我在我的 spring 批处理配置文件中定义了一个作业 bean:

    @Bean
    public Job job() {
        return jobBuilderFactory.get("job1")
                .incrementer(new RunIdIncrementer())
                .start(step1())
                .build();
    }

我想根据启动作业时传递的 jobParameters 动态命名作业。 我正在尝试实现类似jobBuilderFactory.get(jobNameFromJobParams) 的目标。 我知道作业是在配置时定义的,并且参数是在运行时传递的。 有什么办法可以做到吗?

【问题讨论】:

    标签: java spring spring-boot spring-batch


    【解决方案1】:

    作业参数通常用于在同一作业的不同实例之间变化的属性(通常用于识别作业实例)。在您的情况下,所有作业实例的作业名称都相同,因此我认为系统属性比作业参数更合适。这是一个简单的例子:

    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.JobParameters;
    import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
    import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
    import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
    import org.springframework.batch.core.launch.JobLauncher;
    import org.springframework.batch.repeat.RepeatStatus;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableBatchProcessing
    public class MyJobConfig {
    
        @Bean
        public Job job(JobBuilderFactory jobs, StepBuilderFactory steps,
                       @Value("${jobName:myDefaultJobName}") String jobName) {
            return jobs.get(jobName)
                    .start(steps.get("step")
                            .tasklet((contribution, chunkContext) -> {
                                System.out.println("hello world");
                                return RepeatStatus.FINISHED;
                            })
                            .build())
                    .build();
        }
    
        public static void main(String[] args) throws Exception {
            System.setProperty("jobName", "foo");
            ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfig.class);
            JobLauncher jobLauncher = context.getBean(JobLauncher.class);
            Job job = context.getBean(Job.class);
            jobLauncher.run(job, new JobParameters());
        }
    
    }
    

    打印出来:

    [main] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [SimpleJob: [name=foo]] launched with the following parameters: [{}]
    [main] INFO org.springframework.batch.core.job.SimpleStepHandler - Executing step: [step]
    hello world
    [main] INFO org.springframework.batch.core.step.AbstractStep - Step: [step] executed in 31ms
    [main] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [SimpleJob: [name=foo]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 57ms
    

    您可以在使用-DjobName=dynamicJobName 启动作业时动态传递作业名称

    编辑:添加如何从网络控制器启动作业的示例

    @RestController
    public class JobLaunchingController {
    
       @Autowired
       private JobLauncher jobLauncher;
    
       @Autowired
       private ApplicationContext context;
    
       @RequestMapping(value = "/", method = RequestMethod.POST)
       @ResponseStatus(HttpStatus.ACCEPTED)
       public void launch(@RequestParam("jobName") String jobName) throws Exception {
          Job job = context.getBean(jobName, Job.class);
          jobLauncher.run(job, new JobParameters());
       }
    
    
    }
    

    【讨论】:

    • 我正在启动从控制器端点触发的作业。
    • 这不是问题。动态名称从何而来?一个请求参数,一个servlet上下文参数?
    • 作为请求参数。
    • 好吧,作业名称是动态的,但我猜您的应用程序中定义的作业数量有限,您可以通过名称获取这些作业。因此在这种情况下,您可以从请求参数中获取作业名称,并通过名称从应用程序上下文中获取作业。我用一个例子更新了答案。
    • 编辑有帮助吗?如果是,请接受答案:stackoverflow.com/help/someone-answers。否则,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2016-08-19
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多