Quartz任务调度简介--浅谈SimpleTrigger

SimpleTrigger的作用:
在一个指定的时间内执行一次作业任务。
或是在指定的时间间隔内多次执行作业任务。
举例:
创建一个HelloJob3并实现Job接口:
HelloJob3代码如下:

package com.unitop.Demo01;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Date;

public class HelloJob3 implements Job{
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date date =new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH;mm:ss");
        System.out.println("当前执行时间:"+simpleDateFormat.format(date));
        System.out.println("Hello Word");
    }
}

使用SimpleTrigger创建一个定时任务;
任务的内容是当前时间4秒钟之后执行一次任务并且只执行一次。

package com.unitop.Demo01;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

public class HelloScheduler03 {
    public static void main(String[] args) throws Exception {
        test01();
    }

    public static void test01() throws Exception{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        Date  date = new Date();
        System.out.println("当前时间是:"+simpleDateFormat.format(date));
        JobDetail jobDetail=JobBuilder.newJob(HelloJob3.class).withIdentity("myJob","group").build();
        //获取当前时间4秒之后的时间
        date.setTime(date.getTime()+4000);
        //设置当前时间啊4秒钟执行一次任务并且是指执行一次
        SimpleTrigger simpleTrigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity("myTrigger","trigger").startAt(date).build();
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail,simpleTrigger);
    }
}

需要注意的是:
重复的次数可以是0。正整数或者是SimpleTrigger。REPEAT_INDEFINITELY常量值。
重复执行间隔必须为0或者长整数。
一旦指定了Endtime参数,那么他会覆盖重复次数的参数的效果。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值