java - 使用 Quartz 来安排作业

标签 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)。
我完全困惑了。我已经添加了所需的所有 jar 。

最佳答案

它无法编译,因为您忘记导入正确的类。 这可能会修复它:

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

关于java - 使用 Quartz 来安排作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9799945/

相关文章:

java - 我们可以像在本例中那样仅使用 List 及其大小来检查复杂的条件吗?

java - 在jrxml中导入自定义类并使用maven插件编译

Android 应用程序没有响应后退按钮

c# - windows调度器调度后如何获取安装的exe文件系统路径?

linux - 安排作业从 ftp 下载文件

java Quartz2 cron触发器没有立即触发

java - 如何以独立模式启动 Quartz?

java - 添加到 ArrayList 时是否会转换对象?

Java-Spring-quartz :why is the job being fired in a loop?

java - 无法安全调用 Web 服务