Scheduler는 동시 액세스 문제를 피하기 위해 실행될 때마다 JobDetail을 기반으로 새 Job 인스턴스를 생성합니다(jobDetail 인스턴스도 새 항목입니다).
Quzrtz 예약된 작업은 기본적으로 동시에 실행되며 기다리지 않습니다. 이전 작업 실행이 완료되면 간격이 끝나는 대로 실행됩니다. 예약된 작업이 너무 오랫동안 실행되면 리소스를 오랫동안 점유하고 다른 작업이 차단됩니다. 작업 클래스에서는 동일한 작업 정의(JobDetail에 의해 정의됨)의 동시 실행이 금지됩니다.
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; public class TaskClient { public static void main(String[] args) { JobDetail jobDetail = JobBuilder.newJob(TaskJob.class) .withIdentity("job1", "group1") //设置JOB的名字和组 .build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "trigger1") .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1) .repeatForever()) .build(); try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.scheduleJob(jobDetail,trigger); scheduler.start(); } catch (SchedulerException ex) { ex.printStackTrace(); } } }
TaskJob.Java
import cn.hutool.core.date.DateUtil; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class TaskJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("TaskJob => " + DateUtil.now()); } }
usingJobData
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; public class TaskClient { public static void main(String[] args) { JobDetail jobDetail = JobBuilder.newJob(TaskJob.class) .withIdentity("job1", "group1") .usingJobData("job","jobDetail1.JobDataMap.Value") .build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "trigger1") .usingJobData("trigger","trigger.JobDataMap.Value") .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1) .repeatForever()) .build(); try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.scheduleJob(jobDetail,trigger); scheduler.start(); } catch (SchedulerException ex) { ex.printStackTrace(); } } }
TaskJob.java
import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class TaskJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); JobDataMap triggerMap = context.getTrigger().getJobDataMap(); JobDataMap mergeMap = context.getMergedJobDataMap(); System.out.println("jobDataMap => " + jobDataMap.getString("job")); System.out.println("triggerMap => " + triggerMap.getString("trigger")); System.out.println("mergeMap => " + mergeMap.getString("trigger")); } }
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class TaskClient {
public static void main(String[] args) {
JobDetail jobDetail = JobBuilder.newJob(TaskJob.class)
.withIdentity("job1", "group1")
.usingJobData("job","jobDetail1.JobDataMap.Value")
.usingJobData("name","jobDetail1.name.Value") //通过 setName 自动赋值
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "trigger1")
.usingJobData("trigger","trigger.JobDataMap.Value")
.usingJobData("name","trigger.name.Value") //如果 Trigger 有值,会覆盖 JobDetail
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1)
.repeatForever())
.build();
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
} catch (SchedulerException ex) {
ex.printStackTrace();
}
}
}
import org.quartz.*;
public class TaskJob implements Job {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("name => " + name);
}
}
import cn.hutool.core.date.DateUtil; import org.quartz.*; @DisallowConcurrentExecution public class TaskJob implements Job { @Override public void execute(JobExecutionContext context) { System.out.println("Time => " + DateUtil.now()); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }
@PersistJobDataAfterExecution
작업이
import cn.hutool.core.date.DateUtil; import org.quartz.*; //持久化JobDetail中的JobDataMap(对 trigger 中的 datamap 无效),如果一个任务不是 @PersistJobDataAfterExecution public class TaskJob implements Job { @Override public void execute(JobExecutionContext context) { JobDataMap triggerMap = context.getJobDetail().getJobDataMap(); triggerMap.put("count", triggerMap.getInt("count") + 1); System.out.println("Time => " + DateUtil.now() + " count =>" + triggerMap.getInt("count")); } }
Client
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; public class TaskClient { public static void main(String[] args) { JobDetail jobDetail = JobBuilder.newJob(TaskJob.class) .withIdentity("job1", "group1") .usingJobData("job","jobDetail1.JobDataMap.Value") .usingJobData("name","jobDetail1.name.Value") //通过 setName 自动赋值 .usingJobData("count",0) //通过 setName 自动赋值 .build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "trigger1") .usingJobData("trigger","trigger.JobDataMap.Value") .usingJobData("name","trigger.name.Value") //如果 Trigger 有值,会覆盖 JobDetail .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1) .repeatForever()) .build(); try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.scheduleJob(jobDetail,trigger); scheduler.start(); } catch (SchedulerException ex) { ex.printStackTrace(); } } }
위 내용은 Quartz를 사용하여 Java에서 예약된 작업을 구현하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!