使用Quartz安排作业

时间:2012-03-21 06:58:15

标签: java scheduled-tasks quartz-scheduler

我刚刚开始学习Quartz调度,在第一步中我遇到了问题 我在其主要网站上查看它的示例,但是当我尝试在我的工作区中开发它时,它给了我错误。

package testing.quartz.scheduler;

    import java.util.Date;
    import java.util.logging.Logger;

    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerFactory;
    import org.quartz.Trigger;
    import org.quartz.impl.StdSchedulerFactory;

    public class TesterMain {

        /**
         * @param args
         */
         public void run() throws Exception {


                // First we must get a reference to a scheduler
                SchedulerFactory sf = new StdSchedulerFactory();
                Scheduler sched = sf.getScheduler();


                // computer a time that is on the next round minute
                Date runTime = evenMinuteDate(new Date());<--Here its giving me error


                // define the job and tie it to our HelloJob class
                JobDetail job = newJob(HelloJob.class)<--Here its giving me error
                    .withIdentity("job1", "group1")
                    .build();

                // Trigger the job to run on the next round minute
                Trigger trigger = newTrigger()<--Here its giving me error
                    .withIdentity("trigger1", "group1")
                    .startAt(runTime)
                    .build();

                // Tell quartz to schedule the job using our trigger
                sched.scheduleJob(job, trigger);

                // Start up the scheduler (nothing can actually run until the 
                // scheduler has been started)
                sched.start();


                // wait long enough so that the scheduler as an opportunity to 
                // run the job!
                try {
                    // wait 65 seconds to show job
                    Thread.sleep(65L * 1000L); 
                    // executing...
                } catch (Exception e) {
                }

                // shut down the scheduler
                sched.shutdown(true);
            }

            public static void main(String[] args) throws Exception {

               TesterMain example = new TesterMain();
                example.run();

            }

    }

我已经指定了给我编译错误的地方。告诉你这些方法并不是你班上的。
所以我想知道这些方法是否真的有效(newTrigger,newJob,evenMinuteDate)。我很困惑。我已经添加了所有必需的罐子。

3 个答案:

答案 0 :(得分:6)

它无法编译,因为您忘记导入正确的类。 这可能解决了这个问题:

import static org.quartz.DateBuilder.*;
import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;

答案 1 :(得分:1)

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.DateBuilder.evenMinuteDate;

将这些课程导入您的班级。

答案 2 :(得分:0)

Date runTime = evenMinuteDate(new Date());

evenMinuteDate方法未在任何地方声明。

JobDetail job = newJob(HelloJob.class);

未导入HelloJob类,并且在newJob之间缺少空格

Trigger trigger = newTrigger()

您错过了newTrigger()

之间的空格