access作业bean中的作业参数

v8wbuo2f  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(262)

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

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

我想根据启动作业时传递的jobparameters动态命名作业。我在努力实现 jobBuilderFactory.get(jobNameFromJobParams) . 我知道作业是在配置时定义的,参数是在运行时传递的。我有办法做到吗?

osh3o9ms

osh3o9ms1#

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

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 编辑:添加如何从web控制器启动作业的示例

@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());
   }

}

相关问题