Skip to content
Advertisement

Trying to make a daily job schedule with quartz in java

I’m working on a Discord Bot with should do sth every day. I tried using the Quartz library for it, but the job is never executed. Below is the code I use for testing:

import org.quartz.*;
import shedule.Daily;

import static org.quartz.CronScheduleBuilder.cronSchedule;

public class QuartzTestClass {

    private void doIt() throws SchedulerException {
        SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
        Scheduler scheduler = schedFact.getScheduler();
        scheduler.start();

        JobBuilder jobBuilder = JobBuilder.newJob(Job.class);
        JobDataMap data = new JobDataMap();
        data.put("latch", this);

        JobDetail jobDetail = jobBuilder.usingJobData("test", "QuartzTestClass")
                .usingJobData(data)
                .withIdentity("testJob", "Daily")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("fireEveryDay", "Daily")
                .startNow()
                .withSchedule(cronSchedule("0 13 22 ? * * *")) //this is now +1 min
                .build();

        scheduler.scheduleJob(jobDetail, trigger);
    }

    public static void main (String[] args) throws SchedulerException {
        new QuartzTestClass().doIt();
    }


    private class job implements Job{

        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            System.out.println("executed");
        }
    }
}

The job is never called. Can some tell me what I did wrong?

Greetings, Martin.

Advertisement

Answer

  1. You called your class job(small letter), but used in JobBuilder Job.class (with big letter) – it’s an interface of org.quartz. . Call your class like – StartMyBotJob to avoid this, or print JobBuilder.newJob(job.class)
  2. Make your job class separately(in another file) from your class OR make it public (visible for creation by another object) and static (if nested class is not static it depends from the QuartzTestClass, and require it’s instance to create object. See more – https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html)

This version should work:

import org.quartz.*;

import static org.quartz.CronScheduleBuilder.cronSchedule;

public class QuartzTestClass {

    private void doIt() throws SchedulerException {
        SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
        Scheduler scheduler = schedFact.getScheduler();
        scheduler.start();

        JobBuilder jobBuilder = JobBuilder.newJob(StartMyBotJob.class);
        JobDataMap data = new JobDataMap();
        data.put("latch", this);

        JobDetail jobDetail = jobBuilder.usingJobData("test", "QuartzTestClass")
                .usingJobData(data)
                .withIdentity("testJob", "Daily")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("fireEveryDay", "Daily")
                .startNow()
                .withSchedule(cronSchedule("0 13 22 ? * * *")) //this is now +1 min
                .build();

        scheduler.scheduleJob(jobDetail, trigger);
    }

    public static void main (String[] args) throws SchedulerException {
        new QuartzTestClass().doIt();
    }


    public static class StartMyBotJob implements Job{

        public void execute(JobExecutionContext context) throws JobExecutionException {
            System.out.println("executed");
        }
    }
}
6 People found this is helpful
Advertisement