Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java quartz scheduler run at specific time

Tags:

java

For example, I want to write a Java program to print "Hello World" at each day 12 am, how can I use Quartz scheduler to achieve this?

Trigger trigger = TriggerUtils.makeDailyTrigger(0, 0);
trigger.setName("trigger1");
trigger.setGroup("group1");

Like this? Where should I put print "hello world" method?

like image 260
user2597504 Avatar asked Oct 11 '13 16:10

user2597504


People also ask

Does Quartz use cron?

Then we use scheduler. xxx options to configure the scheduler. The Quartz scheduler requires the cron option to be set.

How do I run a Quartz job in Java?

CreateQuartzJob.java These steps are as follows: In the first step, we have to initialize the scheduler instance from Quartz by using StdSchedulerFactory() method. After that, we start the scheduler instance with the Quartz API start() method. Start the scheduler instance with Quartz API start()

What is a cron trigger?

Cron Triggers allow users to map a cron expression to a Worker script using a ScheduledEvent listener that enables Workers to be executed on a schedule. Cron Triggers are ideal for running periodic jobs for maintenance or calling third-party APIs to collect up-to-date data.


2 Answers

You could use an expression to schedule the execution of the job. e.g.:

public static class HelloJob implements Job {
    @Override
    public void execute(JobExecutionContext ctx) throws JobExecutionException {
        System.out.println("Hello World");
    }
}

public static void main(String[] args) throws SchedulerException {
    String exp = "0 0 0 1/1 * ? *";

    SchedulerFactory factory = new StdSchedulerFactory();
    Scheduler scheduler = factory.getScheduler();
    scheduler.start();
    JobDetail job = JobBuilder.newJob(HelloJob.class).build();
    Trigger trigger = TriggerBuilder.newTrigger()
                                    .startNow()
                                    .withSchedule(
                                         CronScheduleBuilder.cronSchedule(exp))
                                    .build();
    scheduler.scheduleJob(job, trigger);
}

See http://www.cronmaker.com/ for build another expression. e.g. 0 0/1 * 1/1 * ? * every minute for to see the output. See also Cron Expressions.

like image 123
Paul Vargas Avatar answered Sep 20 '22 00:09

Paul Vargas


  1. Download quartz Jar Put in lib folder build project
  2. Create Class (Job) from which you want to schedule task

    import org.apache.log4j.Logger;
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    public class MyJob implements Job  {
        private Logger log = Logger.getLogger(MyJob.class);
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
    
            log.debug("Hi....");
            System.out.println("Corn Executing.....");
    
        }
    }
    
  3. Create Class for schedule your task

    import org.quartz.JobBuilder;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.SchedulerFactory;
    import org.quartz.SimpleScheduleBuilder;
    import org.quartz.Trigger;
    import org.quartz.TriggerBuilder;
    import org.quartz.impl.StdSchedulerFactory;
    
    public class JobScheduler {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("myjob").build();
    
                Trigger trigger = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(30).repeatForever()).build();
    
                SchedulerFactory schFactory = new StdSchedulerFactory();
                Scheduler scheduler = schFactory.getScheduler(); 
                scheduler.start();
                scheduler.scheduleJob(job, trigger);
    
            }catch (SchedulerException e) {
    
                e.printStackTrace();
            }
        }
    }
    
like image 22
Pankaj Girme Avatar answered Sep 19 '22 00:09

Pankaj Girme