Quartz simple three-step introduction

Quartz Simple Introductory Teaching

Quartz is our task scheduling framework. In JAVA, we can use Timer for simple task scheduling, but for complex scheduling, we choose to use Quartz.

Quartz is an open source job scheduling framework written entirely in java. Although the Quartz framework integrates many additional features, in its simple form, you will find it uncomfortably easy to use! Simply create a java class that implements the org.quartz.Job interface. The Job interface contains the only method:
public void execute (JobExecutionContext context)
throws JobExecutionException;
in your Job interface implementation class, add some logic to the execute () method. Once you configure the Job implementation class and set the scheduling schedule, Quartz will pay close attention to the remaining time. When the scheduler determines that it is a job to notify you, the Quartz framework will call the execute () method on your Job implementation class (job class) and allow it to do what it should do. No need to report anything to the scheduler or call anything specific. Just perform the task and end the task. If you configure your job to be called again later, the Quartz framework will call it again at the appropriate time.

first step

Quartz official website to download, download to hang vpn can buy or use Taobao mirror, I will not explain here.
Insert picture description here
Then download the latest one, and unzip it after the download is complete.

Second step

Create a new folder in our Java Project, the name is lib or it can be something else, such as the following,
Insert picture description here
then unzip the compressed package we downloaded to
Insert picture description here
find the Quartz lib folder, and all the files inside are ctrl + c and then ctrl + v to In our lib,
Insert picture description here
then right-click lib, BuildPath, and it's built.
Insert picture description here

third step

In our example found Quartz folder, by following this path
Insert picture description here
just open a example
Insert picture description here
copy and paste into a bag and
Insert picture description here
remember to change the name of the package name of this package we are now
and then you can run, then ran'll find tips
Insert picture description here
really nothing Relationship, this is a log file, but we still need to import the
log4j file location in this place,
Insert picture description here
copy and paste it into the src of our Project.
Insert picture description here
Now run it again, and it succeeded. Next, we can think about several other examples Once, you can also import them and run for a while, and paste the modified code below.

public class HelloJob implements Job {


    public HelloJob() {
    }


    public void execute(JobExecutionContext context)
        throws JobExecutionException {
   System.out.println("------start------");
   System.out.println("Hello World!--"+new Date());
   System.out.println("-------end-------");
    }

}
import static org.quartz.DateBuilder.evenSecondDateAfterNow;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
public class Quartzz {

	public void run() throws Exception {

		// 1、创建scheduler工厂
		SchedulerFactory sf = new StdSchedulerFactory();
		// 2、从工厂中获取调度器
		Scheduler sched = sf.getScheduler();

		// 3、创建JobDetai
		JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
		// 时间
		Date runTime = evenSecondDateAfterNow();

		// 4、触发器条件
		Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime)
        .withSchedule(simpleSchedule().withIntervalInSeconds(2)
        .withRepeatCount(3)).build();

		// 5、注册任务和触发条件
		sched.scheduleJob(job, trigger);

		// 6、启动
		sched.start();
		try {
			// 5秒后停止
			Thread.sleep(500L * 1000L);
			// executing...
		} catch (Exception e) {
			//
		}

	}

	public static void main(String[] args) throws Exception {

		Quartzz example = new Quartzz();
		example.run();

	}

}

A lot more clean and concise.
over ~

Published 13 original articles · Like1 · Visits 296

Guess you like

Origin blog.csdn.net/Alagagaga/article/details/105485019