项目中一直是用默认配置,没有深入了解。项目中用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());
最终持久化到数据库中