栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

quartz入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

quartz入门

核心类说明
  • Scheduler 调度器。所有的调度都是由它控制,是Quartz的大脑,所有的任务都是由它来管理
  • Job:任务,定时执行的类(定义业务逻辑)
  • JobDetail:基于Job,进一步包装。其中关联一个Job,并为Job指定更详细的书香,比如标识等
  • Trigger:触发器。可以指定给某个任务。指定任务的触发机制
pom.xml

	4.0.0
	com.cp
	quarz-demo
	0.0.1-SNAPSHOT

	
		
		
			org.quartz-scheduler
			quartz
			2.2.3
		

	

创建Job
package com.quarz.demo;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;

//Job 任务
public class HelloJob implements Job{

	public void execute(JobExecutionContext context) throws JobExecutionException {
		//可以获取到job的标识
		JobDetail jobDetail = context.getJobDetail();
		JobKey key = jobDetail.getKey();
		String name = key.getName();
		String group = key.getGroup();
		System.out.println(name);
		System.out.println(group);
		System.out.println("hello join execute"+ new Date());
	}

}


测试
package com.quarz.demo;

import java.util.GregorianCalendar;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;


public class HelloQuartz {

	public static void main(String[] args) throws SchedulerException {
		//1.构建调度器Scheduler
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		
		//2.触发器
		
		SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
		.startNow()
		.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
		.endAt(new GregorianCalendar(2021, 10, 13, 9, 23, 59).getTime()).build();
		
		//3.JobDetail
		JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1").build();
		
		//4.将JobDetail 和触发器 添加到调度器中
		scheduler.scheduleJob(jobDetail, trigger);
		
		//5.启动,调度器开始工作
		scheduler.start();
		
	}
}


quartz.properties配置文件(可以不配置)

quartz有配置文件,这里没有配置。
当我们没有配置quartz.properties文件时,quartz会有他自己的默认值。
当你有需要修改,就需要配置quartz.properties文件

## 指定调度器名称,非实现类
org.quartz.scheduler.instanceName = MyScheduler
## 指定线程池实现类
org.quartz.threadPool.threadCount = 3
## 线程池线程数量
org.quartz.threadPoll.threadPriority = 5
## 非持久化job
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
控制台输出
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
job1
group1
hello join executeSat Nov 13 10:01:02 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:04 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:06 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:08 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:10 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:12 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:14 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:16 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:18 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:20 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:22 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:24 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:26 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:28 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:30 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:32 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:34 GMT+08:00 2021
job1
group1
hello join executeSat Nov 13 10:01:36 GMT+08:00 2021


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489151.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号