简单的Quartz / Cron作业设置 [英] Simple Quartz/Cron job setup

查看:107
本文介绍了简单的Quartz / Cron作业设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

思考我是m在70%大关附近;我只需要点点滴滴的帮助,就可以将我带到那里。

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!

推荐答案

您几乎在这里:

JobBuilder job = newJob(ServerMonitorJob.class);

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


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

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

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

这篇关于简单的Quartz / Cron作业设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆