【发布时间】: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