(一)Quartz Hello world 项目
程序员文章站
2022-05-08 13:36:17
...
任务详情
让任务每隔两秒打印一次hellworld
0、引入 quartz依赖
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
1、编写Job:PrintWordsJob
public class PrintWordsJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
String printTime = new SimpleDateFormat("yy-MM-dd HH-mm-ss").format(new Date());
System.out.println("PrintWordsJob start at:" + printTime + ", prints: Hello Job-" + new Random().nextInt(100));
}
2、建立调度器Scheduler:MyScheduler
public class MyScheduler {
public static void main(String[] args) throws SchedulerException, InterruptedException {
// 1、创建调度器Scheduler
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
// 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容)
JobDetail jobDetail = JobBuilder.newJob(PrintWordsJob.class)
.withIdentity("job1", "group1").build();
// 3、构建Trigger实例,每隔1s执行一次
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1")
.startNow()//立即生效
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(1)//每隔1s执行一次
.repeatForever()).build();//一直执行
//4、执行
scheduler.scheduleJob(jobDetail, trigger);
System.out.println("--------scheduler start ! ------------");
scheduler.start();
//睡眠3秒
TimeUnit.SECONDS.sleep(3);
scheduler.shutdown();
System.out.println("--------scheduler shutdown ! ------------");
}
}
上一篇: 1043(18)
下一篇: 控制台程序不显示dos窗口的方法
推荐阅读
-
Hello World,使用OpenGL ES绘制一个三角形
-
Zend Framework入门之环境配置及第一个Hello World示例(附demo源码下载)
-
IntelliJ IDEA 创建spring boot 的Hello World 项目(图解)
-
typecho插件编写教程(一):Hello World
-
VS2010怎么新建包含两个form的HELLO WORLD项目?
-
IntelliJ IDEA 创建spring boot 的Hello World 项目(图解)
-
typecho插件编写教程(一):Hello World
-
VS2010怎么新建包含两个form的HELLO WORLD项目?
-
ASP.NET Core使用GraphQL第一章之Hello World
-
一个基于Net Core3.0的WPF框架Hello World实例