Cron Scheduler not working for Specific Time in Java

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>##routes file
POST /startScheduler @controllers.CommonController.startScheduler()
POST /restartScheduler @controllers.CommonController.restartScheduler()
</code>
<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()
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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");
    }
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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();
        }
    }
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật