定时任务框架:Quartz - 快速入门

功能比较简单,贴点烂代码,长话短说。

贴些烂代码:

任务实现Job接口即可,如:

public class HelloQuartzJob implements Job{

public final static String NAME = "hello_quartz";

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("hello quartz job !!!");

JobKey key = context.getJobDetail().getKey();
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String jobSays = dataMap.getString("jobSays");
float myFloatValue = dataMap.getFloat("myFloatValue");

System.err.println("Instance " + key + " of DumbJob says: " + jobSays + ", and val is: " + myFloatValue);
}
}

这样子调用:

Scheduler scheduler = new StdSchedulerFactory("conf/quartz.properties").getScheduler(); // 可添加配置文件
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger", Scheduler.DEFAULT_GROUP)
.withSchedule(
CronScheduleBuilder.cronSchedule(cron)
.withMisfireHandlingInstructionIgnoreMisfires()
)
// .forJob(jobDetail)
.build();
JobDetail jobDetail = JobBuilder.newJob(HelloQuartzJob.class)
.withIdentity(HelloQuartzJob.NAME, Scheduler.DEFAULT_GROUP)
.usingJobData("jobSays", "Hello World!")
.usingJobData("myFloatValue", 3.141f)
.build();
// 监听指定job,默认监听所有
scheduler.getListenerManager().addJobListener(new HelloJobListener(), KeyMatcher.keyEquals(jobDetail.getKey()));
// 监听某个组
scheduler.getListenerManager().addTriggerListener(new HelloTriggerListener(), GroupMatcher.triggerGroupEquals(Scheduler.DEFAULT_GROUP));
scheduler.scheduleJob(jobDetail, trigger);
this.scheduler.start();

就是初始化一下,加上点触发时的监听,运行job时的监听。非常简单。

  • 触发监听实现TriggerListener接口
  • job监听,实现JobListener接口

最后,附上配置:

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.

org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false

#==============================================================
#Configure JobStore
#==============================================================
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#org.quartz.jobStore.tablePrefix = QRTZ_
#org.quartz.jobStore.isClustered = true
#org.quartz.jobStore.clusterCheckinInterval = 20000
#org.quartz.jobStore.dataSource = myDS
##==============================================================
##Configure DataSource
##==============================================================
#org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
#org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.1.81:3306/db_eam_mirror?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
#org.quartz.dataSource.myDS.user = root
#org.quartz.dataSource.myDS.password = 123456
#org.quartz.dataSource.myDS.maxConnections = 30

#==============================================================
#Configure ThreadPool
#==============================================================
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.misfireThreshold = 60000

# skip update check!
org.quartz.scheduler.skipUpdateCheck = true

转载于:https://my.oschina.net/u/1404949/blog/815731

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值