I was trying to implement a simple cron scheduler which gets called at a particular time. It was working perfectly with 1 second or any other delay implementations with the end point. But if I try executing it for a particular time it is failing, and giving me java exception like the below. I am using Java play framework, which also has some parts written in Springboot
<code>org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'training.trainingReminderTrigger', because one already exists with this identification. at org.quartz.simpl.RAMJobStore.storeJob(RAMJobStore.java:277) at org.quartz.simpl.RAMJobStore.storeJobAndTrigger(RAMJobStore.java:249) at org.quartz.core.QuartzScheduler.scheduleJob(QuartzScheduler.java:840) at org.quartz.impl.StdScheduler.scheduleJob(StdScheduler.java:250) at util.executor.SchedulerManager.startTrainingReminderScheduler(SchedulerManager.java:193) at controllers.CommonController.startScheduler(CommonController.java:3044) at Routes$$anonfun$routes$1$$anonfun$applyOrElse$757$$anonfun$apply$2267.apply(routes_routing.scala:9125) at Routes$$anonfun$routes$1$$anonfun$applyOrElse$757$$anonfun$apply$2267.apply(routes_routing.scala:9125) at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.scala:183) at play.core.Router$Routes$$anon$1.invocation(Router.scala:377) at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:56) at util.Global$ActionWrapper.call(Global.java:355) at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:91) at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:90) at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82) at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82) at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251) at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:249) at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:37) at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42) at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386) at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
<code>org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'training.trainingReminderTrigger', because one already exists with this identification. at org.quartz.simpl.RAMJobStore.storeJob(RAMJobStore.java:277) at org.quartz.simpl.RAMJobStore.storeJobAndTrigger(RAMJobStore.java:249) at org.quartz.core.QuartzScheduler.scheduleJob(QuartzScheduler.java:840) at org.quartz.impl.StdScheduler.scheduleJob(StdScheduler.java:250) at util.executor.SchedulerManager.startTrainingReminderScheduler(SchedulerManager.java:193) at controllers.CommonController.startScheduler(CommonController.java:3044) at Routes$$anonfun$routes$1$$anonfun$applyOrElse$757$$anonfun$apply$2267.apply(routes_routing.scala:9125) at Routes$$anonfun$routes$1$$anonfun$applyOrElse$757$$anonfun$apply$2267.apply(routes_routing.scala:9125) at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.scala:183) at play.core.Router$Routes$$anon$1.invocation(Router.scala:377) at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:56) at util.Global$ActionWrapper.call(Global.java:355) at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:91) at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:90) at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82) at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82) at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251) at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:249) at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:37) at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42) at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386) at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
</code>
org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'training.trainingReminderTrigger', because one already exists with this identification. at org.quartz.simpl.RAMJobStore.storeJob(RAMJobStore.java:277) at org.quartz.simpl.RAMJobStore.storeJobAndTrigger(RAMJobStore.java:249) at org.quartz.core.QuartzScheduler.scheduleJob(QuartzScheduler.java:840) at org.quartz.impl.StdScheduler.scheduleJob(StdScheduler.java:250) at util.executor.SchedulerManager.startTrainingReminderScheduler(SchedulerManager.java:193) at controllers.CommonController.startScheduler(CommonController.java:3044) at Routes$$anonfun$routes$1$$anonfun$applyOrElse$757$$anonfun$apply$2267.apply(routes_routing.scala:9125) at Routes$$anonfun$routes$1$$anonfun$applyOrElse$757$$anonfun$apply$2267.apply(routes_routing.scala:9125) at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.scala:183) at play.core.Router$Routes$$anon$1.invocation(Router.scala:377) at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:56) at util.Global$ActionWrapper.call(Global.java:355) at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:91) at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:90) at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82) at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82) at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251) at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:249) at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:37) at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42) at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386) at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
I am sharing my code implementation below,
POST /startScheduler @controllers.CommonController.startScheduler()
POST /restartScheduler @controllers.CommonController.restartScheduler()
<code>##routes file
POST /startScheduler @controllers.CommonController.startScheduler()
POST /restartScheduler @controllers.CommonController.restartScheduler()
</code>
##routes file
POST /startScheduler @controllers.CommonController.startScheduler()
POST /restartScheduler @controllers.CommonController.restartScheduler()
<code>/* controller file */
public Result startScheduler() {
SchedulerManager schedulerExample = new SchedulerManager();
schedulerExample.startTrainingReminderScheduler();
return ok("Scheduler started successfully!");
return ok("Mission failed");
public Result restartScheduler() {
SchedulerManager schedulerExample = new SchedulerManager();
schedulerExample.restartTrainingReminderScheduler();
return ok("Scheduler stopped and restarted successfully");
return ok("Mision Failed");
<code>/* controller file */
public Result startScheduler() {
try {
SchedulerManager schedulerExample = new SchedulerManager();
schedulerExample.startTrainingReminderScheduler();
return ok("Scheduler started successfully!");
} catch(Exception e) {
return ok("Mission failed");
}
}
public Result restartScheduler() {
try {
SchedulerManager schedulerExample = new SchedulerManager();
schedulerExample.restartTrainingReminderScheduler();
return ok("Scheduler stopped and restarted successfully");
} catch(Exception e) {
e.printStackTrace();
}
return ok("Mision Failed");
}
</code>
/* controller file */
public Result startScheduler() {
try {
SchedulerManager schedulerExample = new SchedulerManager();
schedulerExample.startTrainingReminderScheduler();
return ok("Scheduler started successfully!");
} catch(Exception e) {
return ok("Mission failed");
}
}
public Result restartScheduler() {
try {
SchedulerManager schedulerExample = new SchedulerManager();
schedulerExample.restartTrainingReminderScheduler();
return ok("Scheduler stopped and restarted successfully");
} catch(Exception e) {
e.printStackTrace();
}
return ok("Mision Failed");
}
<code>/* Scheduler service implementation */
public static void startTrainingReminderScheduler() {
JobDetail enrolledTrainingReminderjob = JobBuilder.newJob(EnrolledTrainingReminderJob.class)
.withIdentity("trainingReminderTrigger", "training")
Trigger enrolledTrainingReminderJobTrigger = TriggerBuilder.newTrigger()
.withIdentity("trainingReminder", "training")
.withSchedule(CronScheduleBuilder.cronSchedule("0 05 11 ? * FRI,SUN *"))
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.scheduleJob(enrolledTrainingReminderjob, enrolledTrainingReminderJobTrigger);
public static void restartTrainingReminderScheduler() {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.shutdown(true);
scheduler = new StdSchedulerFactory().getScheduler();
<code>/* Scheduler service implementation */
public static void startTrainingReminderScheduler() {
try {
JobDetail enrolledTrainingReminderjob = JobBuilder.newJob(EnrolledTrainingReminderJob.class)
.withIdentity("trainingReminderTrigger", "training")
.build();
Trigger enrolledTrainingReminderJobTrigger = TriggerBuilder.newTrigger()
.withIdentity("trainingReminder", "training")
.withSchedule(CronScheduleBuilder.cronSchedule("0 05 11 ? * FRI,SUN *"))
.build();
try {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(enrolledTrainingReminderjob, enrolledTrainingReminderJobTrigger);
} catch (Exception e) {
e.printStackTrace();
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void restartTrainingReminderScheduler() {
try {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.standby();
scheduler.shutdown(true);
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
} catch(Exception e) {
e.printStackTrace();
}
}
</code>
/* Scheduler service implementation */
public static void startTrainingReminderScheduler() {
try {
JobDetail enrolledTrainingReminderjob = JobBuilder.newJob(EnrolledTrainingReminderJob.class)
.withIdentity("trainingReminderTrigger", "training")
.build();
Trigger enrolledTrainingReminderJobTrigger = TriggerBuilder.newTrigger()
.withIdentity("trainingReminder", "training")
.withSchedule(CronScheduleBuilder.cronSchedule("0 05 11 ? * FRI,SUN *"))
.build();
try {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(enrolledTrainingReminderjob, enrolledTrainingReminderJobTrigger);
} catch (Exception e) {
e.printStackTrace();
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void restartTrainingReminderScheduler() {
try {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.standby();
scheduler.shutdown(true);
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
} catch(Exception e) {
e.printStackTrace();
}
}
<code>/* cron job executor */
package util.executor.job;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import data.dao.MongoBaseDao;
public class EnrolledTrainingReminderJob extends MongoBaseDao implements Job {
static List<String> trainingDetails = new ArrayList<>();
static ZoneId zoneTime = ZoneId.of("Asia/Kolkata");
static LocalTime currentTime = LocalTime.now(zoneTime);
static LocalDate currentDate = LocalDate.now(zoneTime);
public void getAllCurrentTrainings() {
trainingDetails.add("Java");
trainingDetails.add("python");
public void execute(JobExecutionContext context) throws JobExecutionException {
getAllCurrentTrainings();
System.out.println("These are the trainings scheduled today");
System.out.println(trainingDetails);
<code>/* cron job executor */
package util.executor.job;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import data.dao.MongoBaseDao;
public class EnrolledTrainingReminderJob extends MongoBaseDao implements Job {
static List<String> trainingDetails = new ArrayList<>();
static ZoneId zoneTime = ZoneId.of("Asia/Kolkata");
static LocalTime currentTime = LocalTime.now(zoneTime);
static LocalDate currentDate = LocalDate.now(zoneTime);
@SuppressWarnings("all")
public void getAllCurrentTrainings() {
trainingDetails.add("Java");
trainingDetails.add("python");
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
getAllCurrentTrainings();
System.out.println("These are the trainings scheduled today");
System.out.println(trainingDetails);
}
}
</code>
/* cron job executor */
package util.executor.job;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import data.dao.MongoBaseDao;
public class EnrolledTrainingReminderJob extends MongoBaseDao implements Job {
static List<String> trainingDetails = new ArrayList<>();
static ZoneId zoneTime = ZoneId.of("Asia/Kolkata");
static LocalTime currentTime = LocalTime.now(zoneTime);
static LocalDate currentDate = LocalDate.now(zoneTime);
@SuppressWarnings("all")
public void getAllCurrentTrainings() {
trainingDetails.add("Java");
trainingDetails.add("python");
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
getAllCurrentTrainings();
System.out.println("These are the trainings scheduled today");
System.out.println(trainingDetails);
}
}
I am able to send messages with this executor job for delay based execution..But I am getting the exception if I try to execute the endpoint for a particular time. It does not execute automatically if I hit the endpoint before the cron expression time, if I manually hit the endpoint in the time which matches with cron expression time implementation, it gives me quartz exception. I am not able to debug it. I am not sure if it is the issue in my implementation or issue in my approach of testing it.