如何删除或重新安排工作?

时间:2015-03-12 10:50:27

标签: java servlets scheduled-tasks quartz-scheduler

请查看以下代码

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package RemindeWorker.Listener;


import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
import static org.quartz.JobBuilder.newJob;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.TriggerKey.triggerKey;
import org.quartz.impl.StdSchedulerFactory;

/**
 *
 * @author Yohan
 */
public class ReminderExecuter extends ReminderCommon
{
    private static ReminderExecuter r;

    private ReminderExecuter()
    {
        super();
    }

    public static ReminderExecuter  getReminderExecuter()
    {
        if(r==null)
        {
            r = new ReminderExecuter();
        }

        return r;
    }

    public void executeRemnders()
    {
        passportReminder1();
       // passportReminder2();
    }

    private void passportReminder1()
    {
        String[] reminderTime = getFirstReminderTime().trim().split(":");
        String reminderHour = reminderTime[0];
        String reminderMinute = reminderTime[1];

        String[]reminderTime2 = getSecondReminderTime().trim().split(":");
        String reminderHour2 = reminderTime2[0];
        String reminderMinute2 = reminderTime2[1];

        System.out.println("Reminder Hour: "+reminderHour);
        System.out.println("Reminder Minute: "+reminderMinute);

        JobDetail job = newJob(PassportJob.class).withIdentity("passportJob1")
                .usingJobData("userName", getUserName())
                .usingJobData("password",getPassword())
                .usingJobData("host",getHost())

                .build();

         Trigger trigger = newTrigger()
                 .withIdentity(triggerKey("passportTrigger1", "passportTriggerGroup1"))
                 .withSchedule(dailyAtHourAndMinute(Integer.parseInt(reminderHour), Integer.parseInt(reminderMinute)))
                 .startAt(new Date()).build();

         Trigger trigger2 = newTrigger()
                 .withIdentity(triggerKey("passportTrigger2", "passportTriggerGroup2"))
                 .withSchedule(dailyAtHourAndMinute(Integer.parseInt(reminderHour2), Integer.parseInt(reminderMinute2)))
                 .startAt(new Date()).build();

        try {
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(job, trigger);
            System.out.println("Job 1 Scheduled");

            Scheduler scheduler2 = new StdSchedulerFactory().getScheduler();
            scheduler2.start();
            scheduler2.scheduleJob(job, trigger2);
            System.out.println("Job 2 Scheduled");

        } catch (SchedulerException ex) {
            ex.printStackTrace();
        }
    }



    private void deleteJob()
    {
        try {
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.deleteJob(null);

        } catch (SchedulerException ex) {
            System.out.println(ex);
        }
    }
}

在这里,我创建了deleteJob()方法来删除并重新打开或重新安排passportReminder1()中列出的作业。但是我不知道怎么做。我可以删除或重新安排石英作业吗?我正在使用quartz 2 API。

1 个答案:

答案 0 :(得分:3)

我们可以通过两种方式做到:

  1. 手动杀死工作并重新开始
    1. 使用以下代码:
  2.   

    scheduler.deleteJob(new JobKey(jobData.getJobName()));

    有关详细信息,请参阅link

    你要做的是删除一个没用的新作业。

    您可以像下面一样编辑您的deleteJob()并在需要时调用它:

       private void deleteJob(Scheduler scheduler, String jobName)
        {
            try {
    
                scheduler.deleteJob(new JobKey(jobName));
    
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }