随笔 - 5  文章 - 0  评论 - 0  阅读 - 1754

Quartz(一)Quartz入门

Quartz官网:http://www.quartz-scheduler.org/

创建一个SpringBoot Maven工程

启动示例程序QuartzTest

复制代码
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzTest {

    public static void main(String[] args) {

        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            scheduler.start();

            scheduler.shutdown();

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
    }
}
复制代码

注:一旦使用 StdSchedulerFactory.getDefaultScheduler() 获得调度程序,应用程序将不会终止,直到调用 scheduler.shutdown(),因为会有活动线程。

结果如下图

 

 

 start()shutdown()调用之间编写代码

复制代码
  JobDetail job = newJob(HelloJob.class)
      .withIdentity("job1", "group1")
      .build();

  Trigger trigger = newTrigger()
      .withIdentity("trigger1", "group1")
      .startNow()
            .withSchedule(simpleSchedule()
              .withIntervalInSeconds(5)
              .repeatForever())            
      .build();

  scheduler.scheduleJob(job, trigger);
复制代码

此时注意代码中的静态导入

  import static org.quartz.JobBuilder.*;
  import static org.quartz.TriggerBuilder.*;
  import static org.quartz.SimpleScheduleBuilder.*;

文档中并未给出HelloJob,自己补写

HelloJob

复制代码
import com.example.quartz.tools.DFUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Date;

public class HelloJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("HelloJob.execute"+ DFUtil.format(new Date()) + Thread.currentThread().getName());
    }
}
复制代码

编写了一个工具类DFUtil

复制代码
import java.text.SimpleDateFormat;
import java.util.Date;

public class DFUtil {
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static String format(Date date){
        return sdf.format(date);
    }
}
复制代码

完整的QuartzTest

运行结果

复制代码

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;

public class QuartzTest {

public static void main(String[] args) {

try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

scheduler.start();

// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();

// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever())
.build();

// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);

TimeUnit.SECONDS.sleep(20);
scheduler.shutdown();

} catch (SchedulerException se) {
se.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
复制代码
posted on   默默努力就好  阅读(521)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示