Quartz 2.0 分布式任务调度

业务需求:

选型对比:

Quartz 2.0 发布了,Quartz 是一个 Java 轻量级开源企业级的作业调度框架,来自 Quartz 开发团队的信息,这是 Quartz 有史以来最大的、最值得兴奋的一个版本。

该版本除了在性能上有所提升外,增加了如下新特性:

Scheduler.clear() 提供方便用于清除所有任务、触发器和日程的方法
Scheduler.scheduleJobs(Map<JobDetail, List> triggersAndJobs, boolean replace) 方法可批量增加任务和触发器
Scheduler.unscheduleJobs(List triggerKeys) 方法提供批量取消任务的
Scheduler.deleteJobs(List jobKeys) ,不用说,这是批量删除任务的
Scheduler.checkExists(JobKey jobKey) 和 Scheduler.heckExists(TriggerKey triggerKey) 提供用于检测任务关键字的唯一性
JDBCJobStore now allows one set of tables to be used by multiple distinct scheduler instances
JDBCJobStore is now capable of storing non-core Trigger implementations without using BLOB columns, through the use of the new TriggerPersistenceDelegate interface, which can (optionally) be implemented by implementers of custom Trigger types.
JDBCJobStore 包含了 SybaseDelegate 用以提升跟 Sybase 数据库的兼容性
Cron 表达式支持指定每月的最后一天和最后一周,例如 L-3 为每月的最后三天
包含调度信息的 XML 文件增加了用来指定启动时间和间隔时间的方法
From schema: <xs:element name=“start-time-seconds-in-future” type=“xs:nonNegativeInteger”/>
XML 文件支持为触发器指定 priority 属性
QuartzInitializerListener (and QuartzInitializerServlet) 支持新的参数 “wait-on-shutdown”
增加核心任务 DirectoryScanJob
Added ability to add the ServletContext into the SchedulerContext when using QuartzInitializerListener or QuartzInitializerServlet to initialize Quartz within a Java EE web application.
另外Quartz 2.0 不再使用 DBCP 做为数据库连接池,而改用了 C3P0。

JOB持久化策略 JDBCJobStore RAMJobStore
Job Persistence
The design of Quartz includes a JobStore interface that can be implemented to provide various mechanisms for the storage of jobs.

With the use of the included JDBCJobStore, all Jobs and Triggers configured as “non-volatile” are stored in a relational database via JDBC.

With the use of the included RAMJobStore, all Jobs and Triggers are stored in RAM and therefore do not persist between program executions - but this has the advantage of not requiring an external database.

SchedulerConfiguration( Spring配置 支持RAM和JDBC持久化)

package com.kxtx.xks.common.quartz;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;

/**
 * 注意事项
 * 1. 使用之前需要命名SchedulerFactoryBean名称  命名规则请使用服务名称 eg: kxtx-demo-order
 * 2. yml配置添加
 * quartz:
 * is_durable: true
 * threadPool:
 * threadCount: 10
 * scheduler:
 * instanceName: instanceName_example
 */
@Configuration
public class SchedulerConfiguration {

    @Autowired(required = false)
    private XKJobFactory xkJobFactory;
    //读取yml配置信息
    @Value("${spring.application.name:DEFAULT_NAME}")
    private String applicationName;
    @Value("${spring.datasource.username:}")
    private String username;
    @Value("${spring.datasource.password:}")
    private String password;
    @Value("${spring.datasource.driver-class-name:}")
    private String driver;
    @Value("${spring.datasource.url:}")
    private String url;
    @Value("${quartz.scheduler.instanceName:}")
    private String instanceName;
    @Value("${quartz.threadPool.threadCount:10}")
    private String threadCount;
    @Value("${quartz.is_durable:false}")
    private Boolean is_durable;

    private static final String JOB_STORE_CLASS = "org.quartz.impl.jdbcjobstore.JobStoreTX";
    private static final String JOB_STORE_DRIVERDELEGATECLASS = "org.quartz.impl.jdbcjobstore.StdJDBCDelegate";
    private static final String JOB_STORE_USEPROPERTIES = "true";
    private static final String TABLE_PREFIX = "qrtz_";
    private static final String DATA_SOURCE = "qzDS";
    private static final String IS_CLUSTERED = "true";


    /**
     * 使用之前需要命名SchedulerFactoryBean名称  命名规则请使用服务名称 eg: kxtx-demo-order
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        //获取配置属性
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("quartz.properties"));//配置文件路径
        //在quartz.properties中的属性被读取并注入后再初始化对象
        propertiesFactoryBean.afterPropertiesSet();
        //创建SchedulerFactoryBean
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        Properties pro = propertiesFactoryBean.getObject();
        //基本数据配置
        instanceName=StringUtils.isEmpty(instanceName)?applicationName:instanceName;
        pro.setProperty("org.quartz.scheduler.instanceName", instanceName);

        pro.setProperty("org.quartz.scheduler.instanceId", "AUTO");
        pro.setProperty("org.quartz.scheduler.rmi.export", "false");
        pro.setProperty("org.quartz.scheduler.rmi.proxy", "false");
        pro.setProperty("org.quartz.scheduler.wrapJobExecutionInUserTransaction", "false");

        pro.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        pro.setProperty("org.quartz.threadPool.threadCount",threadCount);
        pro.setProperty("org.quartz.threadPool.threadPriority", "5");
        pro.setProperty("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
        pro.setProperty("org.quartz.jobStore.misfireThreshold", "60000");

        //设置数据库任务持久化
        if (is_durable && !StringUtils.isAnyEmpty(driver, url, username, password)) {
            pro.setProperty("org.quartz.dataSource.qzDS.driver", driver);
            pro.setProperty("org.quartz.dataSource.qzDS.URL", url);
            pro.setProperty("org.quartz.dataSource.qzDS.user", username);
            pro.setProperty("org.quartz.dataSource.qzDS.password", password);
            pro.setProperty("org.quartz.jobStore.class", JOB_STORE_CLASS);
            pro.setProperty("org.quartz.jobStore.driverDelegateClass", JOB_STORE_DRIVERDELEGATECLASS);
            pro.setProperty("org.quartz.jobStore.useProperties", JOB_STORE_USEPROPERTIES);
            pro.setProperty("org.quartz.jobStore.tablePrefix", TABLE_PREFIX);
            pro.setProperty("org.quartz.jobStore.dataSource", DATA_SOURCE);
            pro.setProperty("org.quartz.jobStore.isClustered", IS_CLUSTERED);
            pro.setProperty("org.quartz.jobStore.selectWithLockSQL", "SELECT * FROM {0}LOCKS WHERE SCHED_NAME = {1} AND LOCK_NAME = ? FOR UPDATE");
        }
        factory.setOverwriteExistingJobs(true);
        factory.setAutoStartup(true);
        factory.setQuartzProperties(pro);
        factory.setJobFactory(xkJobFactory);
        //设置bean name作为分布式服务任务标示
        factory.setBeanName(instanceName);
        return factory;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值