简体   繁体   English

如何在Java中使用Quartz Scheduler框架运行cron作业?

[英]How to run cron job using Quartz Scheduler framework in Java?

I am using Quartz Scheduler in Java to run cron jobs. 我在Java中使用Quartz Scheduler运行cron作业。 This is the first time I am using this framework to run the cron jobs so I am having some confusion. 这是我第一次使用此框架运行cron作业,因此我有些困惑。

I was following this tutorial to get the better understanding how to use Quartz framework. 我一直在跟随本教程 ,以更好地了解如何使用Quartz框架。

I am trying to run JobA every week and every month so I started with basic example - 我试图每周和每月运行JobA ,所以我从基本示例开始-

Here is my example which I have got so far. 这是到目前为止我的示例。

public class JobA implements Job {

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        System.out.println("Job A is runing");

        // print whether it is week or month
    }
}

Below is my CronTriggerExample which schedules the job to run 下面是我的CronTriggerExample,它调度作业运行

public class CronTriggerExample {
    public static void main(String[] args) throws Exception {

        JobKey jobKeyA = new JobKey("jobA", "group1");
        JobDetail jobA = JobBuilder.newJob(JobA.class).withIdentity(jobKeyA)
                .build();

        Trigger trigger1 = TriggerBuilder
                .newTrigger()
                .withIdentity("dummyTriggerName1", "group1")
                .withSchedule(CronScheduleBuilder.cronSchedule("5 8 * * 6 ?"))
                .build();

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();

        scheduler.start();
        scheduler.scheduleJob(jobA, trigger1);
    }
}

Problem Statement:- 问题陈述:-

I am not sure how to run JobA every week and every month using the above code. 我不确定如何使用上述代码每周和每月运行JobA。 What will be my cron tab entry for a week and month in my case? 在我的情况下,一周和一个月我的cron选项卡条目将是什么? I don't want to run any Job between 8 PM till 5 AM and any random day is fine. 我不想在晚上8点到凌晨5点之间运行任何Job,而且任何随机的日子都可以。

If JobA is running every week, then it should print out one-week and report_week . 如果JobA每周运行一次,则应打印one-weekreport_week But if JobA is running every month, then it should print out one-month and report_one_month so the next question is - Is there any way, we can pass parameters to JobA when we try to run it? 但是,如果JobA每个月都在运行,那么它应该打印one-monthreport_one_month所以下一个问题是-有什么办法可以在尝试运行JobA时将参数传递给JobA?

The meaning of the 7 fields of cron in quartz: 石英中cron的7个字段的含义:

second minute hour day month week year

year field is optional. year字段是可选的。 * means every, for example, * in week field means every week, so you should use * in both week field and month field. *表示每个,例如,“ week字段中的“ *表示每周,因此您应该在“ week字段和“ month字段中都使用* Note when the week field is specified, don't forget to use ? 请注意,当指定week字段时,不要忘记使用? in day field. day字段中。

My example cron entry for you requirement is: 我为您准备的cron条目示例是:

0 0 0 ? * *

which means running the job every week and every month on 00:00:00, please tune it for your need. 这意味着每周和每月的00:00:00运行该作业,请根据需要进行调整。

For more information, reference: CronTrigger . 有关更多信息,请参考: CronTrigger

I hope it helps. 希望对您有所帮助。

You can pass JobData if required

JobBuilder.newJob(JobClass.class);
jobDetail = jobBuilder.usingJobData("Key", "VALUE")
                    .withIdentity(dbname.getSchemaName(), "group1").build();


However for your case you need to modify cron expression provided in your cronschedular

http://www.cronmaker.com/

Follow above link to build cron expression

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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