【问题标题】:Simple Quartz/Cron job setup简单的 Quartz/Cron 作业设置
【发布时间】:2012-06-07 03:43:53
【问题描述】:

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

【问题讨论】:

    标签: java cron quartz-scheduler


    【解决方案1】:

    你快到了:

    JobBuilder job = newJob(ServerMonitorJob.class);
    
    TriggerBuilder trigger = newTrigger()
            .withSchedule(
                simpleSchedule()
                    .withIntervalInMinutes(15)
            );
    
    
    scheduler.scheduleJob(job.build(), trigger.build());
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2012-02-22
      • 2012-11-12
      • 1970-01-01
      • 2015-11-30
      • 2010-11-04
      • 1970-01-01
      相关资源
      最近更新 更多