java quartz配置_java quartz简单使用

1.在项目里引入quartz

org.quartz-scheduler

quartz

2.3.0

2.quartz的简单实例

package com.example.quartz_demo;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import static org.quartz.JobBuilder.newJob;

import static org.quartz.TriggerBuilder.newTrigger;

import static org.quartz.SimpleScheduleBuilder.*;

import org.quartz.SchedulerException;

import org.quartz.Trigger;

import org.quartz.impl.StdSchedulerFactory;

/**

* Hello world!

*

*/

public class App

{

public static void main( String[] args )

{

try {

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

scheduler.start();

try {

Thread.sleep(100000);

} catch (InterruptedException e) {

e.printStackTrace();

}

scheduler.shutdown();

} catch (SchedulerException e) {

e.printStackTrace();

}

}

}

启动程序,默认会启动10个线程,我们可以在配置文件中设置quartz运行的相关属性,在resources目录下新建一个quartz.properties文件,添加

org.quartz.scheduler.instanceName = MyScheduler

org.quartz.threadPool.threadCount = 3

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

再次启动程序,启动的线程就变成3个了,一个配置项为调度器的名称。第三个为设置Quartz数据的存储方式

再看一个例子:

package com.example.quartz_demo;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import static org.quartz.JobBuilder.newJob;

import static org.quartz.TriggerBuilder.newTrigger;

import static org.quartz.SimpleScheduleBuilder.*;

import org.quartz.SchedulerException;

import org.quartz.Trigger;

import org.quartz.impl.StdSchedulerFactory;

/**

* Hello world!

*

*/

public class App

{

public static void main( String[] args )

{

try {

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

scheduler.start();

JobDetail job = newJob(HelloJob.class).

withIdentity("job1", "group1").

build();

Trigger trigger = newTrigger().

withIdentity("trigger1", "group1")

.startNow()

.withSchedule(simpleSchedule()

.withIntervalInSeconds(10)

.repeatForever())

.build();

scheduler.scheduleJob(job, trigger);

// try {

// Thread.sleep(100000);

// } catch (InterruptedException e) {

// e.printStackTrace();

// }

// scheduler.shutdown();

} catch (SchedulerException e) {

e.printStackTrace();

}

}

}

可以详细的设置任务的开始,频率等调度详情

3.trigger

withIntervalInSeconds秒级的任务

withRepeatCount指定运行次数后,仅仅运行argumnt+1次Job就停止运行Job

Trigger trigger = newTrigger()

.withIdentity("trigger3", "group1")

.startNow() // if a start time is not given (if this line were omitted), "now" is implied

.withSchedule(simpleSchedule()

.withIntervalInSeconds(1)

.withRepeatCount(3)) // note that 10 repeats will give a total of 11 firings

.build();

withIntervalInMinutes分钟级的Job

withIntervalInHours小时级的Job

endAt指定任务的结束时间,到期后停止Job的调度

Trigger trigger = newTrigger()

.withIdentity("trigger7", "group1")

.withSchedule(simpleSchedule()

.withIntervalInMinutes(5)

.repeatForever())

.endAt(dateOf(22, 0, 0))

.build();

4.使用数据库级别的存储任务

导入D:\UserData\Downloads\quartz-2.2.3\docs\dbTables\tables_mysql_innodb.sqlquartz包中的sql脚步到数据库中

ce062d96fdbe

sql文件导入到数据库中的表.png

项目中, pom.xml中引入mysql数据库的连接驱动

mysql

mysql-connector-java

5.1.35

配置 quartz.properties文件

org.quartz.scheduler.instanceName = MyScheduler

org.quartz.scheduler.instanceId = AUTO

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool

org.quartz.threadPool.threadCount = 10

org.quartz.threadPool.threadPriority = 5

org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

org.quartz.jobStore.tablePrefix = QRTZ_

org.quartz.jobStore.useProperties = true

org.quartz.jobStore.isClustered = false

org.quartz.jobStore.clusterCheckinInterval = 20000

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.dataSource = qzDs

org.quartz.dataSource.qzDs.driver = com.mysql.jdbc.Driver

org.quartz.dataSource.qzDs.URL = jdbc:mysql://localhost:3306/qurtz_db

org.quartz.dataSource.qzDs.user = root

org.quartz.dataSource.qzDs.password = 123456

org.quartz.dataSource.qzDs.maxConnections = 10

实例代码:

package com.example.quartz_demo;

import org.quartz.JobDetail;

import org.quartz.JobKey;

import org.quartz.Scheduler;

import static org.quartz.JobBuilder.newJob;

import static org.quartz.TriggerBuilder.newTrigger;

import static org.quartz.SimpleScheduleBuilder.*;

import org.quartz.SchedulerException;

import org.quartz.SchedulerFactory;

import org.quartz.Trigger;

import org.quartz.TriggerKey;

import org.quartz.impl.StdSchedulerFactory;

/**

* Hello world!

*

*/

public class App

{

public static void main( String[] args )

{

try {

SchedulerFactory factory = new StdSchedulerFactory();

Scheduler scheduler = factory.getScheduler();

scheduler.start();

TriggerKey tKey = new TriggerKey("trigger1", "group1");

JobKey jKey = new JobKey("job1", "group1");

if(scheduler.checkExists(tKey)){

Trigger trigger = scheduler.getTrigger(tKey);

scheduler.rescheduleJob(tKey, trigger);

}else{

JobDetail job = newJob(HelloJob.class).

withIdentity(jKey).

build();

Trigger trigger = newTrigger().

withIdentity(tKey)

.startNow()

.withSchedule(simpleSchedule()

.withIntervalInSeconds(10)

.repeatForever())

.build();

// Trigger trigger = newTrigger()

// .withIdentity("trigger3", "group1")

// .startNow() // if a start time is not given (if this line were omitted), "now" is implied

// .withSchedule(simpleSchedule()

// .withIntervalInSeconds(1)

// .withRepeatCount(3)) // note that 10 repeats will give a total of 11 firings

// .build();

scheduler.scheduleJob(job, trigger);

}

// try {

// Thread.sleep(100000);

// } catch (InterruptedException e) {

// e.printStackTrace();

// }

// scheduler.shutdown();

} catch (SchedulerException e) {

e.printStackTrace();

}

}

}

代码中,进行了已存在触发器的检查,如果数据库中已经存在,再创建就会引起异常

org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'group1.job1', because one already exists with this identification.

at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeJob(JobStoreSupport.java:1113)

at org.quartz.impl.jdbcjobstore.JobStoreSupport$2.executeVoid(JobStoreSupport.java:1067)

at org.quartz.impl.jdbcjobstore.JobStoreSupport$VoidTransactionCallback.execute(JobStoreSupport.java:3765)

at org.quartz.impl.jdbcjobstore.JobStoreSupport$VoidTransactionCallback.execute(JobStoreSupport.java:3763)

at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3849)

at org.quartz.impl.jdbcjobstore.JobStoreTX.executeInLock(JobStoreTX.java:93)

at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeJobAndTrigger(JobStoreSupport.java:1063)

at org.quartz.core.QuartzScheduler.scheduleJob(QuartzScheduler.java:855)

at org.quartz.impl.StdScheduler.scheduleJob(StdScheduler.java:249)

at com.example.quartz_demo.App.main(App.java:53)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值