Quartz JobListener dataManager is null in Quartz JobListener jobToBeExecuted

Following
#Quartz :: Jmix Documentation

Executing Quartz Job I can get a valid instance of dataManager
Tracking Quartz Job event I cannot get a valid instance of dataManager

Do I miss something ?

@Component
public class DummyQuartz {

@Autowired
private Scheduler scheduler;

private void scheduleJob(final GE globalEvent) {
final UUID uuid = UUID.randomUUID();
final JobKey jobKey = JobKey.jobKey(“KeyForJob” + uuid, “GE”);

   final JobDetail jd = JobBuilder.newJob()  .withIdentity(jobKey)
  														 .storeDurably(true)
  														 .ofType(NewJob.class)
  														 .usingJobData(CONST_1, globalEvent.info1())
  														 .usingJobData(CONST_2, globalEvent.info2())
  														 .build();

   try {
  	 // add job
  	 scheduler.addJob(jd, true);
  	 // add job listener
  	 scheduler.getListenerManager().addJobListener(new NewJobListener(), KeyMatcher.keyEquals(jobKey));
  	 // start job
  	 scheduler.triggerJob(jobKey);
   } catch (SchedulerException e) {
  	 // throw new RuntimeException(e);
  	 LOGGER.error(e.getMessage());
   }

}
}
// JOB example …

@Component
public class NewJob implements Job {
@Autowired
private DataManager dataManager;

 @Authenticated
 @Override
 public void execute(JobExecutionContext context) {

dataManager. … is valid

}
}

// JOB Listener example …

@Component
public class NewJobListener implements JobListener {

@Autowired
private DataManager dataManager;

// Run this if job is about to be executed.
@Authenticated
@Override
public void jobToBeExecuted(JobExecutionContext context) {
// datamanager is "NULL"
dataManager. …
// datamanager is "NULL"
}
}

Hello Stefano.

DummyQuartz, line

scheduler.getListenerManager().addJobListener(new NewJobListener(), KeyMatcher.keyEquals(jobKey));

You create NewJobListener by constructor as POJO, not as Spring bean. So no beans are autowired and you get null instead.

Autowire NewJobListener as a bean to DummyQuartz and then register it in the Scheduler - that should work.

Regards,
Ivan

1 Like

thanks for the suggestion
it works perfectly.