标签:tail cto 开发 线程同步 mave rip 任务 AC version
简介
开源的完全使用JAVA开发的一种时间调度框架,可多线程同步执行。
使用
quartz有两种使用方式:RAM、JDBC。任务的调度也有两种方式:SimpleSchedule、CronSchedule
简单调用
<!-- quartz begin --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency> <!-- quartz end --> <!-- log --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> </dependency>
package com.quartz.first; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class MyJob implements Job { private static final String NAME = "name"; @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println(" execute job() ... "); } }
package com.quartz.first; import java.util.Date; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; /** * * 使用 RAM 方式定时 * * @author 丰志 * */ public class RAMQuartz { public static void main(String[] args) throws SchedulerException { //1.创建Scheduler的工厂 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); //2.从工厂中获取调度器实例 Scheduler scheduler = schedulerFactory.getScheduler(); //3.创建JobDetail JobDetail jobDetail = JobBuilder.newJob(MyJob.class) .withDescription("this is a ram job") //job的描述 .withIdentity("ramJob", "ramGroup") //job 的name和group .build(); // jobDetail.getJobDataMap().put("name", "zhangsan"); //传参用与MyJob中获取参数 jobDataMap.get("name") //任务运行的时间,SimpleSchedle类型触发器有效 long time= System.currentTimeMillis() + 3*1000L; //3秒后启动任务 Date statTime = new Date(time); //4.创建Trigger //使用SimpleScheduleBuilder或者CronScheduleBuilder Trigger trigger = TriggerBuilder.newTrigger() .withDescription("") .withIdentity("ramTrigger", "ramTriggerGroup") .startAt(statTime) //默认当前时间启动 // .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).withRepeatCount(10)) // 每个2秒钟执行一次,一共执行10次 // .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) //两秒执行一次,cron 秒、分、时、日、月、年、 .build(); //5.注册任务和定时器 scheduler.scheduleJob(jobDetail, trigger); //6.启动 调度器 scheduler.start(); } }
标签:tail cto 开发 线程同步 mave rip 任务 AC version
原文地址:https://www.cnblogs.com/guofz/p/8963367.html