我正在使用Spring 3和Quartz 1.8.6。我已经按以下方式在applicationcontext文件中定义了SchedulerFactoryBean:
<bean id="quartzscheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<property name="autoStartup">
<value>true</value>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<prop key="org.quartz.scheduler.instanceName">MyClusteredScheduler</prop>
<prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
<prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.StdJDBCDelegate</prop>
<prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
<prop key="org.quartz.jobStore.isClustered">true</prop>
<prop key="org.quartz.jobStore.clusterCheckinInterval">20000</prop>
<prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
</props>
</property>
<property name="dataSource">
<ref bean="quartzdataSource" />
</property>
</bean>
<bean id="quartzdataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3305/quartz" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
这是我的工作类,基本上是一个实体bean类,定义如下:
@Entity
@Table(name = "DATABASEMONITOR")
public class DatabaseMonitor implements Serializable, Job {
@Id
@GeneratedValue
@Column(name = "ID", unique = true, nullable = false)
private Integer id;
@Column(name = "NAME", nullable = false)
private String name;
public DatabaseMonitor(String name, String sqlQuery){
this.name = name;
this.sqlQuery = sqlQuery;
}
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("inside job");
}
}
这是我的调度器控制器类:
public class SchedulerController {
public static void addJob(DatabaseMonitor databaseMonitor){
JobDetail job = new JobDetail();
job.setName(databaseMonitor.getName());
job.setJobClass(DatabaseMonitor.class);
CronTrigger trigger = new CronTrigger();
trigger.setName(databaseMonitor.getName());
try {
trigger.setCronExpression("0/2 * * * * ?");
Scheduler scheduler = (Scheduler) BeansManager.getInstance().getBean("quartzscheduler");
scheduler.scheduleJob(job, trigger);
} catch (ParseException e) {
e.printStackTrace();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
我在主类中调用此调度函数来在运行时添加作业:
DatabaseMonitor databaseMonitor = new DatabaseMonitor(new Date().toString(), "query string");
SchedulerController.addJob(databaseMonitor);
现在问题是一切工作正常,没有任何异常,并且作业详细信息已更新到预定义的quartz数据库详细信息中。但是该作业未被执行。您能帮我解决这个问题吗?