我是grails技术的新手。现在我要写一个带有grails的调度程序,所以我在那里遇到麻烦。
Quartz 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文件夹中添加了这个罐子。
1) c3p0-0.9.1.1.jar
2) quartz-all-2.1.1.jar
这完全适用于IDE。 但是当我创建war文件并进行部署时 tomcat然后作业和触发器不会在 mysql server 5.5.30 中持续存在 tomcat版本是 apache-tomcat-7.0.42
答案 0 :(得分:1)
最后经过这么多麻烦后我把这个问题搞错了,只需更改名称quartz.properties而不是Quartz.properties文件名。
感谢所有人。
答案 1 :(得分:0)
如果不调用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>