功能比较简单,贴点烂代码,长话短说。
贴些烂代码:
任务实现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