Quartz之JobDataMap,PersistJobDataAfterExecution,DisallowConcurrentExecution

参考资料 
http://stackoverflow.com/questions/1636556/ensure-that-spring-quartz-job-execution-doesnt-overlap 

JobDataMap:任务数据传输对象,并且以Map形式存储,它重载了:putAsString()方法,如下图: 
,类说明如下: 
Java代码   收藏代码
  1. JobDataMap instances are stored once when the Job is added to a scheduler. They are also re-persisted after every execution of StatefulJob instances.  
  2. 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.  
  3. 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()方法,如下图: 
,类说明如下: 
Java代码   收藏代码
  1. An implementation of Map that wraps another Map and flags itself 'dirty' when it is modified, enforces that all keys are Strings.  
  2. All allowsTransientData flag related methods are deprecated as of version 1.6.   
,可以说:JobDataMap这个对象可以向任务传递任何对象. 
以下这个示例用到了Quartz的二个注解,其实它推荐我们使用这二个注解.在这篇文章中作了如下说明: 
 
以下这二个注解是我根据字面上的意思理解的呀  
@PersistJobDataAfterExecution:保存在JobDataMap传递的参数,当你要一个计数器的时候,详情可参见以下这个例子. 
@DisallowConcurrentExecution:保证多个任务间不会同时执行.所以在多任务执行时最好加上
 
具体参见代码(你可反复测试效果就知道了  ),详情请参见注解 
ColorJob.java 
Java代码   收藏代码
  1. package org.quartz.examples.example4;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5. import org.quartz.DisallowConcurrentExecution;  
  6. import org.quartz.Job;  
  7. import org.quartz.JobDataMap;  
  8. import org.quartz.JobExecutionContext;  
  9. import org.quartz.JobExecutionException;  
  10. import org.quartz.JobKey;  
  11. import org.quartz.PersistJobDataAfterExecution;  
  12. import org.slf4j.Logger;  
  13. import org.slf4j.LoggerFactory;  
  14.   
  15. @PersistJobDataAfterExecution  
  16. @DisallowConcurrentExecution  
  17. public class ColorJob implements Job {  
  18.   
  19.     private static Logger _log = LoggerFactory.getLogger(ColorJob.class);      
  20.      
  21.     public static final String FAVORITE_COLOR = "favorite color";  
  22.     public static final String EXECUTION_COUNT = "count";      
  23.      
  24.     //这个属性如不是static,那么每次都要实例这个任务类,始终打印为: 1  
  25.     private static  int _counter = 1;  
  26.      
  27.     public ColorJob() {  
  28.     }    
  29.     public void execute(JobExecutionContext context)  
  30.         throws JobExecutionException {  
  31.   
  32.         
  33.         JobKey jobKey = context.getJobDetail().getKey();          
  34.          
  35.         JobDataMap data = context.getJobDetail().getJobDataMap();  
  36.         String favoriteColor = data.getString(FAVORITE_COLOR);  
  37.         int count = data.getInt(EXECUTION_COUNT);  
  38.           
  39.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");          
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值