参考资料
http://stackoverflow.com/questions/1636556/ensure-that-spring-quartz-job-execution-doesnt-overlap
JobDataMap:任务数据传输对象,并且以Map形式存储,它重载了:putAsString()方法,如下图:
,类说明如下:
它继承了父类:StringKeyDirtyFlagMap,它重载了方法:put()方法,如下图:
,类说明如下:
,可以说:JobDataMap这个对象可以向任务传递任何对象.
以下这个示例用到了Quartz的二个注解,其实它推荐我们使用这二个注解.在这篇文章中作了如下说明:
以下这二个注解是我根据字面上的意思理解的呀
@PersistJobDataAfterExecution:保存在JobDataMap传递的参数,当你要一个计数器的时候,详情可参见以下这个例子.
@DisallowConcurrentExecution:保证多个任务间不会同时执行.所以在多任务执行时最好加上
具体参见代码(你可反复测试效果就知道了
),详情请参见注解
ColorJob.java
http://stackoverflow.com/questions/1636556/ensure-that-spring-quartz-job-execution-doesnt-overlap
JobDataMap:任务数据传输对象,并且以Map形式存储,它重载了:putAsString()方法,如下图:
![](http://dl.iteye.com/upload/attachment/533937/fb5040f0-336d-36aa-bdf5-3a485b3d8278.jpg)
- JobDataMap instances are stored once when the Job is added to a scheduler. They are also re-persisted after every execution of StatefulJob instances.
- JobDataMap instances can also be stored with a Trigger. This can be useful in the case where you have a Job that is stored in the scheduler for regular/repeated use by multiple Triggers, yet with each independent triggering, you want to supply the Job with different data inputs.
- The JobExecutionContext passed to a Job at execution time also contains a convenience JobDataMap that is the result of merging the contents of the trigger's JobDataMap (if any) over the Job's JobDataMap (if any).
它继承了父类:StringKeyDirtyFlagMap,它重载了方法:put()方法,如下图:
![](http://dl.iteye.com/upload/attachment/533945/2d9fec15-2638-3920-9eb2-13182ec67402.jpg)
- An implementation of Map that wraps another Map and flags itself 'dirty' when it is modified, enforces that all keys are Strings.
- All allowsTransientData flag related methods are deprecated as of version 1.6.
以下这个示例用到了Quartz的二个注解,其实它推荐我们使用这二个注解.在这篇文章中作了如下说明:
![](http://dl.iteye.com/upload/attachment/533948/1585fcc2-ea62-329e-ae86-4559704703e0.jpg)
以下这二个注解是我根据字面上的意思理解的呀
![](https://i-blog.csdnimg.cn/blog_migrate/62af6430d8f5231bec4b4a02e2c53b8b.gif)
@PersistJobDataAfterExecution:保存在JobDataMap传递的参数,当你要一个计数器的时候,详情可参见以下这个例子.
@DisallowConcurrentExecution:保证多个任务间不会同时执行.所以在多任务执行时最好加上
具体参见代码(你可反复测试效果就知道了
![](https://i-blog.csdnimg.cn/blog_migrate/bc1400c727cc0f35a70deb28ad608d00.gif)
ColorJob.java
- package org.quartz.examples.example4;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.quartz.DisallowConcurrentExecution;
- import org.quartz.Job;
- import org.quartz.JobDataMap;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.quartz.JobKey;
- import org.quartz.PersistJobDataAfterExecution;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- @PersistJobDataAfterExecution
- @DisallowConcurrentExecution
- public class ColorJob implements Job {
- private static Logger _log = LoggerFactory.getLogger(ColorJob.class);
- public static final String FAVORITE_COLOR = "favorite color";
- public static final String EXECUTION_COUNT = "count";
- //这个属性如不是static,那么每次都要实例这个任务类,始终打印为: 1
- private static int _counter = 1;
- public ColorJob() {
- }
- public void execute(JobExecutionContext context)
- throws JobExecutionException {
- JobKey jobKey = context.getJobDetail().getKey();
- JobDataMap data = context.getJobDetail().getJobDataMap();
- String favoriteColor = data.getString(FAVORITE_COLOR);
- int count = data.getInt(EXECUTION_COUNT);
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");