Using the quartz achieve timing task scheduling

Task scheduling methods are Thread, Timertask, thread pool, quartz, SpringBoot built schedul, pay attention not to use a distributed, should be distributed using the XXL-Job, pumping below a few common recap.

Note: These codes do not usually write when developing, directly on the line. Essentially the same, nothing too difficult to understand

A, TimerTask achieve timing schedule

The general client development with more, usually not used

/**
 * 使用TimerTask类实现定时任务
*/
public class Test01{
	static long count = 0;

	public static void main(String[] args) {
		TimerTask timerTask = new TimerTask() {

			@Override
			public void run() {
				count++;
				System.out.println(count);
			}
		};
		Timer timer = new Timer();
		// 天数
		long delay = 0;
		// 秒数
		long period = 1000;
		timer.scheduleAtFixedRate(timerTask, delay, period);
	}

}

Second, the thread pool mode Executors-ScheduledExecutorService

java.util.concurrent, the tools to be as complicated by the introduction of

public class Test02{
	public static void main(String[] args) {
		Runnable runnable = new Runnable() {
			public void run() {
				// task to run goes here
				System.out.println("Hello !!");
			}
		};
           //使用线程池方式,用的也比较少
		ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
		// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
		service.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS);
	}
}

Three, Quartz

This is quite common to introduce specific point

Quartz expression can visit: http://cron.qqe2.com/

3.1, dependent on the introduction maven

	<dependencies>
		<!-- quartz -->
		<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>
	</dependencies>

3.2, create a task scheduling classes

public class MyJob implements Job {
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("quartz MyJob date:" + new Date().getTime());
	}
}

3.3, the task startup class

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


      //3.创建JobDetail
      JobDetail jb = JobBuilder.newJob(MyJob.class)
              .withDescription("this is a ram job") //job的描述
              .withIdentity("ramJob", "ramGroup") //job 的name和group
              .build();

      //任务运行的时间,SimpleSchedle类型触发器有效
      long time=  System.currentTimeMillis() + 3*1000L; //3秒后启动任务
      Date statTime = new Date(time);

      //4.创建Trigger
          //使用SimpleScheduleBuilder或者CronScheduleBuilder
      Trigger t = TriggerBuilder.newTrigger()
                  .withDescription("")
                  .withIdentity("ramTrigger", "ramTriggerGroup")
                  //.withSchedule(SimpleScheduleBuilder.simpleSchedule())
                  .startAt(statTime)  //默认当前时间启动
                  .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) //两秒执行一次
                  .build();

      //5.注册任务和定时器
      scheduler.scheduleJob(jb, t);

      //6.启动 调度器
      scheduler.start();

 

 

 

Published 52 original articles · won praise 116 · views 50000 +

Guess you like

Origin blog.csdn.net/RuiKe1400360107/article/details/103756573