quartz

项目中一直是用默认配置,没有深入了解。项目中用RAM-based scheduler,还有其他类型scheduler如数据库持久化
初始化配置,修改线程池参数等。默认当前工作目录加载quartz.props,没有加载jar中props。配置文件优于代码初始化,方便改动

一、初始化

1 代码
Properties props = new Properties();
    props.put(StdSchedulerFactory.PROP_THREAD_POOL_CLASS,
            "org.quartz.simpl.SimpleThreadPool");
    props.put("org.quartz.threadPool.threadCount", "50");       //任务线程个数

    StdSchedulerFactory factory = new StdSchedulerFactory();
    factory.initialize(props);
2、配置文件quartz.properties
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 20
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

org.quartz.jobStore.misfireThreshold: 60000

org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore  

二、@DisallowConcurrentExecution

禁止并发执行多个相同定义的JobDetail, 这个注解是加在Job类上的

三、持久化到数据

得有驱动jar包mysql-connector-java-5.1.6.jar,版本太高也不行;还得建立持久相关库表;配置文件quartz.properties如下

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource = quartz

org.quartz.dataSource.quartz.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.quartz.URL = jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=utf-8
org.quartz.dataSource.quartz.user = ***
org.quartz.dataSource.quartz.password = ***
org.quartz.dataSource.quartz.maxConnections = 5

//第一次初始化
sched.scheduleJob(job, trigger);
//初始化后启动
sched.resumeJob(job.getKey());

最终持久化到数据库中
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值