文档章节

spring定时器----JobExecutionContext

绿林小子
 绿林小子
发布于 2015/11/25 15:30
字数 804
阅读 222
收藏 0
    1. 层次结构
       
    2. 类
    
public class JobExecutionContext implements java.io.Serializable {
 
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Data members.
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */
 
    private transient Scheduler scheduler; 
    private Trigger trigger; 
    private JobDetail jobDetail;     
    private JobDataMap jobDataMap; 
    private transient Job job;     
    private Calendar calendar; 
    private boolean recovering = false; 
    private int numRefires = 0; 
    private Date fireTime; 
    private Date scheduledFireTime; 
    private Date prevFireTime; 
    private Date nextFireTime;     
    private long jobRunTime = -1;     
    private Object result;     
    private HashMap data = new HashMap();
 
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Constructors.
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */
 
    /**
     * Create a JobExcecutionContext with the given context data.
     */
    public JobExecutionContext(Scheduler scheduler,
            TriggerFiredBundle firedBundle, Job job) {
        this.scheduler = scheduler;
        this.trigger = firedBundle.getTrigger();
        this.calendar = firedBundle.getCalendar();
        this.jobDetail = firedBundle.getJobDetail();
        this.job = job;
        this.recovering = firedBundle.isRecovering();
        this.fireTime = firedBundle.getFireTime();
        this.scheduledFireTime = firedBundle.getScheduledFireTime();
        this.prevFireTime = firedBundle.getPrevFireTime();
        this.nextFireTime = firedBundle.getNextFireTime();
         
        this.jobDataMap = new JobDataMap();
        this.jobDataMap.putAll(jobDetail.getJobDataMap());
        this.jobDataMap.putAll(trigger.getJobDataMap());
         
        this.jobDataMap.setMutable(false);
        this.trigger.getJobDataMap().setMutable(false);
    }
 
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Interface.
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */
 
    /**
     * Get a handle to the <code>Scheduler</code> instance that fired the
     * <code>Job</code>.
     */
    public Scheduler getScheduler() {
        return scheduler;
    }
 
    /**
     * Get a handle to the <code>Trigger</code> instance that fired the
     * <code>Job</code>.
     */
    public Trigger getTrigger() {
        return trigger;
    }
 
    /**
     * Get a handle to the <code>Calendar</code> referenced by the <code>Trigger</code>
     * instance that fired the <code>Job</code>.
     */
    public Calendar getCalendar() {
        return calendar;
    }
 
    /**
     * If the <code>Job</code> is being re-executed because of a 'recovery'
     * situation, this method will return <code>true</code>.
     */
    public boolean isRecovering() {
        return recovering;
    }
 
    public void incrementRefireCount() {
        numRefires++;
    }
 
    public int getRefireCount() {
        return numRefires;
    }
 
    /**
     * Get the convenience <code>JobDataMap</code> of this execution context.
     * 
     * The <code>JobDataMap</code> found on this object serves as a convenience -
     * it is a merge of the <code>JobDataMap</code> found on the 
     * <code>JobDetail</code> and the one found on the <code>Trigger</code>, with 
     * the value in the latter overriding any same-named values in the former.
     * <i>It is thus considered a 'best practice' that the execute code of a Job
     * retrieve data from the JobDataMap found on this object</i>
     * 
     * NOTE: Do not
     * expect value 'set' into this JobDataMap to somehow be set back onto a
     * <code>StatefulJob</code>'s own JobDataMap.
     * 
     * Attempts to change the contents of this map typically result in an 
     * <code>IllegalStateException</code>.
     * 
     */
    public JobDataMap getMergedJobDataMap() {
        return jobDataMap;
    }
 
    /**
     * Get the <code>JobDetail</code> associated with the <code>Job</code>.
     */
    public JobDetail getJobDetail() {
        return jobDetail;
    }
 
    /**
     * Get the instance of the <code>Job</code> that was created for this
     * execution.
     * 
     * Note: The Job instance is not available through remote scheduler
     * interfaces.
     */
    public Job getJobInstance() {
        return job;
    }
 
    /**
     * The actual time the trigger fired. For instance the scheduled time may
     * have been 10:00:00 but the actual fire time may have been 10:00:03 if
     * the scheduler was too busy.
     * 
     * @return Returns the fireTime.
     * @see #getScheduledFireTime()
     */
    public Date getFireTime() {
        return fireTime;
    }
 
    /**
     * The scheduled time the trigger fired for. For instance the scheduled
     * time may have been 10:00:00 but the actual fire time may have been
     * 10:00:03 if the scheduler was too busy.
     * 
     * @return Returns the scheduledFireTime.
     * @see #getFireTime()
     */
    public Date getScheduledFireTime() {
        return scheduledFireTime;
    }
 
    public Date getPreviousFireTime() {
        return prevFireTime;
    }
 
    public Date getNextFireTime() {
        return nextFireTime;
    }
 
    public String toString() {
        return "JobExecutionContext:" + " trigger: '"
                + getTrigger().getFullName() + " job: "
                + getJobDetail().getFullName() + " fireTime: '" + getFireTime()
                + " scheduledFireTime: " + getScheduledFireTime()
                + " previousFireTime: '" + getPreviousFireTime()
                + " nextFireTime: " + getNextFireTime() + " isRecovering: "
                + isRecovering() + " refireCount: " + getRefireCount();
    }
 
    /**
     * Returns the result (if any) that the <code>Job</code> set before its 
     * execution completed (the type of object set as the result is entirely up 
     * to the particular job).
     * 
     * The result itself is meaningless to Quartz, but may be informative
     * to <code>{@link JobListener}s</code> or 
     * <code>{@link TriggerListener}s</code> that are watching the job's 
     * execution.
     * 
     * @return Returns the result.
     */
    public Object getResult() {
        return result;
    }
     
    /**
     * Set the result (if any) of the <code>Job</code>'s execution (the type of 
     * object set as the result is entirely up to the particular job).
     * 
     * The result itself is meaningless to Quartz, but may be informative
     * to <code>{@link JobListener}s</code> or 
     * <code>{@link TriggerListener}s</code> that are watching the job's 
     * execution.
     * 
     * @return Returns the result.
     */
    public void setResult(Object result) {
        this.result = result;
    }
     
    /**
     * The amount of time the job ran for (in milliseconds).  The returned 
     * value will be -1 until the job has actually completed (or thrown an 
     * exception), and is therefore generally only useful to 
     * <code>JobListener</code>s and <code>TriggerListener</code>s.
     * 
     * @return Returns the jobRunTime.
     */
    public long getJobRunTime() {
        return jobRunTime;
    }
     
    /**
     * @param jobRunTime The jobRunTime to set.
     */
    public void setJobRunTime(long jobRunTime) {
        this.jobRunTime = jobRunTime;
    }
 
    /**
     * Put the specified value into the context's data map with the given key.
     * Possibly useful for sharing data between listeners and jobs.
     *
     * NOTE: this data is volatile - it is lost after the job execution
     * completes, and all TriggerListeners and JobListeners have been 
     * notified.
     *  
     * @param key
     * @param value
     */
    public void put(Object key, Object value) {
        data.put(key, value);
    }
     
    /**
     * Get the value with the given key from the context's data map.
     * 
     * @param key
     */
    public Object get(Object key) {
        return data.get(key);
    }
}




    3. 常用方法
       <1> public Scheduler getScheduler()

© 著作权归作者所有

绿林小子
粉丝 5
博文 131
码字总数 45566
作品 0
福州
私信 提问
在OpenJWeb平台中使用Spring框架配置定时器的实例

由于OpenJWeb平台集成了Spring框架,所以定时作业的开发非常方便,首先一个被定时器调用的类: package org.openjweb.common.timer; import org.apache.log4j.Logger; import org.quartz.JobExe...

迷途d书童
2012/03/09
205
0
Spring Cloud 2.x系列之springboot集成quartz

java设计模式之观察者模式 一大波视频分享 Mysql代理中间件Atlas安装和配置 在做项目时有时候会有定时器任务的功能,比如某某时间应该做什么,多少秒应该怎么样之类的。定时任务常用的业务场...

技术小能手
2018/10/15
0
0
OpenJWeb使用Spring框架实现定时作业调度的例子

因Spring集成了QuartzJob,所以实现定时作业调度非常方便,首先实现一个作业调度类,类里面可以任意增加用于定时调用的方法,如定时发邮件,定时构造索引,工作流引擎定时监控等,下面是一个继承了的...

迷途d书童
2012/03/09
112
0
关于Spring中用quartz定时器在定时到达时同时执行两次的问题

我在使用spring的quartz配置定时任务时,发现每次定时时间到达后,指定的定时方法同时执行两次,而且此方法还是使用的synchronized关键字,每次定时一到,会发现此方法内的System.out输出信息输出...

迷途d书童
2012/03/09
2.9K
1
Spring整合Quartz(JobDetailBean方式)

一、Spring创建JobDetail的两种方式 定时任务两种方式,Spring很好的封装使用Quartz的细节,第一种方式是利用SPring封装的Quartz类进行特定方法的实现,第二种是通过透明的使用Quartz达到定时...

Leons
2015/08/04
139
0

没有更多内容

加载失败,请刷新页面

加载更多

JS 打印控制

JS 打印控制 var PrintStartString = "<!--打印开始标示符-->";//设置打印开始区域var PrintEndString = "<!--打印结束标示符-->";//设置打印结束区域var HtmlText = window.do......

DrChenXX
18分钟前
5
0
LevelDB:使用介绍

LevelDB 提供的接口其实很简单,下面举例进行简单说明。 安装 git clone https://github.com/google/leveldb cd leveldb mkdir -p build && cd build cmake -DCMAKE_BUILD_TYPE=Release .. ......

slagga
23分钟前
5
0
《JavaScript正则表达式迷你书》读书笔记

正则基础 常见简写形式 字符组 具体含义 记忆方式 \d 表示 [0-9]。表示是一位数字。 其英文是 digit(数字) \D 表示 [^0-9]。表示除数字外的任意字符。 \w 表示 [0-9a-zA-Z_]。表示数字、大小...

muzi131313
28分钟前
4
0
Git的反悔操作

概述 这次主要来讲讲Git的反悔操作,自己平时在写代码的过程中经常会出现想要弃用所有的改动或回滚到上一次commit的情况。Git上的反悔操作有reset、rebase、revert等,每个操作各有区别和对应...

duduYZ
29分钟前
2
0
实现双向绑定Proxy比defineproperty优劣如何?

前言 双向绑定其实已经是一个老掉牙的问题了,只要涉及到MVVM框架就不得不谈的知识点,但它毕竟是Vue的三要素之一. Vue三要素 响应式: 例如如何监听数据变化,其中的实现方法就是我们提到的双向...

寻找海蓝
41分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部