gpt4 book ai didi

java - Quartz 工作没有开始

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:54 28 4
gpt4 key购买 nike

编辑:我正在使用quartz-2.1.5.jar。这是我的类(class)摘要:

HttpPollingJob extends PollingJob extends ScheduledJob implements org.quartz.Job

具体来说:

1) ScheduledJob 实现了 Quartz Job(我所有的 Job 类型的抽象基类):

import org.quartz.Job;
import org.quartz.Trigger;

public abstract class ScheduledJob implements Job {
private Trigger trigger;

public ScheduledJob() {
this(null);
}

public ScheduledJob(Trigger trig) {
super();

if(trig == null)
trig = getDefaultTrigger();

setTrigger(trig);
}

public Trigger getTrigger() {
return trigger;
}

public void setTrigger(final Trigger trig) {
trigger = trig;
}

protected abstract Trigger getDefaultTrigger();
}

2) PollingJob extends ScheduledJob - 所有“轮询器”以特定频率轮询某些资源/端点:

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

import com.me.jobs.ScheduledJob;

public abstract class PollingJob extends ScheduledJob {
private static long DEF_FREQUENCY = 10 * 1000; // 10 secs
private String name;
private long frequency;

public PollingJob(final String nm) {
this(nm, DEF_FREQUENCY);
}

public PollingJob(final String nm, final long freq) {
super();

setName(nm);
setFrequency(freq);
}

public abstract void poll(JobExecutionContext context);

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
poll(context);
}

public String getName() {
return name;
}

public void setName(final String nm) {
name = nm;
}

public long getFrequency() {
return frequency;
}

public void setFrequency(final long freq) {
frequency = freq;
}

protected final Trigger getDefaultTrigger() {
TriggerBuilder<?> triggerBuilder = TriggerBuilder.newTrigger()
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMilliseconds(DEF_FREQUENCY));

return triggerBuilder.build();
}
}

3) HttpPollingJob extends PollingJob - “HTTP pollers” 轮询网络服务器(使用 HTTPClient):

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.quartz.JobExecutionContext;

import com.me.MonitoredEvent;
import com.me.MonitoredEventRegistrar;

public class HttpPollingJob extends PollingJob {
private String serverURL;
private org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(HttpPollingJob.class);

public HttpPollingJob(final String nm, final String server) {
super(nm);

setServerURL(server);
}

public String getServerURL() {
return serverURL;
}

public void setServerURL(final String server) {
serverURL = server;
}

@Override
public final void poll(JobExecutionContext context) {
MonitoredEvent event = null;

try {
// This is where we would use HttpClient to connect to a web server and poll it.
System.out.println("Job fired!");
}
catch(Throwable thrown) {
logger.error(thrown.getMessage());
}
}
}

4) JobDriver - 定义几个 HttpPollingJob 并使用 Quartz 启动它们:

public class JobDriver {
private List<HttpPollingJob> jobs;

public JobDriver() {
HttpPollingJob job1 = new HttpPollingJob("job-1", "http://www.example.com/1");
HttpPollingJob job2 = new HttpPollingJob("job-2", "http://www.example.com/2");
HttpPollingJob job3 = new HttpPollingJob("job-3", "http://www.example.com/3");

jobs = new ArrayList<HttpPollingJob>();
jobs.add(job1);
jobs.add(job2);
jobs.add(job3);
}

public static void main(String[] args) {
JobDriver driver = new JobDriver();
driver.startJobs();
}

private void startJobs() {
try {
// Obtain a basic SchedulerFactory and fire it up.
SchedulerFactory schedulerFactory = new org.quartz.impl.StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();

// Define a job for every declared monitor.
JobBuilder jobBuilder = null;

for(ScheduledJob job : jobs) {

Trigger trigger = job.getTrigger();
jobBuilder = JobBuilder.newJob(job.getClass());

// Bind the current job to this trigger.
scheduler.scheduleJob(jobBuilder.build(), trigger);

// TODO: Shut the scheduler down politely?!?!
}
catch(Throwable exc) {
logger.error(exc.getMessage());

// Force application to kick out.
throw new RuntimeException(exc);
}
}
}

当我运行这段代码时,我得到了一个完美的启动,没有错误或运行时异常。如果我使用 System.out.println 语句,我可以看到每一行代码都在完美执行。唯一的问题是,程序运行后,它不会打印“Job fired!”消息,表明轮询作业正在启动。

我已经尝试了所有我能想到的 start()shutdown() 的组合,但都无济于事。任何 Quartz 专家都可以查看此代码并告诉我为什么作业没有触发吗?

在日志中(我配置了 log4j),我看到正在为计划作业创建 Quartz 工作线程。我觉得我已经完成了 99%,但只是遗漏了一些明显的东西。提前致谢!

最佳答案

@4herpsand7derpsago 是的,你是对的,你已经完成了 99%,我运行了你的代码,其中只有一个问题,默认构造函数在 HttpPollingJob 中不存在。和 PollingJob类,因此调度程序无法创建它们的实例,

简单的解决方案在下面的类中添加以下代码
HttpPollingJob

public HttpPollingJob() {
}

PollingJob

public PollingJob() {
}

Bingo,将打印以下消息

Job fired!
Job fired!
Job fired!

如果想重复触发,在PollingJob中添加如下代码

protected final Trigger getDefaultTrigger() {
TriggerBuilder<?> triggerBuilder = TriggerBuilder
.newTrigger()
.startNow()
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMilliseconds(DEF_FREQUENCY).repeatForever());

return triggerBuilder.build();
}

希望现在我能收到赏金:)

奖金
似乎您想对 url 进行轮询或做一些事情,使用 king 传递它的更好方法 JobDataMap

更新了JobDriver

import java.util.ArrayList;
import java.util.List;

import org.quartz.JobBuilder;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;

public class JobDriver {
private List<HttpPollingJob> jobs;

public JobDriver() {
HttpPollingJob job1 = new HttpPollingJob("job-1",
"http://www.example.com/1");
HttpPollingJob job2 = new HttpPollingJob("job-2",
"http://www.example.com/2");
HttpPollingJob job3 = new HttpPollingJob("job-3",
"http://www.example.com/3");

jobs = new ArrayList<HttpPollingJob>();
jobs.add(job1);
jobs.add(job2);
jobs.add(job3);
}

public static void main(String[] args) {
JobDriver driver = new JobDriver();
driver.startJobs();
}

private void startJobs() {
try {
// Obtain a basic SchedulerFactory and fire it up.
SchedulerFactory schedulerFactory = new org.quartz.impl.StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
// Define a job for every declared monitor.
JobBuilder jobBuilder = null;

for (HttpPollingJob job : jobs) {

Trigger trigger = job.getTrigger();
jobBuilder = JobBuilder.newJob(job.getClass());
jobBuilder.usingJobData("name", job.getName());
jobBuilder.usingJobData("url", job.getServerURL());

// Bind the current job to this trigger.
scheduler.scheduleJob(jobBuilder.build(), trigger);

// TODO: Shut the scheduler down politely?!?!
}


} catch (Throwable exc) {
// Force application to kick out.
throw new RuntimeException(exc);
}
}
}

更新HttpPollingJob

import java.util.Map;

import org.quartz.JobExecutionContext;

public class HttpPollingJob extends PollingJob {
private String serverURL;
private org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(HttpPollingJob.class);

public HttpPollingJob(final String nm, final String server) {
super(nm);

setServerURL(server);
}
public HttpPollingJob() {
}

public String getServerURL() {
return serverURL;
}

public void setServerURL(final String server) {
serverURL = server;
}

@Override
public final void poll(JobExecutionContext context) {

try {
Map dataMap = context.getJobDetail().getJobDataMap();
String nm = (String)dataMap.get("name");
String url = (String)dataMap.get("url");
// This is where we would use HttpClient to connect to a web server and poll it.
System.out.println("Job fired! name:"+nm+" url:"+url);
}
catch(Throwable thrown) {
logger.error(thrown.getMessage());
}
}
}

新输出

Job fired! name:job-1 url:http://www.example.com/1
Job fired! name:job-2 url:http://www.example.com/2
Job fired! name:job-3 url:http://www.example.com/3

关于java - Quartz 工作没有开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11208327/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com