【问题标题】:Quartz Scheduler is not working in war fileQuartz 调度程序在 war 文件中不起作用
【发布时间】:2014-08-12 07:01:17
【问题描述】:

我是 Grails 技术的新手。现在我要编写一个带有 grails 的调度程序,所以我在那里遇到了麻烦。

石英版本 2.2.1 Grails 2.3.7 版

1) 这是我的工作。

public class OrderFetchJob implements Job {
    private static Logger log = Logger.getLogger(OrderFetchJob.class);
    public OrderFetchJob() {
    }

    public void execute(JobExecutionContext context)throws JobExecutionException {
        System.out.println("Hello!  HelloJob is executing. " + new Date());
        GlobalAppService globalAppService =    
        SpringsUtil.getBean(SpringsUtil.GLOBAL_APP_SERVICE);
        globalAppService.startApplication();
    } 
}

2) 这是我的调度器。

    public class OrderFetchScheduler {
    private static Logger log = Logger.getLogger(OrderFetchScheduler.class);

    private static OrderFetchScheduler JOB_SCHEDULER = new OrderFetchScheduler();
    private Scheduler scheduler = null;

    public OrderFetchScheduler() {
    }

    public static OrderFetchScheduler getInstance() {
        return JOB_SCHEDULER;
    }

    public void startup() {
        try {
            // and start it off
            scheduler = StdSchedulerFactory.getDefaultScheduler();

            // define the job and tie it to our OrderFetchJob class
            JobDetail job = newJob(OrderFetchJob.class).withIdentity("job1",
                    "group1").build();

            // Trigger a job that repeats every 20 seconds

            Trigger trigger = newTrigger().withIdentity("trigger1", "group1")
                    .withSchedule(cronSchedule("0 0/5 * 1/1 * ? *")).build();
            Trigger trg = scheduler.getTrigger(trigger.getKey());
            if (trg == null) {
                // Tell quartz to schedule the job using our trigger
                scheduler.scheduleJob(job, trigger);
                log.debug("if");
            } else {

                TriggerBuilder tb = trg.getTriggerBuilder();
                Trigger trigger1 = tb.withSchedule(
                        cronSchedule("0 0/5 * 1/1 * ? *")).build();
                scheduler.rescheduleJob(trg.getKey(), trigger1);
                log.debug("else");

            }
            scheduler.start();

            log.debug("success");

        } catch (SchedulerException se) {
            log.debug(se);
        }
    }

    public void shutdown() {
        try {
            scheduler.shutdown();
            log.debug("shutdown");

        } catch (SchedulerException se) {
            log.debug(se);
        }
    }
}

3) 这是 conf/Quartz.properties

#    Configure Main Scheduler Properties

org.quartz.scheduler.instanceName = MyClusteredScheduler
org.quartz.scheduler.instanceId = AUTO


#    Configure ThreadPool   

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5

#   Configure JobStore

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_

org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000

# Configure Datasources

org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost/mydb?   
useUnicode=yes&characterEncoding=UTF-8
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = root
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery=select 0 from dual  

4) 最后在 lib 文件夹中添加了这个 jars。

1) c3p0-0.9.1.1.jar
2) quartz-all-2.1.1.jar

这与 IDE 完美配合。 但是当我创建战争文件并部署时 tomcat 然后作业和触发器不会在 mysql server 5.5.30 中持续存在 tomcat 版本是 apache-tomcat-7.0.42

【问题讨论】:

    标签: java tomcat grails quartz-scheduler


    【解决方案1】:

    经过这么多麻烦,我终于解决了这个问题,只需更改名称 quartz.properties 而不是 Quartz.properties 文件名。

    谢谢大家。

    【讨论】:

      【解决方案2】:

      如果不调用您的 OrderFetchScheduler.startup() 方法,调度程序将无法启动。你在你的网络应用程序中哪里调用了这个方法?

      MyStartUp.java

      package com.you.web;
      
      import javax.servlet.ServletContextEvent;
      import javax.servlet.ServletContextListener;
      
      public class MyStartUp implements ServletContextListener {
      
          public void contextInitialized(ServletContextEvent sce) {
              // call your Scheduler startup function
              Example : 
              new OrderFetchScheduler().startup()
          }
          public void contextDestroyed(ServletContextEvent sce) {
          }
      }
      

      web.xml中配置

      <web-app ...>
          ...
          <listener>
              <listener-class>com.you.web.MyStartUp</listener-class>
          </listener>
      </web-app>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        相关资源
        最近更新 更多