user2597504
user2597504

Reputation: 1533

java quartz scheduler run at specific time

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?

Upvotes: 8

Views: 22659

Answers (4)

Pankaj Girme
Pankaj Girme

Reputation: 11

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

Upvotes: 1

pappu_kutty
pappu_kutty

Reputation: 2488

you can create cron expression for this. to have quartz job you need to have following objects

  1. Job
  2. Task which will be associated to a Job
  3. Finally create a trigger and associate a Job to the trigger

Triggers of two type

Simple triggers, where you can control job , you can run every min or 10 mins and so on. you can also have additional parameters

initial delay - to kick off

repeatcount - no of times the job should be executes, if -1 then job will be executed infinitely

In your case you can use cron triggers since you want to run every day at 12 am.

For more details and sample program look at this below link

http://www.mkyong.com/spring/spring-quartz-scheduler-example/

and about quartz cron expression , see the quartz documentation

http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

Upvotes: 0

Paul Vargas
Paul Vargas

Reputation: 42010

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.

Upvotes: 9

coder
coder

Reputation: 4466

You have to create your custom job by implementing Job interface and providing your implementation of execute method.In execute method you can print "hello world". Then you can schedule your job like this

scheduler.scheduleJob(job, trigger);

Refer this link for step by step details: Quartz tutorial

Upvotes: 0

Related Questions