我通过使用实现 ServletContextListener 的 Servlet 在 strut2 web-app 中添加了 QuartzFramework。在使用石英属性的 servlet 中,我创建了一个名为“AsyncJobsQuartz”的石英实例,并使用 scheduler.start() 启动它。
使用 JobStore 的计划作业,因此每天在计划的时间执行作业。但问题是当一个作业正在执行时,我不能在其他地方使用调度程序实例。即,在其他操作中,我试图通过使用来获取实例
scheduler = schedulerFactory.getScheduler("ASYNC_JOBS_SCHEDULER");
但是我将调度程序设为空,后来 scheduler.getJobGroupNames() 是空指针异常。但是一旦工作结束,调度程序就会返回,并提供所有详细信息。
我很困惑,是我错过了一些设置还是 Quartz 有错误。请为此提供解决方案。
实例化和启动调度程序的类(servlet)。我没有使用quartz.properties 文件,因为按照说明,我不应该多次指定db 凭据,即在hibernate.cfg.xml 中,然后在quartz.properties 中。
package com.generalsentiment.test.quartz;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.lang.reflect.Field;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.common.collect.Maps;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.hibernate.SessionFactory;
import org.hibernate.impl.SessionFactoryImpl;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerMetaData;
import org.quartz.impl.matchers.GroupMatcher;
import com.generalsentiment.util.HibernateUtil;
import com.generalsentiment.util.quartz.SchedulerConstants;
import com.generalsentiment.util.quartz.QuartzSchedulerUtil;
import org.apache.commons.configuration.*;
public class QuartzServlet implements ServletContextListener {
private Map<String, String> existingJobsList;
private String hibernateConnectionUrl;
private SessionFactory sessionFactory;
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
Properties properties = new Properties();
Field f = SessionFactoryImpl.class.getDeclaredField("properties");
f.setAccessible(true);
sessionFactory = HibernateUtil.getSessionFactory();
Properties p = (Properties) f.get(sessionFactory);
properties.setProperty("org.quartz.scheduler.instanceName", SchedulerConstants.SCHEDULER_INSTANCE_NAME);
properties.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
properties.setProperty("org.quartz.threadPool.threadCount", "4");
properties.setProperty("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
properties.setProperty("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreCMT");
properties.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
properties.setProperty("org.quartz.jobStore.useProperties", "false");
properties.setProperty("org.quartz.jobStore.dataSource", "tasksDataStoreContainer");
properties.setProperty("org.quartz.jobStore.nonManagedTXDataSource", "tasksDataStoreQuartz");
properties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
properties.setProperty("org.quartz.jobStore.misfireThreshold", "60000");
properties.setProperty("org.quartz.jobStore.isClustered", "false");
properties.setProperty("org.quartz.dataSource.tasksDataStoreContainer.driver", p.getProperty("hibernate.connection.driver_class"));
properties.setProperty("org.quartz.dataSource.tasksDataStoreContainer.URL", p.getProperty("hibernate.connection.url"));
properties.setProperty("org.quartz.dataSource.tasksDataStoreContainer.user", p.getProperty("hibernate.connection.username"));
properties.setProperty("org.quartz.dataSource.tasksDataStoreContainer.password", p.getProperty("hibernate.connection.password"));
properties.setProperty("org.quartz.dataSource.tasksDataStoreContainer.maxConnections", "20");
properties.setProperty("org.quartz.dataSource.tasksDataStoreQuartz.driver", p.getProperty("hibernate.connection.driver_class"));
properties.setProperty("org.quartz.dataSource.tasksDataStoreQuartz.URL", p.getProperty("hibernate.connection.url"));
properties.setProperty("org.quartz.dataSource.tasksDataStoreQuartz.user", p.getProperty("hibernate.connection.username"));
properties.setProperty("org.quartz.dataSource.tasksDataStoreQuartz.password", p.getProperty("hibernate.connection.password"));
properties.setProperty("org.quartz.dataSource.tasksDataStoreQuartz.maxConnections", "20");
hibernateConnectionUrl = p.getProperty("hibernate.connection.url");
System.out.println("THE APPLICATION STARTED");
// First we must get a reference to a scheduler
existingJobsList = Maps.newHashMap();
Scheduler scheduler = QuartzSchedulerUtil.initiateSchedulerFactory(properties);
System.out.println("------- Initialization Complete --------");
System.out.println("------- Scheduling Jobs ----------------");
System.out.println("------- Starting Scheduler ----------------");
List<String> jobGroupNames = scheduler.getJobGroupNames();
for (String current : jobGroupNames) {
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.<JobKey>groupEquals(current))) {
existingJobsList.put(jobKey.getName(), jobKey.getGroup());
}
}
if (!existingJobsList.containsKey("ExportOrgUsersToFileJob")) {
JobDetail job = newJob(ExportOrgUsersToFileJob.class).withIdentity("ExportOrgUsersToFileJob",
SchedulerConstants.JOBS_GROUP).build();
CronTrigger trigger = newTrigger().withIdentity("trigger@morning",
SchedulerConstants.TRIGGER_MORNING_GROUP).withSchedule(cronSchedule("00 10 15 * * ?")).build();
job.getJobDataMap().put("jobDescription", "Exports a particular Organization users to file");
job.getJobDataMap().put("jobStatus", SchedulerConstants.JOB_STATUS.PENDING);
Date ft = scheduler.scheduleJob(job, trigger);
}
if (!existingJobsList.containsKey("DownloadScoresJob")) {
JobDetail job1 = newJob(DownloadScoresJob.class).withIdentity("DownloadScoresJob",
SchedulerConstants.JOBS_GROUP).build();
CronTrigger trigger1 = newTrigger().withIdentity("trigger@evening",
SchedulerConstants.TRIGGER_EVENING_GROUP).withSchedule(cronSchedule("00 15 15 * * ?")).build();
job1.getJobDataMap().put("jobDescription", "Downloads involvement index scores for given synsets");
job1.getJobDataMap().put("jobStatus", SchedulerConstants.JOB_STATUS.PENDING);
Date ft1 = scheduler.scheduleJob(job1, trigger1);
}
// JobListener jobListener = new QuartzJobListener();
// scheduler.getListenerManager().addJobListener(jobListener, allJobs());
scheduler.start();
System.out.println("------- Started Scheduler -----------------");
SchedulerMetaData metaData = scheduler.getMetaData();
System.out.println("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
} catch (IllegalAccessException ex) {
Logger.getLogger(QuartzServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchFieldException ex) {
Logger.getLogger(QuartzServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(QuartzServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SchedulerException se) {
System.out.println(se);
} catch (ConfigurationException ex) {
Logger.getLogger(QuartzServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
try {
Scheduler scheduler = QuartzSchedulerUtil.getCurrentScheduler();
if (scheduler != null) {
//to avoid NPE
scheduler.shutdown(true);
}
Thread.sleep(1000);
Driver mySqlDriver = DriverManager.getDriver(hibernateConnectionUrl);
DriverManager.deregisterDriver(mySqlDriver);
sessionFactory.close();
} catch (SchedulerException ex) {
Logger.getLogger(QuartzServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(QuartzServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(QuartzServlet.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("THE QUARTZ APPLICATION STOPPED");
}
public String getHibernateConnectionUrl() {
return hibernateConnectionUrl;
}
public void setHibernateConnectionUrl(String hibernateConnectionUrl) {
this.hibernateConnectionUrl = hibernateConnectionUrl;
}
}