当前位置: 首页 > 知识库问答 >
问题:

我的配置有Java Quartz调度程序错误或问题

桂智志
2023-03-14
scheduler = schedulerFactory.getScheduler("ASYNC_JOBS_SCHEDULER");

我很困惑,是我错过了一些设置还是有石英的错误。请为我提供一个解决方案。

实例化并启动调度程序的类(servlet)。我没有使用quartz.properties文件,因为根据说明,我不应该在hibernate.cfg.xml和quartz.properties中多次指定db凭据。

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;
    }
}

共有1个答案

钦侯林
2023-03-14

我使用的是Quartz V1.8.4和Struts2(我不认为这在这里特别重要)。要在我的一个操作中访问调度器,我必须使用以下命令。

    public Scheduler getScheduler() throws SchedulerException {
    ServletContext context = this.getServletRequest().getSession().getServletContext();
    SchedulerFactory factory = (StdSchedulerFactory) context.getAttribute("org.quartz.impl.StdSchedulerFactory.KEY");
    return factory.getScheduler();
}

我项目中的石英罐是Quartz-1.8.4.jar和Quartz-Oracle-1.8.4.jar。

我使用他们提供的servlet初始化Quartz,我只是在web.xml中定义了这个servlet:

<servlet> 
  <servlet-name>QuartzInitializer</servlet-name> 
  <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class> 
  <init-param>
    <param-name>shutdown-on-unload</param-name>
    <param-value>true</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
</servlet>
 类似资料:
  • 我已经配置了一个Hadoop 2.7.4集群,其中有3个工作人员和一个主服务器。我必须在集群上运行多个作业。一项工作需要很多时间来完成,而其他工作是小的。我已经知道公平的时间表最适合我的情况。当我更新yarn-site.xml公平调度器类并重新启动集群时,当我访问http://localhost:8088/cluster(资源管理器web UI)时,会导致以下错误

  • 问题内容: 我正在尝试为我的项目需求实现简单的调度程序,我的项目正在使用。到目前为止,我浏览了Adobe网站并尝试实现所提供的示例,但是没有一个示例可以更新我的文件。 因此,根据这个,它应该工作 依赖 我是否需要进行其他任何更改才能使调度程序正常工作?请建议缺少的内容。 问题答案: 以下解决方案按要求工作,这里处理了三种情况 with :每分钟执行一次作业 with :每3分钟执行一次作业 wit

  • 相关: 我觉得我错过了什么?

  • 我们正在将代码从 WAS 8 迁移到 Liberty.In WAS 8,我们有一个配置,可以将更新的策略放在 3 到 8 之间,间隔 1 小时。我们试图在自由中模仿相同的配置。任何人都可以帮助我们如何配置它

  • 这些是什么,我们应该如何配置它们,以查看它们如何影响应用程序性能? 谢谢!

  • 我正在我的ionic应用程序3中实现微软ADAL插件,因此我已经在微软azure上创建了一个帐户,并在azure门户中注册了我的应用程序。在所有必要的配置后,我使用了应用程序凭据在我的离子,但它通过配置错误。