在 Java 中使用 Quartz 实现动态添加和删除任务,你可以采用以下几种方式:使用 JobDetail
和 Trigger
、使用 CronTriggerFactoryBean
、使用 SchedulerFactoryBean
。
以下是每种方式的详细步骤、代码示例和相关依赖坐标:
步骤流程:
Job
接口的任务类,其中实现 execute
方法定义具体任务逻辑。JobDetail
实例,将任务类与其绑定。Trigger
实例,设置触发任务的时间规则。Scheduler
实例,将 JobDetail
和 Trigger
添加到调度器中。Maven 依赖:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
Gradle 依赖:
implementation 'org.quartz-scheduler:quartz:2.3.2'
示例代码:
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzDynamicAddDelete {
public static void main(String[] args) throws SchedulerException, InterruptedException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
// Create JobDetail
JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
.withIdentity("myJob", "group1")
.build();
// Create Trigger
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10)
.repeatForever())
.build();
// Schedule the job
scheduler.scheduleJob(jobDetail, trigger);
// Wait for a while
Thread.sleep(30000);
// Delete the job
scheduler.deleteJob(jobDetail.getKey());
scheduler.shutdown();
}
}
class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Executing job at: " + new Date());
}
}
步骤流程:
Job
接口的任务类。CronTriggerFactoryBean
和 JobDetailFactoryBean
,并将它们与任务类绑定。Maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
Gradle 依赖:
implementation 'org.springframework.boot:spring-boot-starter-quartz'
示例代码:
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Executing job at: " + new Date());
}
}
Spring 配置文件 application.properties
或 application.yml
:
spring.quartz.job-store-type=jdbc
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
@Configuration
public class QuartzConfig {
@Bean
public JobDetailFactoryBean jobDetail() {
JobDetailFactoryBean factory = new JobDetailFactoryBean();
factory.setJobClass(MyJob.class);
factory.setGroup("group1");
return factory;
}
@Bean
public CronTriggerFactoryBean cronTrigger(JobDetailFactoryBean jobDetail) {
CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
factory.setJobDetail(jobDetail.getObject());
factory.setStartDelay(0L);
factory.setName("myTrigger");
factory.setGroup("group1");
factory.setCronExpression("0/10 * * * * ?");
return factory;
}
}
步骤流程:
Job
接口的任务类。SchedulerFactoryBean
,并在其中设置触发器和任务。Maven 依赖和示例代码同上一种方式。
Spring 配置文件 application.properties
或 application.yml
:
spring.quartz.job-store-type=jdbc
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class QuartzConfig {
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
schedulerFactory.setTriggers(cronTrigger().getObject());
return schedulerFactory;
}
@Bean
public CronTriggerFactoryBean cronTrigger() {
CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
factory.setJobDetail(jobDetail().getObject());
factory.setStartDelay(0L);
factory.setName("myTrigger");
factory.setGroup("group1");
factory.setCronExpression("0/10 * * * * ?");
return factory;
}
@Bean
public JobDetailFactoryBean jobDetail() {
JobDetailFactoryBean factory = new JobDetailFactoryBean();
factory.setJobClass(MyJob.class);
factory.setGroup("group1");
return factory;
}
}
请注意,以上示例中的 Spring 相关配置假定你正在使用 Spring 框架。这些示例仅仅是基础示例,实际使用时可能需要根据业务逻辑和需求进行适当的调整。