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.package.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.package.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.pakage.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. Because of this I decided to generate a random int vale and add it to the group name and the trigger name but it did not work. I used the following link as a reference: quarz doc
UPDATE:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:context="http://ift.tt/GArMu7"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/GAf8ZW
http://ift.tt/GArMu7
http://ift.tt/1bb5cbf">
<!-- Database Configuration -->
<import resource="DataSource.xml" />
<import resource="hibernate.xml" />
<bean class="com.package7.config.WebSocketConfig" />
<!-- Auto scan the components -->
<context:annotation-config />
<context:component-scan base-package="com.package1.entities" />
<context:component-scan base-package="com.package2.ving.controllers" />
<context:component-scan base-package="com.package3.dao" />
<context:component-scan base-package="com.package4.daoimpl" />
<context:component-scan base-package="com.package5.serviceimpl" />
<context:component-scan base-package="com.package6.jobs" />
Any help is appreciated! Thank you!
Aucun commentaire:
Enregistrer un commentaire