quartz定时任务之demo实现

痴心易碎 提交于 2020-02-18 03:55:36

一.创建maven项目,引入jar包

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>

二.写一个xxxJob类实现Job接口,并实现里面的方法,

package com.cy;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RamJob implements Job{

	private static Logger _log = LoggerFactory.getLogger(RamJob.class);
	
	private static BankServiceImpl bsImpl = new BankServiceImpl();
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		_log.info("开始处理申请贷款业务");
		bsImpl.doApplyLoan();
		_log.info("申请贷款业务处理完成");
	}
	

}

添加一个业务处理类,模拟处理业务

package com.cy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BankServiceImpl {

	private static Logger _log = LoggerFactory.getLogger(BankServiceImpl.class);
	
	public void doLoan(){
		System.out.println("银行开始放款");
		_log.info("银行开始放款");
	}
	
	public void doTransfer(){
		System.out.println("借款人A转帐1000给借款人B");
		_log.info("借款人A转帐1000给借款人B");
	}
	
	public void doApplyLoan(){
		System.out.println("申请人申请贷款");
		_log.info("申请人申请贷款");
	}
}

 

三.处理任务

package com.cy;

import java.util.Date;

import org.quartz.CronScheduleBuilder;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RamQuartz {

	private static Logger _Logger = LoggerFactory.getLogger(RamQuartz.class);
	
	//设置任务启动时间
	static long applyLoanTime = System.currentTimeMillis() + 10*1000L;
	static long transferTime = System.currentTimeMillis() + 20*1000L;
	static long loanTime = System.currentTimeMillis() + 30*1000L;
	
	public static void main(String[] args) throws SchedulerException {
		//1.创建调度器工厂
		SchedulerFactory sFactory = new StdSchedulerFactory();
		//2.由调度器工厂创建调度器
		Scheduler scheduler = sFactory.getScheduler();
		//3.创建jobDetail对象
		JobDetail applyJD = JobBuilder.newJob(RamJob.class)
				.withDescription("this is applyLoan task")
				.withIdentity("ramJob", "ramJobGroup").build();
		JobDetail transferJD = JobBuilder.newJob(RamJob2.class)
				.withDescription("this is transfer task")
				.withIdentity("ramJob2", "ramJob2Group").build();
		JobDetail loanJD = JobBuilder.newJob(RamJob3.class)
				.withDescription("this is loan task")
				.withIdentity("ramJob3", "ramJob3Group").build();
		//设置启动时间
		Date applyStartTime = new Date(applyLoanTime);
		Date transferStartTime = new Date(transferTime);
		Date loanStartTime = new Date(loanTime);
		
		//4.创建触发器对象
		Trigger t1 = TriggerBuilder.newTrigger()
				.withDescription("")
				.withIdentity("ramJobTrigger", "ramJobTriggerGroup")
				.startAt(applyStartTime)
				.withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?")).build();
		Trigger t2 = TriggerBuilder.newTrigger()
				.withDescription("")
				.withIdentity("ramJob2Trigger", "ramJob2TriggerGroup")
				.startAt(transferStartTime)
				.withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?")).build();
		Trigger t3 = TriggerBuilder.newTrigger()
				.withDescription("")
				.withIdentity("ramJob3Trigger", "ramJob3TriggerGroup")
				.startAt(loanStartTime)
				.withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?")).build();
		
		//5.调度器注册
		scheduler.scheduleJob(applyJD, t1);
		scheduler.scheduleJob(transferJD, t2);
		scheduler.scheduleJob(loanJD, t3);
		
		//6.调度器执行
		scheduler.start();
		_Logger.info("开始执行调度器");
	}
}

总结:直接使用quartz,直接写一个xxxjob实现job类,然后分六步进行实现:

(1)创建调度器工厂 ScheduleFactory sf = new stdScheduleFactory()

(2)获取调度器实例 Scheduler s = sf.getScheduler()

(3)创建jobDetail对象 JobDetail jd = JobBuilder.newJob(RAMJob.class)
                .withDescription("this is a ram job")
                .withIdentity("ramJob", "ramGroup").build();;

(4)创建触发器对象 Trigger t = TriggerBuilder.newTrigger()
        .withDescription("")
        .withIdentity("ramTrigger", "ramTriggerGroup")
        .startAt(startTime)
        .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) //两秒执行一次
        .build();

(5)调度器注册jobDetail和触发器 s.scheduleJob(jd, t);

(6)调度器执行 s.start();

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!