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

发布于 2024-10-09 16:47:51 字数 1174 浏览 4 评论 0原文

我正在学习石英调度程序框架,作为基础,我从定期打印的“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..

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁许谁一生繁华 2024-10-16 16:47:51

只需在安排要运行的作业后放置 scheduler.start() - scheduler.scheduleJob...

更新:我接受组织的更正.life.java。语句的顺序不会产生太大的影响。您的麻烦根源在于 shutdown() 调用。调度程序的合约 [javadoc ] 的意思是只要未对其发出明确的关闭命令,就会继续运行。如果您从代码中删除该行,它就可以正常工作。

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.

一口甜 2024-10-16 16:47:51

我从头开始创建它并且运行良好。!!
我建议您将您的代码与此进行比较,并在 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());
    }
}

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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文