执行简单石英调度程序时出现问题

时间:2010-12-29 07:34:22

标签: java quartz-scheduler scheduler

我正在学习石英调度程序框架,作为基础,我开始使用“Hello World”打印常规间隔。

这是我的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) {

    }

}
}

这是我的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());
}

}

输出结果是

Scheduler Started...
Scheduler Stopped..

我没有得到所需的输出。我在控制台中运行它。我需要做任何配置或什么?请帮帮我

2 个答案:

答案 0 :(得分:1)

在安排作业运行后scheduler.start()放置scheduler.scheduleJob...

更新:我对org.life.java更正了。陈述的顺序不会产生太大影响。麻烦的来源是shutdown()调用。只要未在其上发出明确的 shutdown 命令,调度程序的合同[javadoc]就会继续运行。如果从代码中删除该行,则可以正常工作。

答案 1 :(得分:1)

我是从零开始创建的,效果很好。!!
我建议你将你的代码与这个ans比较,并在catch中记录异常,这样你就会有好主意。

<强> 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) {
        }
    }
}

<强>作业

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