0

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:
harry123
  • 760
  • 1
  • 7
  • 22
  • What's the stack trace message? – glovemobile Sep 16 '21 at 00:32
  • @glovemobile updated – harry123 Sep 16 '21 at 00:37
  • I never use dropwizard but I saw quartz package in error message. With quartz, you could define a job that takes a Runnable via the JobDataMap, and run that on execution.. You can check this out https://stackoverflow.com/questions/65022209/quartz-scheduler-execute-an-runnable – glovemobile Sep 16 '21 at 01:02
  • I'm confused on what you are trying to solve. I believe each job already runs on it's own thread. Are you try to run jobs so the next execution is not blocked if the previous one is still running? – Ben Smith Oct 30 '21 at 01:07

0 Answers0