更新时间:2022-08-29 11:01:46 来源:极悦 浏览984次
Quartz是一个定时器框架,你可以在java中使用它来做什么?
比如,每天00.00.00时服务器刷新缓存,清除过期数据,或者每逢节假日,固定给用户发送祝福信息提醒。
话不多说,先下载jar包:
// Timer quartz
compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.0'
那么核心代码如下:
package job;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Rock-Ayl 2019-4-30
* This is to register and implement quartz The timing management task of the framework class, There are two ways to set up scheduled tasks
* <p>
* 1:- Simple timer implementation : Use a simple schedule generator . Set the task . Every how many hours (1). There is no limit to the number of times ;
* <p>
* 2:corn Expression implementation corn Expressions are similar to regular expressions , You can call it Time regular expression eg: '0 0 0 * * ?' : ' Every day 0 Click to execute once '
* <p>
*/
public class Scheduler {
protected static Logger logger = LoggerFactory.getLogger(Scheduler.class);
// Static scheduler , from FileServer The default startup
public static org.quartz.Scheduler instance;
// Storage Work task & Mission conditions Object's list
protected static List<ScheduleTask> tasks = new ArrayList();
// Simple implementation : Every hour
private static final SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInHours(1).repeatForever();
//cron: Every day 0 Click to execute once
private static final CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 0 * * ?");
//Task class , Deposit job and trigger
static class ScheduleTask {
public JobDetail job;
public Trigger trigger;
public ScheduleTask(JobDetail job, Trigger trigger) {
this.job = job;
this.trigger = trigger;
}
}
// Register the print timer - Simple timer implementation
private static void registerPrintJob() {
String jobName = "PrintJob";
String jobGroup = "file";
JobDetail job = JobBuilder.newJob(PrintJob.class).withIdentity(jobName, jobGroup).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroup).startNow().withSchedule(simpleScheduleBuilder).build();
tasks.add(new ScheduleTask(job, trigger));
}
// start-up Scheduler
public static void startup() {
try {
instance = StdSchedulerFactory.getDefaultScheduler();
// Start registration to list
registerPrintJob();
for (ScheduleTask task : tasks) {
instance.scheduleJob(task.job, task.trigger);
}
instance.start();
logger.info(">>>file scheduler start.");
} catch (SchedulerException se) {
logger.error(" Work scheduler startup error :" + se.getMessage(), se);
}
}
// close Scheduler
public static void stop() {
try {
instance.shutdown();
} catch (Exception e) {
logger.error(" Work scheduler shutdown error [{}]", e);
}
}
}
业务层:
package job;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* Created by Rock-Ayl 2019-5-10
* Printing tasks demo
*/
@DisallowConcurrentExecution
public class PrintJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println(" Perform timer tasks ing...");
}
}
写完这两个代码后,你就可以简单地运行它们了。
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习