java 无法存储作业,因为已存在具有此标识的作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34176482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to store job because one already exists with this identification
提问by Majestic
I'm new with Quartz. I succeeded to install it and run it. But I have an error when I run it for the second time because the job already exists with this identification.
我是 Quartz 的新手。我成功安装并运行它。但是当我第二次运行它时出现错误,因为该作业已经存在并带有此标识。
Here my code :
这是我的代码:
public void scheduleJobs() throws Exception {
try {
int i = 0;
scheduler = new StdSchedulerFactory().getScheduler();
JobKey job1Key = JobKey.jobKey("job"+i, "my-jobs"+i);
JobDetail job1 = JobBuilder
.newJob(SimpleJob.class)
.withIdentity(job1Key)
.build();
TriggerKey tk1 = TriggerKey.triggerKey("trigger"+i, "my-jobs"+i);
Trigger trigger1 = TriggerBuilder
.newTrigger()
.withIdentity(tk1)
.withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(11, 25))
.build();
scheduler.start(); // start before scheduling jobs
scheduler.scheduleJob(job1, trigger1);
i++;
printJobsAndTriggers(scheduler);
} catch (SchedulerException e) {
LOG.error("Error while creating scheduler", e);
}
}
I tried to use an integer i to change the name but it does not work. Do you have any idea how can I fix it? Many thanks.
我尝试使用整数 i 来更改名称,但它不起作用。你知道我该如何解决吗?非常感谢。
回答by olivierlemasle
You can:
你可以:
check if the "job key" already exists, and remove the existing job before creating a new one:
scheduler.deleteJob(job1Key);
or create a new job with another key (in your case, each time you execute
scheduleJobs()
, variablei
has the same value (0
)or just re-use the same job (why would you create a new job if the old one is still good)
or use the RAM Job Store, which does not persist jobs in database (each time you will use your software, you will have an empty job store)
检查“作业密钥”是否已存在,并在创建新作业之前删除现有作业:
scheduler.deleteJob(job1Key);
或使用另一个键创建一个新作业(在您的情况下,每次执行时
scheduleJobs()
,变量i
都具有相同的值 (0
)或者只是重复使用相同的工作(如果旧工作仍然不错,为什么要创建新工作)
或使用 RAM 作业存储,它不会将作业保存在数据库中(每次您使用您的软件时,您都会有一个空的作业存储)
It really depends on what you want to do with your jobs!
这真的取决于你想用你的工作做什么!
回答by eouw0o83hf
This is not a direct answer to the specific code listed in the question, but I didn't notice it when searching elsewhere and thought this might be useful for future readers:
这不是问题中列出的特定代码的直接答案,但我在其他地方搜索时没有注意到它,并认为这可能对未来的读者有用:
If you're in a situation where you have an existingJob
but just want to add a newTrigger
, you can call:
如果您处于现有的情况Job
但只想添加一个新的Trigger
,您可以调用:
scheduler.ScheduleJob(trigger);
scheduler.ScheduleJob(trigger);
and it will add the Trigger
to the Job
without trying to recreate the Job
. The only trick is that you have to make sure the Trigger
's JobKey
is correct.
它将添加Trigger
到Job
而不尝试重新创建Job
. 唯一的技巧是您必须确保Trigger
'sJobKey
是正确的。
My overall code for this interaction looks roughly like:
我的这种交互的整体代码大致如下:
IJobDetail job; // Handed in
ITrigger trigger; // Handed in
// Keeping track of this because we need to know later whether it's new or not
var newJob = job == null;
if (newJob)
{
job = JobBuilder.Create<TargetJob>()
.WithIdentity([whatever])
[.OtherConfiguration()]
.Build();
}
var trigger = TriggerBuilder
.Create()
.WithIdentity([whatever])
// ** Gotcha #1: Make sure it's linked to the job **
.ForJob(job.Key)
[.OtherConfiguration()]
.Build();
if (newJob)
{
_scheduler.ScheduleJob(job, trigger);
}
else
{
// ** Gotcha #2: Make sure you don't reschedule the job **
_scheduler.ScheduleJob(trigger);
}
回答by Oleg Mikhailov
Check for existing job before scheduling:
在安排之前检查现有作业:
JobDetail job;
SimpleTrigger trigger;
//Create your trigger and job
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
if (scheduler.checkExists(job.getKey())){
scheduler.deleteJob(job.getKey());
}
scheduler.scheduleJob(job, trigger);
回答by Sibeesh Venu
If anyone of you are facing the same issue and your solution is in C#. This is how you can fix this error.
如果你们中的任何人都面临同样的问题并且您的解决方案是在 C# 中。这是您可以修复此错误的方法。
This is where we configure the scheduler.
这是我们配置调度程序的地方。
public async Task StartAsync(CancellationToken cancellationToken) {
try {
var scheduler = await GetScheduler();
var serviceProvider = GetConfiguredServiceProvider();
scheduler.JobFactory = new CustomJobFactory(serviceProvider);
await scheduler.Start();
await ConfigureDailyJob(scheduler);
}
catch(Exception ex) {
_logger.Error(new CustomConfigurationException(ex.Message));
}
}
This is how we can configure the Job, please be noted that we are checking whether the job is already there, and if the await scheduler.CheckExists(dailyJob.Key)
returns true, we delete that job info and create a new one with the same key.
这是我们如何配置作业,请注意我们正在检查作业是否已经存在,如果await scheduler.CheckExists(dailyJob.Key)
返回 true,我们将删除该作业信息并使用相同的密钥创建一个新的。
private async Task ConfigureDailyJob(IScheduler scheduler) {
var dailyJob = GetDailyJob();
if (await scheduler.CheckExists(dailyJob.Key)) {
await scheduler.DeleteJob(dailyJob.Key);
_logger.Info($ "The job key {dailyJob.Key} was already existed, thus deleted the same");
}
await scheduler.ScheduleJob(dailyJob, GetDailyJobTrigger());
}
There are the supporting private functions.
有配套的私有功能。
private IJobDetail GetDailyJob() {
return JobBuilder.Create < IDailyJob > ().WithIdentity("dailyjob", "dailygroup").Build();
}
private ITrigger GetDailyJobTrigger() {
return TriggerBuilder.Create().WithIdentity("dailytrigger", "dailygroup").StartNow().WithSimpleSchedule(x = >x.WithIntervalInHours(24).RepeatForever()).Build();
}
You can get the complete source code from this GitHub repository.
您可以从此GitHub 存储库获取完整的源代码。
回答by Rushi Pradhan
You can create new jobs by taking i as a static int. And instead of "job"+i it would be "job"+ Integer.toString(i) . It worked for me.
您可以通过将 i 作为静态整数来创建新作业。而不是 "job"+i 它将是 "job"+ Integer.toString(i) 。它对我有用。