java 定时器 quartz_Java定时器和Quartz使用

一、Java普通自定义定时器

1 /**

2 * 自定义一个定时器3 *@authorlw4 */

5 public class MyTimer extendsThread{6

7 privateLong time ;8

9 publicMyTimer(Long time) {10 this.time =time;11 }12

13 @Override14 public voidrun() {15 while(true){16 try{17 Thread.sleep(time);18 } catch(InterruptedException e) {19 e.printStackTrace();20 }21 send();22 }23 }24 /**

25 * 开始执行任务26 */

27 public voidexecute(){28 this.start();29 }30

31 /**

32 * 定时任务33 */

34 private voidsend() {35 System.out.println("任务执行了:" + newDate());36 }37

38 public static voidmain(String[] args) {39 MyTimer myTimer = new MyTimer(2000L) ;40 myTimer.execute();41 }42

43 }

输出内容:

1 任务执行了:Tue Nov 13 10:10:02 CST 2018

2 任务执行了:Tue Nov 13 10:10:04 CST 2018

3 任务执行了:Tue Nov 13 10:10:06 CST 2018

4 任务执行了:Tue Nov 13 10:10:08 CST 2018

5 任务执行了:Tue Nov 13 10:10:10 CST 2018

6 任务执行了:Tue Nov 13 10:10:12 CST 2018

7 任务执行了:Tue Nov 13 10:10:14 CST 2018

8 任务执行了:Tue Nov 13 10:10:16 CST 2018

9 任务执行了:Tue Nov 13 10:10:18 CST 2018

10 任务执行了:Tue Nov 13 10:10:20 CST 2018

二、使用Java   Timer类

1 public classTimerTest {2

3 public static voidmain(String[] args) {4 Timer timer = newTimer();5 //5s后执行6 //timer.schedule(new MyTask(), 5000);7 //马上执行任务,每隔1000执行一次

8 timer.scheduleAtFixedRate(new MyTask(), new Date(), 1000);9 }10

11 }12

13 //定时任务,这是一个线程

14 class MyTask extendsTimerTask {15 @Override16 public voidrun() {17 System.out.println("task execute ");18 }19 }

Timer也是基于线程来实现的。

三、Quartz框架

Quartz是一个完全由Java编写的开源作业调度框架,为在Java应用程序中进行作业调度提供了简单却强大的机制。Quartz允许开发人员根据时间间隔来调度作业。它实现了作业和触发器的多对多的关系,还能把多个作业与不同的触发器关联。

使用如下:

创建job

1 public class HelloJob implementsJob{2

3 public void execute(JobExecutionContext context) throwsJobExecutionException {4 JobDetail detail =context.getJobDetail();5 String name = detail.getJobDataMap().getString("name");6 System.out.println("say hello " +name );7 }8

9 }

测试

1 public classQuartzTest {2

3 public static void main(String[] args) throwsInterruptedException {4

5 //创建工厂

6 SchedulerFactory schedulerfactory = newStdSchedulerFactory();7 Scheduler scheduler = null;8 try{9 //通过schedulerFactory获取一个调度器

10 scheduler =schedulerfactory.getScheduler();11

12 //指明job的名称,所在组的名称,以及绑定job类

13 JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("JobName", "JobGroupName")14 .usingJobData("name", "quartz").build();15 //定义触发的条件

16 Trigger trigger = TriggerBuilder.newTrigger().withIdentity("CronTrigger1", "CronTriggerGroup")17 .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever())18 .startNow().build();19

20 //把作业和触发器注册到任务调度中

21 scheduler.scheduleJob(job, trigger);22

23 //启动调度

24 scheduler.start();25

26 Thread.sleep(10000);27

28 //停止调度

29 scheduler.shutdown();30

31 } catch(SchedulerException e) {32 e.printStackTrace();33 }34

35 }36 }

步骤:

创建job

创建工厂,获取调度器

使用JobBuilder通过job生成jobDetail,可以传递数据

通过TriggerBuilder创建Trigger,可以设置时间

通过调度器注册ob和trigger

启动调度

四、Quarts 和 Spring整合

创建job

1 public classMailJob {2

3 private voidsend() {4 System.out.println("发送邮件了:" + newDate());5 }6 }

配置

1 Quartz配置文件

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

输出结果:

1 发送邮件了:Sat Apr 15 16:12:00 CST 2017

2 发送邮件了:Sat Apr 15 16:12:10 CST 2017

四、spring 自带的定时任务:spring-task

spring 自身的spring-task 不依赖任何的第三方框架,实现方式也很简单:

这里需要做的就是在xml文件中的引入task命名空间,以便后续标签中的使用:

xmlns:task="http://www.springframework.org/schema/task"http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.2.xsd

1:定义任务类:

1 public classTaskJob {2

3 public voidspringJob(){4 System.out.println("spring 自身的定时任务");5 }6 }

2:在xml配置文件中配置定时job:

1

2

3

4

5

6

7

8

经过这样的配置后在在开启这个定时任务的spring容器后就会开始相应的定时job。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值