quartz 动态创建定时任务 内存方式 动态创建

解决问题:

不写入数据库;动态创建定时任务

1 引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

2 配置文件

  quartz:
    job-store-type: jdbc
    properties:
      org:
        quartz:
          scheduler:
            #调度器实例编号自动生成
            instanceId: AUTO
          jobStore:
            #是否加入集群
            isClustered: true
            #调度实例失效的检查时间间隔
            clusterCheckinInterval: 10000
            # 在调度流程的第一步,也就是拉取待即将触发的triggers时,是上锁的状态,即不会同时存在多个线程拉取到相同的trigger的情况,也就避免的重复调度的危险
            acquireTriggersWithinLock: true

3解释

查看一下 QuartzProperties  类
JobStoreType : 需要关注的 默认是 MEMORY 适应生产需要必须改为 JDBC 否则服务器一旦重启定时任务全部丢失
package org.springframework.boot.autoconfigure.quartz;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceInitializationMode;

/**
 * Configuration properties for the Quartz Scheduler integration.
 *
 * @author Vedran Pavic
 * @author Stephane Nicoll
 * @since 2.0.0
 */
@ConfigurationProperties("spring.quartz")
public class QuartzProperties {

	/**
	 * Quartz job store type.
	 */
	private JobStoreType jobStoreType = JobStoreType.MEMORY;

	/**
	 * Name of the scheduler.
	 */
	private String schedulerName;

	/**
	 * Whether to automatically start the scheduler after initialization.
	 */
	private boolean autoStartup = true;

	/**
	 * Delay after which the scheduler is started once initialization completes. Setting
	 * this property makes sense if no jobs should be run before the entire application
	 * has started up.
	 */
	private Duration startupDelay = Duration.ofSeconds(0);

	/**
	 * Whether to wait for running jobs to complete on shutdown.
	 */
	private boolean waitForJobsToCompleteOnShutdown = false;

	/**
	 * Whether configured jobs should overwrite existing job definitions.
	 */
	private boolean overwriteExistingJobs = false;

	/**
	 * Additional Quartz Scheduler properties.
	 */
	private final Map<String, String> properties = new HashMap<>();

	private final Jdbc jdbc = new Jdbc();

	public JobStoreType getJobStoreType() {
		return this.jobStoreType;
	}

	public void setJobStoreType(JobStoreType jobStoreType) {
		this.jobStoreType = jobStoreType;
	}

	public String getSchedulerName() {
		return this.schedulerName;
	}

	public void setSchedulerName(String schedulerName) {
		this.schedulerName = schedulerName;
	}

	public boolean isAutoStartup() {
		return this.autoStartup;
	}

	public void setAutoStartup(boolean autoStartup) {
		this.autoStartup = autoStartup;
	}

	public Duration getStartupDelay() {
		return this.startupDelay;
	}

	public void setStartupDelay(Duration startupDelay) {
		this.startupDelay = startupDelay;
	}

	public boolean isWaitForJobsToCompleteOnShutdown() {
		return this.waitForJobsToCompleteOnShutdown;
	}

	public void setWaitForJobsToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown) {
		this.waitForJobsToCompleteOnShutdown = waitForJobsToCompleteOnShutdown;
	}

	public boolean isOverwriteExistingJobs() {
		return this.overwriteExistingJobs;
	}

	public void setOverwriteExistingJobs(boolean overwriteExistingJobs) {
		this.overwriteExistingJobs = overwriteExistingJobs;
	}

	public Map<String, String> getProperties() {
		return this.properties;
	}

	public Jdbc getJdbc() {
		return this.jdbc;
	}

	public static class Jdbc {

		private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/quartz/impl/"
				+ "jdbcjobstore/tables_@@platform@@.sql";

		/**
		 * Path to the SQL file to use to initialize the database schema.
		 */
		private String schema = DEFAULT_SCHEMA_LOCATION;

		/**
		 * Database schema initialization mode.
		 */
		private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED;

		/**
		 * Prefixes for single-line comments in SQL initialization scripts.
		 */
		private List<String> commentPrefix = new ArrayList<>(Arrays.asList("#", "--"));

		public String getSchema() {
			return this.schema;
		}

		public void setSchema(String schema) {
			this.schema = schema;
		}

		public DataSourceInitializationMode getInitializeSchema() {
			return this.initializeSchema;
		}

		public void setInitializeSchema(DataSourceInitializationMode initializeSchema) {
			this.initializeSchema = initializeSchema;
		}

		public List<String> getCommentPrefix() {
			return this.commentPrefix;
		}

		public void setCommentPrefix(List<String> commentPrefix) {
			this.commentPrefix = commentPrefix;
		}

	}

}

4 动态增加定时任务

        JobDetail build = JobBuilder.newJob(ValidSourceSearchJob.class).withIdentity("Trigger_" + 123, "product-group").build();
        // 构建参数
        build.getJobDataMap().put("id", 123);
        // 构建 Trigger
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("Trigger_" + 123, "product-group").startAt(DateUtils.addDays(new Date(), days)).build();
        try {
            scheduler.scheduleJob(build, trigger);
            scheduler.start();
            
            logger.info("动态添加定时end:{}", .getId());
        } catch (SchedulerException e) {
            logger.error("添加定时任务异常:{}",.getId());
        }

5 删除定时任务

    @Autowired
    private Scheduler scheduler;        

//...
try {
            TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
            // 停止触发器
            scheduler.pauseTrigger(triggerKey);
            // 移除触发器
            scheduler.unscheduleJob(triggerKey);
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
            logger.info("删除定时任务完成:{}", jobName);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
//...

Quartz是一个功能强大的定时任务框架,可以用于动态创建定时任务。下面是一个示例代码,演示了如何使用Quartz动态创建定时任务: ```java import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; public class QuartzDynamicTask { public static void main(String[] args) throws SchedulerException { // 创建调度器 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); // 启动调度器 scheduler.start(); // 创建JobDetail JobDetail jobDetail = JobBuilder.newJob(MyJob.class) .withIdentity("myJob", "group1") .build(); // 创建Trigger Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) // 每5秒执行一次 .build(); // 将JobDetail和Trigger加入调度器 scheduler.scheduleJob(jobDetail, trigger); } public static class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 定时任务的逻辑 System.out.println("Hello, Quartz!"); } } } ``` 上述代码中,我们首先创建了一个调度器(Scheduler),然后创建了一个JobDetail,用于定义具体的任务逻辑。接着,我们创建了一个Trigger,用于定义触发任务的时间规则。最后,将JobDetail和Trigger加入调度器中,调度器会按照Trigger定义的时间规则执行任务。 需要注意的是,上述代码是Java代码,使用了QuartzJava API。如果你想在其他语言中使用Quartz,可以查阅相应的文档和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值