I am trying to create a job scheduler in java dropwizard framework. I found the dropwizard-sundial
and used as following:
I added the configuration from https://github.com/knowm/dropwizard-sundial. My CronJob.class looks like:
import org.knowm.sundial.Job;
import org.knowm.sundial.annotations.CronTrigger;
import org.knowm.sundial.exceptions.JobInterruptException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@CronTrigger(cron = ""* * * * * ? *"")
public class CronJob extends Job {
private static final Logger logger = LoggerFactory.getLogger(CronJob.class);
@Override
public void doRun() throws JobInterruptException {
logger.info("Hello from Cron Job");
}
}
When I run the service, I can see that the Hello from Cron Job
is being spit out every second.
Is there any way in which I can simply run the cronjob on a separate thread? I tried to make the class implement Runnable
like:
public class CronJob extends Job implements Runnable{
....
}
but then I am getting,
An error occured instantiating job to be executed. job= 'CronJob'
Edit: I also get the above error while making my own constructor for CronJob
class
INFO [2021-09-16 00:28:36,747] org.knowm.sundial.ee.SundialInitializerListener: Sundial Scheduler has been started...
ERROR [2021-09-16 00:28:36,755] org.quartz.ErrorLoggingScheduleListener: An error occured instantiating job to be executed. job= 'CronJob'
! java.lang.NoSuchMethodException: job.CronJob.<init>()
! at java.base/java.lang.Class.getConstructor0(Class.java:3349)
! at java.base/java.lang.Class.newInstance(Class.java:556)
! ... 3 common frames omitted
! Causing: java.lang.InstantiationException: job.CronJob
! at java.base/java.lang.Class.newInstance(Class.java:571)
! at org.quartz.jobs.SimpleJobFactory.newJob(SimpleJobFactory.java:47)
! ... 2 common frames omitted
! Causing: org.quartz.exceptions.SchedulerException: Problem instantiating class 'job.CronJob'
! at org.quartz.jobs.SimpleJobFactory.newJob(SimpleJobFactory.java:49)
! at org.quartz.core.JobRunShell.initialize(JobRunShell.java:113)
! at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:346)
INFO [2021-09-16 00:28:36,756] org.quartz.core.RAMJobStore: All triggers of Job CronJob set to ERROR state.
INFO [2021-09-16 00:28:37,441] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources: