besmart besmart - 19 days ago 11
Java Question

Spring + Quartz, Trigger is null

I'm dwelling with Quartz from days..

I need to create, the app starts, some triggers and job details..

So, this is my Job

@DisallowConcurrentExecution
public class TimeoutJob extends QuartzJobBean{

public final String ID = "idInterruttore";

private final Logger logger = Logger.getLogger(TimeoutJob.class);

@Autowired InterruttoreService interruttoreService;

@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

JobDataMap dataMap = context.getJobDetail().getJobDataMap();
int idInterruttore = dataMap.getIntFromString(ID);
Interruttore interruttore = interruttoreService.findById(idInterruttore);
logger.debug("Job reached for " + interruttore.getNomeInterruttore());

}
}


Then i configure some bean in QuartzConfiguration.java

@Configuration
@ComponentScan("it.besmart")
public class QuartzConfiguration {

@Autowired
ApplicationContext applicationContext;

@Bean
public SchedulerFactoryBean scheduler() {
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
schedulerFactory.setJobFactory(springBeanJobFactory());
return schedulerFactory;
}

@Bean
public SpringBeanJobFactory springBeanJobFactory() {
AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);

return jobFactory;
}
}


Now, I have a JobManager.class which manage jobDetails and Triggers

@Service("jobManager")
public class JobManager {

private final Logger logger = Logger.getLogger(JobManager.class);

@Autowired
SchedulerFactoryBean scheduler;

@Autowired
InterruttoreService interruttoreService;

@PostConstruct
public void createInitialJobs() {
logger.debug("Start ut jobs to create");
List<Interruttore> interruttori = interruttoreService.findAllSwitches();
Date now = new Date();
for (int i = 0; i < interruttori.size(); i++) {
Interruttore interruttore = interruttori.get(i);
if (interruttore.getTimeoutDate().after(now) && interruttore.isStato()) {
// JobDetail and Trigger creation
createJob(interruttore, interruttore.getTimeoutDate());
}
}

}

public void createJob(Interruttore interruttore, Date richiesta) {

JobDetailFactoryBean jobDetail = new JobDetailFactoryBean();
jobDetail.setJobClass(TimeoutJob.class);
jobDetail.setName("Job detail for " + interruttore.getNomeInterruttore());
jobDetail.setDescription("Job Description");
jobDetail.setDurability(true);

Map<String, Integer> map = new HashMap<String,Integer>();
map.put("idInterruttore", interruttore.getIdInterruttore());
jobDetail.setJobDataAsMap(map);

long future = richiesta.getTime() - new Date().getTime();
logger.debug("next timeout is " + future / 1000 / 60 + " minuti for " + interruttore.getNomeInterruttore());

//trigger creation
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setName("myTrigger"+interruttore.getNomeInterruttore());
trigger.setGroup("timeoutTriggers");
trigger.setJobDetail(jobDetail.getObject());
trigger.setStartDelay(0);
trigger.setRepeatCount(1);
trigger.setRepeatInterval(future);
trigger.afterPropertiesSet();
logger.debug("Trigger for " + interruttore.getNomeInterruttore());
logger.debug("Trigger object is :" + trigger.getObject());
logger.debug("Next Trigger date " + trigger.getObject().getFinalFireTime());

try {
scheduler.getScheduler().scheduleJob(jobDetail.getObject(), trigger.getObject());
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


When launching the app, the
@PostConstruct
method tries to create the triggers, but i'm getting an exception when creating jobManager

Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobManager': Invocation of init method failed; nested exception is java.lang.NullPointerException


caused by

Caused by: java.lang.NullPointerException
at org.springframework.scheduling.quartz.SimpleTriggerFactoryBean.afterPropertiesSet(SimpleTriggerFactoryBean.java:231)
at it.besmart.quartz.JobManager.createJob(JobManager.java:85)
at it.besmart.quartz.JobManager.createInitialJobs(JobManager.java:54)


which is

trigger.afterPropertiesSet();


as my triggers are not created...

Answer

There is a bug in the spring-context-support jar 4.2.5 version.

        sti.setJobKey(this.jobDetail.getKey());

i.e. jobDetail can be null.

In the new versions it is fixed. I checked 4.3.2 version.

You can use 4.3.2 or later.

In 4.3.2 version

if (this.jobDetail != null) {
        sti.setJobKey(this.jobDetail.getKey());
    }
Comments