i2ijeya
i2ijeya

Reputation: 16430

Problem while executing simple quartz scheduler

I am learning quartz scheduler framework and as a base I have started with "Hello World" thats prints at regular Intervals.

This is my SampleScheduler

public class SampleScheduler {
public static void main(String arfs[]) {
    try {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();
        System.out.println("Scheduler Started...");

        JobDetail job = new JobDetail("job1","group1",SampleJobInter.class);
        Trigger trigger = new SimpleTrigger("trigger1",Scheduler.DEFAULT_GROUP,new Date(),null,SimpleTrigger.REPEAT_INDEFINITELY,60L*1000L);
        scheduler.scheduleJob(job, trigger);
        scheduler.shutdown();
        System.out.println("Scheduler Stopped..");  
    } catch(SchedulerException e) {

    }

}
}

Here is my SampleJobInter.class

public class SampleJobInter implements Job {

SampleJobInter(){}
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
    // TODO Auto-generated method stub
    System.out.println("Hello World at "+new Date());
}

}

The output am getting is

Scheduler Started...
Scheduler Stopped..

I am not getting the desired output. I am running it in the console. Do I need to do any configurations or what?. Please Help me in this

Upvotes: 1

Views: 2738

Answers (2)

anirvan
anirvan

Reputation: 4877

just put scheduler.start() after you have scheduled a job to run - scheduler.scheduleJob...

UPDATE: I stand corrected by org.life.java. The order of statements won't make much of a difference. The source of your troubles is the shutdown() invocation. A scheduler's contract [javadoc] is to keep running as long as an explicit shutdown command is not issued on it. if you remove that line from your code, it works fine.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240996

I have created it from scratch and it works well.!!
I would suggest you to compare your code with this ans also log exception in catch so that you will have good idea.

JobRunner

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.life.java.so.questions;

/**
 *
 * @author Jigar
 */
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;



public class HelloSchedule {

    public HelloSchedule() throws Exception {
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();
        sched.start();
        JobDetail jd = new JobDetail("myjob", sched.DEFAULT_GROUP, SampleJobInter.class);
        SimpleTrigger st = new SimpleTrigger("mytrigger", sched.DEFAULT_GROUP, new Date(),
                null, SimpleTrigger.REPEAT_INDEFINITELY, 100L);
        sched.scheduleJob(jd, st);
    }

    public static void main(String args[]) {
        try {
            new HelloSchedule();
        } catch (Exception e) {
        }
    }
}

Job

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.life.java.so.questions;

import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 *
 * @author Jigar
 */
public class SampleJobInter implements Job {

    public SampleJobInter() {
    }

    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("Hello World at " + new Date());
    }
}

Upvotes: 1

Related Questions