我在使用Quarz在基於Spring的Java應用程序中安排作業的問題。這裏是我的自定義工作:Quarz日程安排作業拋出一些錯誤
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();
}
}
我ConnectionScheduler類:
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();
}
}
}
當我建立使用Apache Tomcat 7的控制檯我的項目記錄一些錯誤:
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
我想我我正在弄亂一些與工作名稱和觸發器名稱有關的東西。因此,我決定生成一個隨機int值並將其添加到組名稱和觸發器名稱,但它不起作用。我用下面的鏈接作爲參考: quarz doc
UPDATE:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 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" />
任何幫助表示讚賞!謝謝!
包含您的xml配置。 – bitkot 2015-03-31 09:05:59
xml包含在請求中 – skywalker 2015-03-31 11:47:14
看起來我的UserService在作業中不是自動佈線 – skywalker 2015-03-31 11:56:20