欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Quartz定时任务框架(一) 入门案例

程序员文章站 2022-06-09 14:50:16
...

介绍

Quartz是一套支持时间调度的框架,官网http://www.quartz-scheduler.org/, jar 包下载,点这里

入门案例

1. quartz.properties文件

这个文件不是必须创建的,如果需要创建使用,必须创建在calsspath下面,对于web工程,编译后放到WEB-INF/classes下面

#定义scheduler的名称
org.quartz.scheduler.instanceName = MyScheduler
#定义quartz的线程池中最大的线程数量,意味着最多能有几个job同时执行
org.quartz.threadPool.threadCount = 3
#所有scheduler的数据都是放在内存中的,这个类是官网建议的类用来存储数据,给配置上
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

 2. 简单案例

public static void main(String[] args) throws InterruptedException {
		try {
			//从调度程序工厂获取一个调度程序的实例
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            //输出从quartz.properties文件中设置的调度程序的名称
            System.out.println("自定义的调度程序的名称:"+scheduler.getSchedulerName());
            //........ 加入具体的定时任务
            
            scheduler.start();//启动scheduler,将启动具体的job
            scheduler.shutdown();//关闭scheduler,将job线程停止

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
	}

2. 定义具体的任务

*  定义一个类实现job接口,在execute方法内,执行任务的具体内容

这个类必须是public,否则将会抛异常:

Class org.quartz.simpl.SimpleJobFactory can not access a member of class com.jiaoyiping.stuty_quartz.SimpleJob2 with modifiers ""

package cn.bing.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job{
	@Override
	public void execute(JobExecutionContext paramJobExecutionContext)
			throws JobExecutionException {
		 System.err.println("["+Thread.currentThread().getName()+"]"+"Hello World!  MyJob is executing.");
	}
}

* 将job配置进去schelar并且设置任务的执行时间间隔

 // 定义一个job并且绑定到HelloJob上,这里并不会马上创建hellojob实例,在scheduler调度执行任务时候创建
            JobDetail job = JobBuilder.newJob (HelloJob.class).withIdentity("job1", "group1")
                .build();
            //配置任务是马上执行的,并且是1秒执行一次
            Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(1)
                        .repeatForever())            
                .build();

            //将任务的配置应用到job上
            scheduler.scheduleJob(job, trigger);

完整的代码

package cn.bing.quartz;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class Myquartz {
	public static void main(String[] args) throws InterruptedException {
		try {
			//从调度程序工厂获取一个调度程序的实例
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            //输出从quartz.properties文件中设置的调度程序的名称
            System.out.println("自定义的调度程序的名称:"+scheduler.getSchedulerName());

            // 定义一个job并且绑定到HelloJob上,这里并不会马上创建hellojob实例,在scheduler调度执行任务时候创建
            JobDetail job = JobBuilder.newJob (HelloJob.class).withIdentity("job1", "group1")
                .build();
            //配置任务是马上执行的,并且是1秒执行一次
            Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(1)
                        .repeatForever())            
                .build();

            //将任务的配置应用到job上
            scheduler.scheduleJob(job, trigger);
            
            scheduler.start();//启动任务
            //为了调度的任务线程执行,需要休眠一段时间,才调用shutdown方法关闭调度线程
            //或者不调用shutdown方法
            Thread.sleep(10000);
            scheduler.shutdown();//停止任务

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
	}
}

打印日志:

[MyScheduler_Worker-1]Hello World!  MyJob is executing.
[MyScheduler_Worker-2]Hello World!  MyJob is executing.
[MyScheduler_Worker-3]Hello World!  MyJob is executing.
[MyScheduler_Worker-1]Hello World!  MyJob is executing.
[MyScheduler_Worker-2]Hello World!  MyJob is executing.
[MyScheduler_Worker-3]Hello World!  MyJob is executing.
[MyScheduler_Worker-1]Hello World!  MyJob is executing.
[MyScheduler_Worker-2]Hello World!  MyJob is executing.
[MyScheduler_Worker-3]Hello World!  MyJob is executing.
[MyScheduler_Worker-1]Hello World!  MyJob is executing.
[MyScheduler_Worker-2]Hello World!  MyJob is executing.

 

相关标签: quartz入门案例