besmart besmart - 1 year ago 109
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

public class TimeoutJob extends QuartzJobBean{

public final String ID = "idInterruttore";

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

@Autowired InterruttoreService interruttoreService;

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

public class QuartzConfiguration {

ApplicationContext applicationContext;

public SchedulerFactoryBean scheduler() {
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
return schedulerFactory;

public SpringBeanJobFactory springBeanJobFactory() {
AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();

return jobFactory;

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

public class JobManager {

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

SchedulerFactoryBean scheduler;

InterruttoreService interruttoreService;

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.setName("Job detail for " + interruttore.getNomeInterruttore());
jobDetail.setDescription("Job Description");

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

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

When launching the app, the
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(
at it.besmart.quartz.JobManager.createJob(
at it.besmart.quartz.JobManager.createInitialJobs(

which is


as my triggers are not created...

Answer Source

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


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) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download