简体   繁体   中英

How to implement Quartz Scheduler with specific job in servlet?

I am trying to implement Quartz Scheduler in Servlet and when I try to run and see the Log then Output is coming linke this. I cant understand what is wrong in this. I am new for this topic.

Code-

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);
    }
}

Output in Log-

[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.

I have defined quartz.properties in src in default package and here is that file-

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

I am trying to sort this problem for last few days but is unable to do so.

You can get all possible scheduling(Quartz Scheduling) from below link:

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

Please try this way it will execute every minute.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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