Java 有多个作业,并选择在春季运行哪个作业
我需要实现两个不同的作业,并选择其中一个我想运行,但我目前无法运行,这给了我以下错误: JobInvokerController中的Field processJob需要一个bean,但找到了2个: -procesaClientesJob:由类路径资源[BatchConfig.class]中的方法“procesaClientesJob”定义 -ProcesaParcialClientsJob:由类路径资源[BatchConfig.class]中的方法“ProcesaParcialClientsJob”定义 行动: 考虑将其中一个bean标记为@Primary,更新使用者以接受多个bean,或者使用@Qualifier来标识应该使用的bean 我的BatchConfig.java类似于:Java 有多个作业,并选择在春季运行哪个作业,java,spring,spring-batch,Java,Spring,Spring Batch,我需要实现两个不同的作业,并选择其中一个我想运行,但我目前无法运行,这给了我以下错误: JobInvokerController中的Field processJob需要一个bean,但找到了2个: -procesaClientesJob:由类路径资源[BatchConfig.class]中的方法“procesaClientesJob”定义 -ProcesaParcialClientsJob:由类路径资源[BatchConfig.class]中的方法“ProcesaParcialClientsJo
@RestController
public class JobInvokerController {
private static final Logger log = Logger.getLogger(JobInvokerController.class);
@Autowired
JobLauncher jobLauncher;
@Autowired
Job processJob;
@GetMapping("/invokejob/{strFecha}")
public String invokejob(@PathVariable String strFecha) throws Exception {
Date fecha;
if (log.isDebugEnabled()) {
log.debug("Procesamos el parámetro de entrada " + strFecha);
}
try {
if (!StringUtils.isEmpty(strFecha)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
fecha = sdf.parse(strFecha);
} else {
fecha = new Date();
}
} catch (Exception e) {
fecha = new Date();
}
if (log.isDebugEnabled()) {
log.debug("Invocamos al batch con la fecha: " + fecha);
}
JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).addDate("fecha", fecha).toJobParameters();
jobLauncher.run(processJob, jobParameters);
return "Batch job has been invoked";
}
}
[……]
@Bean
Job procesaClientesJob(JobBuilderFactory jobBuilderFactory,
@Qualifier("flowCargaIdsClientes") Flow flowCargaIdsClientes,
@Qualifier("flowCargaDatosClientes") Flow flowCargaDatosClientes,
@Qualifier("flowCargaParcialIdsClientes") Flow flowCargaParcialIdsClientes,
@Qualifier("flowCargaParcialDatosClientes") Flow flowCargaParcialDatosClientes
) {
return jobBuilderFactory.get("procesaClientesJob")
.incrementer(new RunIdIncrementer())
.start(flowCargaIdsClientes).next(flowCargaDatosClientes).end()
.build();
}
@Bean
Job procesaParcialClientesJob(JobBuilderFactory jobBuilderFactory,
@Qualifier("flowCargaParcialIdsClientes") Flow flowCargaParcialIdsClientes,
@Qualifier("flowCargaParcialDatosClientes") Flow flowCargaParcialDatosClientes
) {
return jobBuilderFactory.get("procesaClientesJob")
.incrementer(new RunIdIncrementer())
.start(flowCargaParcialIdsClientes).next(flowCargaParcialDatosClientes).end()
.build();
}
我的类JobInvokeController.java类似于:
@RestController
public class JobInvokerController {
private static final Logger log = Logger.getLogger(JobInvokerController.class);
@Autowired
JobLauncher jobLauncher;
@Autowired
Job processJob;
@GetMapping("/invokejob/{strFecha}")
public String invokejob(@PathVariable String strFecha) throws Exception {
Date fecha;
if (log.isDebugEnabled()) {
log.debug("Procesamos el parámetro de entrada " + strFecha);
}
try {
if (!StringUtils.isEmpty(strFecha)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
fecha = sdf.parse(strFecha);
} else {
fecha = new Date();
}
} catch (Exception e) {
fecha = new Date();
}
if (log.isDebugEnabled()) {
log.debug("Invocamos al batch con la fecha: " + fecha);
}
JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).addDate("fecha", fecha).toJobParameters();
jobLauncher.run(processJob, jobParameters);
return "Batch job has been invoked";
}
}
我还有一个SpringBatchApplication.java:
@SpringBootApplication
public class SpringBatchApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchApplication.class, args);
}
}
如何同时拥有两个作业并选择要运行的作业?何时选择要运行的作业? 如果是在应用程序启动之前,我建议使用配置文件,然后激活要运行的作业的配置文件(
-Dspring.profiles.active=job1
)
或者,如果您想在运行时进行选择,则只需@Autowire两个作业,您可以使用如下所述的限定符:我想在运行时进行选择。如果可能的话,我希望有两个不同的@GetMapping方法,每个方法执行一个不同的作业。我该怎么做呢?您实际上不需要它,只需使用一个:
@GetMapping(/invokejob/{jobId}/{strFecha}”)
,然后在方法中使用一个if(“job1.equals(jobId){}else{}
语句来选择要运行的作业。只是一个提醒-您需要自动连接两个bean(并使用@Qualifier允许容器区分它们)下面是我想说的:,可以直接在github上发表评论。