java - 简单的 Quartz/Cron 作业设置

标签 java cron quartz-scheduler

我正在使用 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% 左右;我只需要帮助将各个点连接起来,让我一路走到那里。提前致谢!

最佳答案

你就快到了:

JobBuilder job = newJob(ServerMonitorJob.class);

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


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

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

关于java - 简单的 Quartz/Cron 作业设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10912941/

相关文章:

java - quartz : The Scheduler has been shutdown

java - 如果我不 shutdown() quartz 调度程序会发生什么

linux - 获取完整的当前 crontab 行,并为每行创建唯一的结果电子邮件 header (发件人、主题等)

ruby-on-rails - 创建每周/每月/每日排行榜

spring - 如何为持久性 Quartz 作业重命名作业类?

java - 如何使用java中的quartz api将给定时间转换为cron表达式?

java - Instanceof 和 Casting,多态性

java - 无法将 List<Object[]> 值分配给特定类型的 ArrayList

java - 在 tomcat 7.0 中运行 Spring MVC 应用程序给我 HTTP 状态 404 : requested resource is not available.

java - 在 JavaParser 中获取继承类的名称