如何在servlet中实现具有特定作业的Quartz Scheduler?

时间:2018-06-28 09:16:22

标签: java servlets quartz-scheduler

我正在尝试在Servlet中实现Quartz Scheduler,当我尝试运行并看到Log(日志),然后Output(输出)即将链接到此时。我不明白这有什么问题。我是这个主题的新手。

代码-

package Controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzScheduler implements Job {

    private static final Logger LOG = Logger.getLogger(QuartzScheduler.class);

    @Override
    public void execute(JobExecutionContext jec) throws JobExecutionException {
        System.out.println("Executing Job");
        LOG.info("Executing JOb");
    }


    public static void main(String args[]) throws SchedulerException, ParseException {
        String startDateStr = "2018-06-23 16:00:00.0";
        Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
        System.out.println(startDate);
        JobDetail j = JobBuilder.newJob(QuartzScheduler.class).build();
        Trigger t = TriggerBuilder.newTrigger()
                .withIdentity("CroneTrigger")
                .startAt(startDate)
                .withSchedule(CronScheduleBuilder.cronSchedule("0 30 14 * * ?").withMisfireHandlingInstructionDoNothing()).build();
        Scheduler s = StdSchedulerFactory.getDefaultScheduler();
        s.start();
        s.scheduleJob(j, t);
    }
}

日志输出

[2018-06-28 14:23:15,906] main org.quartz.impl.StdSchedulerFactory INFO  - Using default implementation for ThreadExecutor

[2018-06-28 14:23:15,971] main org.quartz.simpl.SimpleThreadPool INFO  - Job execution threads will use class loader of thread: main

[2018-06-28 14:23:16,021] main org.quartz.core.SchedulerSignalerImpl INFO  - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

[2018-06-28 14:23:16,021] main org.quartz.core.QuartzScheduler INFO  - Quartz Scheduler v.2.2.3 created.

[2018-06-28 14:23:16,029] main org.quartz.simpl.RAMJobStore INFO  - RAMJobStore initialized.

[2018-06-28 14:23:16,030] main org.quartz.core.QuartzScheduler INFO  - Scheduler meta-data: Quartz Scheduler (v2.2.3) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

[2018-06-28 14:23:16,031] main org.quartz.impl.StdSchedulerFactory INFO  - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'

[2018-06-28 14:23:16,031] main org.quartz.impl.StdSchedulerFactory INFO  - Quartz scheduler version: 2.2.3

[2018-06-28 14:23:16,031] main org.quartz.core.QuartzScheduler INFO  - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.

我已经在默认包中的src中定义了quartz.properties,这是该文件-

org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

org.quartz.jobStore.misfireThreshold: 60000

org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

最近几天我一直在尝试解决此问题,但无法解决。

2 个答案:

答案 0 :(得分:1)

您可以从下面的链接获取所有可能的调度(石英调度):

https://github.com/javabypatel/spring-boot-quartz-demo

答案 1 :(得分:0)

请尝试以这种方式每分钟执行一次。

String exp="0 0/1 * 1/1 * ? *";//every 1 min schedular
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        JobDetail job = JobBuilder.newJob(QuartzScheduler.class).build();
        Trigger trigger = TriggerBuilder.newTrigger()
                                        .startNow()
                                        .withSchedule(
                                             CronScheduleBuilder.cronSchedule(exp))
                                        .build();
        scheduler.scheduleJob(job, trigger);