Quartz无状态的JOB

package com.lz.quartz.job;

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

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class NoStateHelloJob implements Job {

    /**
     *无状态的JOB 每次执行JOB 都创建一个新的JobDataMap
     * 可以从打印结果中看出:cout 每次都是1
     */
    private Integer count;

    public void setCount(Integer count) {
        this.count = count;
    }

    public NoStateHelloJob(){
        System.out.println("quartz 每次任务执行 都会创建一个新的StateHelloJob 对象实例");
    }


    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("************************************进入任务************************************************");

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd hh:mm:ss");
        Instant instant1 = jobExecutionContext.getFireTime().toInstant();
        ZoneId zoneId1 = ZoneId.systemDefault();
        LocalDateTime taskTimeNow = instant1.atZone(zoneId1).toLocalDateTime();
        System.out.println("获取当前任务执行的时间:"+formatter.format(taskTimeNow));

        Instant instant2 = jobExecutionContext.getNextFireTime().toInstant();
        ZoneId zoneId2 = ZoneId.systemDefault();
        LocalDateTime taskTimeNext = instant2.atZone(zoneId2).toLocalDateTime();
        System.out.println("获取任务下次执行的时间:"+formatter.format(taskTimeNext));

        ++count;
        System.out.println("打印count结果:"+count);

        //覆盖JobDetail中的JobDataMap中的count值
        jobExecutionContext.getJobDetail().getJobDataMap().put("count",count);

        System.out.println("------------------------------------------------------------");

    }

}
package com.lz.quartz.main;

import com.lz.quartz.job.NoStateHelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class NoStateHelloJobMain {

    public static void main(String[] args) {
        try {
            //1:调度器Schedule
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();


            //2:任务实例 JobDeatil hellojob任务名称 group1任务分组名称
            JobDetail jobDetail = JobBuilder.newJob(NoStateHelloJob.class).
                    withIdentity("stateHelloJob","group2").
                    usingJobData("message","嘿嘿嘿").
                    usingJobData("count",0).
                    build();

            //3:触发器Trigger' trigger1触发器名称  group1触发器分组名 每5S执行一次打印 startNow马上启动触发器
            Trigger trigger = TriggerBuilder.newTrigger().
                    withIdentity("trigger2","group2").startNow().
                    withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).
                    usingJobData("message","哈哈哈").
                    build();

            //让调度器 使触发器和任务关联
            scheduler.scheduleJob(jobDetail,trigger);

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

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

}

打印结果:

log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
quartz 每次任务执行 都会创建一个新的StateHelloJob 对象实例
************************************进入任务************************************************
获取当前任务执行的时间:2018-52-18 09:52:46
获取任务下次执行的时间:2018-52-18 09:52:51
打印count结果:1
------------------------------------------------------------
quartz 每次任务执行 都会创建一个新的StateHelloJob 对象实例
************************************进入任务************************************************
获取当前任务执行的时间:2018-52-18 09:52:51
获取任务下次执行的时间:2018-52-18 09:52:56
打印count结果:1
------------------------------------------------------------

Process finished with exit code -1

从打印结果可以看出 每次任务执行 都创建一个新的JobDataMap 因为每次打印出JobDataMap中的count的值都是1 一样的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值