I am having problem using Quarz for scheduling jobs in Java application based on Spring. Here is my custom Job:
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.package.UserService;
@Component
public class ConnectionIntervalJob implements Job {
@Autowired
private UserService userService;
public void execute(JobExecutionContext jExeCtx) throws JobExecutionException {
userService.calculateDelta();
}
}
My ConnectionScheduler class:
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class ConnectionScheduler {
public ConnectionScheduler() throws Exception {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
JobDetail job = JobBuilder
.newJob(com.musala.ving.jobs.ConnectionIntervalJob.class)
.withIdentity("job1" + (int)Math.floor(Math.random()*10) + 3 , "group1" +(int)Math.floor(Math.random()*10) + 3).build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("trigger" +(int)Math.floor(Math.random()*10) + 3, "group1" +(int)Math.floor(Math.random()*10) + 3)
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(20).withRepeatCount(5))
.build();
try {
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
When I build my project using Apache Tomcat 7. The console logs some errors:
ERROR org.quartz.core.JobRunShell - Job group163.job163 threw an unhandled Exception:
java.lang.NullPointerException
at com.musala.ving.jobs.ConnectionIntervalJob.execute(ConnectionIntervalJob.java:18)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
[DefaultQuartzScheduler_Worker-1] ERROR org.quartz.core.ErrorLogger - Job (group163.job163 threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException
at com.musala.ving.jobs.ConnectionIntervalJob.execute(ConnectionIntervalJob.java:18)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
... 1 more
I suppose I am messing up something with the job name and the trigger name. I used the following link as a reference: quarz doc
Any help is appreciated! Thank you!
Aucun commentaire:
Enregistrer un commentaire