Quartz调度程序作业未存储在数据库中

发布于 2024-12-22 01:13:21 字数 2119 浏览 0 评论 0原文

当我使用 QuartzHelper.getScheduler().checkExists(new JobKey(name,group_name)); 来管理调度程序中的作业时 -quartz 在数据库中查找它(要查看它,我只需删除表并在该方法上遇到 jdbc 异常)。

但随后新作业被添加到调度程序中,没有记录被添加到数据库中。当作业正在运行时,如果我尝试创建一个具有相同名称的新作业,则会引发异常。例外:

Unable to store Job : 'jobname', because one already exists with this identification.
at org.quartz.simpl.RAMJobStore.storeJob(RAMJobStore.java:277)

很明显使用RAMJobStore而不是JDBC。

问题:我做错了什么,我该怎么做才能使quartz将数据存储在数据库中?

我有下一个石英配置:

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://127.0.0.1:3306/quartz
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = qwerty
org.quartz.dataSource.myDS.maxConnections = 10

这是代码:

JobBuilder builder = JobBuilder.newJob(ScriptBaseJob.class);
builder.withIdentity(name, GROUP_NAME);
JobDataMap dataMap = new JobDataMap(params);
builder.usingJobData(dataMap);
builder.requestRecovery(true);
builder.storeDurably(true);
JobDetail jobDetail = builder.build();

SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
scheduleBuilder.repeatForever();
scheduleBuilder.withIntervalInSeconds(60);

Trigger trigger = null;
try {
    trigger = QuartzHelper.getScheduler().getTrigger(new TriggerKey(name, GROUP_NAME));
} catch (Exception e) {
    e.printStackTrace();
}
if (trigger==null) {
    TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
    triggerBuilder.withSchedule(scheduleBuilder);
    triggerBuilder.withIdentity(name, GROUP_NAME);
    trigger = triggerBuilder.build();
}

SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();

sched.scheduleJob(jobDetail, trigger);

When I use QuartzHelper.getScheduler().checkExists(new JobKey(name,group_name)); to look after a job in scheduler - quartz looks for it in DB (to see it I simply remove tables and got an jdbc exception on that metod).

But then the new job is added to scheduler no records is added to DB. While the job is running if I trying to make a new one with same name - exception is fired. The exception:

Unable to store Job : 'jobname', because one already exists with this identification.
at org.quartz.simpl.RAMJobStore.storeJob(RAMJobStore.java:277)

It is clearly that RAMJobStore is used instead of JDBC.

Question: what I am doing wrong and what can I do to make quartz store data in DB?

I've got next quartz config:

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://127.0.0.1:3306/quartz
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = qwerty
org.quartz.dataSource.myDS.maxConnections = 10

Here is the code:

JobBuilder builder = JobBuilder.newJob(ScriptBaseJob.class);
builder.withIdentity(name, GROUP_NAME);
JobDataMap dataMap = new JobDataMap(params);
builder.usingJobData(dataMap);
builder.requestRecovery(true);
builder.storeDurably(true);
JobDetail jobDetail = builder.build();

SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
scheduleBuilder.repeatForever();
scheduleBuilder.withIntervalInSeconds(60);

Trigger trigger = null;
try {
    trigger = QuartzHelper.getScheduler().getTrigger(new TriggerKey(name, GROUP_NAME));
} catch (Exception e) {
    e.printStackTrace();
}
if (trigger==null) {
    TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
    triggerBuilder.withSchedule(scheduleBuilder);
    triggerBuilder.withIdentity(name, GROUP_NAME);
    trigger = triggerBuilder.build();
}

SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();

sched.scheduleJob(jobDetail, trigger);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2024-12-29 01:13:21

看来应用程序使用默认设置。返回的对象来自什么

QuartzHelper.getScheduler()

It appears that application uses default setting. What does the object returned form from

QuartzHelper.getScheduler()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文