I am seeing this exception when trying to start a new spring batch job by quartz scheduler. This is my job configuration:
@Autowired
private BagCmTrkRepository bagCmTrkRepo;
@Autowired
private BagTrkRepository bagTrkRepo;
@Setter(onMethod = @__({@Autowired, @Qualifier("reposDataSource")}))
private DataSource jobDataSource;
@Setter(onMethod = @__({@Autowired, @Qualifier("slaveEntityManagerFactory")}))
private EntityManagerFactory slaveManagerFactory;
private static final String SELECT_SQL = "SELECT b "
"FROM Bag b "
"WHERE created>= :startTime and created < :endTime "
"AND isDone = 1";
private final String masterStepName = "bagCMMasterStep";
@Bean("bagCMPartitioning")
public BagCMPartitionerAndJobListener bagCMPartitionerAndJobListener() {
BagCMPartitionerAndJobListener partitioner = new BagCMPartitionerAndJobListener();
partitioner.setBagCmTrkRepo(bagCmTrkRepo);
partitioner.setJobDataSource(jobDataSource);
partitioner.setMasterStepName(masterStepName);
return partitioner;
}
@Bean("bagCMReader")
@StepScope
public JpaCursorItemReader<Bag> bagCMReader(@Value("#{stepExecutionContext['startTime']}") Object startTime,
@Value("#{stepExecutionContext['endTime']}") Object endTime) {
JpaCursorItemReader<Bag> cursorItemReader = new JpaCursorItemReader<>();
Map<String, Object> namedParameters = new HashMap<>() {{
put("startTime", (Timestamp) startTime);
put("endTime", (Timestamp) endTime);
}};
cursorItemReader.setName("CursorItemReader");
cursorItemReader.setQueryString(SELECT_SQL);
cursorItemReader.setEntityManagerFactory(slaveManagerFactory);
cursorItemReader.setParameterValues(namedParameters);
return cursorItemReader;
}
@Bean("bagCMProcessor")
@StepScope
public ItemProcessor<Bag, BagTrk> bagCMItemProcessor(@Value("#{stepExecution.jobExecution.jobId}") Object jobId) {
Long cmJobId = (Long) jobId;
return new BagCMProcessor(cmJobId);
}
@Bean("bagCMWriter")
@StepScope
public ItemWriter<BagTrk> bagCMItemWriter() {
return new BagCMWriter(bagTrkRepo);
}
@Bean("slaveCMStep")
public Step slaveCMStep() {
return steps.get("bagCMSlaveStep")
.<Bag, BagTrk>chunk(10)
.reader(bagCMReader(null, null))
.processor(bagCMItemProcessor(null))
.writer(bagCMItemWriter())
.build();
}
@Bean("masterCMStep")
public Step masterCMStep() {
return steps.get(masterStepName)
.partitioner(slaveCMStep().getName(), bagCMPartitionerAndJobListener())
.step(slaveCMStep())
.gridSize(BagCMPartitionerAndJobListener.NUMBER_OF_WINDOWS)
.taskExecutor(new SimpleAsyncTaskExecutor())
.build();
}
@Bean("cmJob")
public Job job() {
return jobs.get("cmJob")
.listener(bagCMPartitionerAndJobListener())
.start(masterCMStep())
.build();
}
and when starting the application, I got this:
org.springframework.batch.core.launch.NoSuchJobException: No job configuration with the name [cmJob] was registered at org.springframework.batch.core.configuration.support.MapJobRegistry.getJob(MapJobRegistry.java:68) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:128) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) at jdk.proxy2/jdk.proxy2.$Proxy86.getJob(Unknown Source) at vn.ghtk.archiving.ecom.bag.cm.job.BagCMQuartzJob.executeInternal(BagCMQuartzJob.java:29) at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75) at org.quartz.core.JobRunShell.run(JobRunShell.java:202) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Can anyone help me?
CodePudding user response:
Your job is missing from the job registry.
Try adding a JobRegistryBeanPostProcessor Bean to your configuration
@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor();
postProcessor.setJobRegistry(jobRegistry);
return postProcessor;
}