0

我正在使用 Quartz 用 Ja​​va 编写一个简单的服务器监视器:

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();
    }
}

我在 70% 左右;我只需要帮助连接点来让我一直到那里。提前致谢!

4

1 回答 1

1

你快到了:

JobBuilder job = newJob(ServerMonitorJob.class);

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


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

查看文档,请注意,当您只想每 15 分钟运行一次作业时,您不需要 CRON 触发器。

于 2012-06-06T19:43:53.020 回答