简体   繁体   中英

Simple Quartz/Cron job setup

I'm using Quartz to write a simple server monitor in Java:

public class ServerMonitorJob implements Job {
    @Override
    public void execute(JobExecutionContext ctx) {
        // Omitted here for brevity, but uses HttpClient to connect
        // to a server and examine the response's status code.
    }
}

public class ServerMonitorApp {
    private ServerMonitorJob job;

    public ServerMonitorApp(ServerMonitorJob jb) {
        super();

        this.job = jb;
    }

    public static void main(String[] args) {
        ServerMonitorApp app = new ServerMonitorApp(new ServerMonitorJob());
        app.configAndRun();
    }

    public void configAndRun() {
        // I simply want the ServerMonitorJob to kick off once
        // every 15 minutes, and can't figure out how to configure
        // Quartz to do this...

        // My initial attempt...
        SchedulerFactory fact = new org.quartz.impl.StdSchedulerFactory();
        Scheduler scheduler = fact.getScheduler();
        scheduler.start();

        CronTigger cronTrigger = new CronTriggerImpl();

        JobDetail detail = new Job(job.getClass()); // ???

        scheduler.schedule(detail, cronTrigger);

        scheduler.shutdown();
    }
}

I think I'm somewhere around the 70% mark; I just need help connecting the dots to get me all the way there. Thanks in advance!

You are almost there:

JobBuilder job = newJob(ServerMonitorJob.class);

TriggerBuilder trigger = newTrigger()
        .withSchedule(
            simpleSchedule()
                .withIntervalInMinutes(15)
        );


scheduler.scheduleJob(job.build(), trigger.build());

Check out the documentation , note that you don't need a CRON trigger when you simply want to run the job every 15 minutes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM