spring task db

来源:互联网 发布:ps10.0官方软件下载 编辑:程序博客网 时间:2024/06/06 00:10
1. 根据配置文件 创建ScheduledTaskRegistrar 对象之后, 
TaskScheduler taskScheduler 任务池  已有值; 
      Map<Runnable, String> cronTasks  计划任务map,key为任务对象,value为定时表达式 
     Set<ScheduledFuture<?>> scheduledFutures = new LinkedHashSet<ScheduledFuture<?>>(); 被创建 
2.ScheduledTaskRegistrar.afterPropertiesSet()被执行 
      
              根据  cronTasks 创建 ScheduledFuture 放入 scheduledFutures  中 
        如下: 
         for (Map.Entry<Runnable, String> entry : this.cronTasks.entrySet()) { 
this.scheduledFutures.add(this.taskScheduler.schedule(entry.getKey(), new CronTrigger(entry.getValue()))); 
         } 
        
         (1)创建ScheduledFuture 是通过创建ReschedulingRunnable 对象调用对应的 schedule() 方法来完成。 
         (2)在 ReschedulingRunnable . schedule() 中,根据表达式计算当前任务下次执行时间,然后将this, 也就是ReschedulingRunnable对象本身, 
交由 taskScheduler  任务池 去执行。 
(3)任务池到时执行 ReschedulingRunnable 对象的run方法,记录开始执行的时间和  调用supper.run()后的结束时间,用这两个时间修改triggerContext; 
                   因为 ReschedulingRunnable  对象在创建的时候,已将业务方法的执行代理,给了 supper的delegate属性,所以supper.run()执行的时候其实就是 
   执行 delegate.run() 方法,也就是执行业务方法; 
           ScheduledMethodRunnable  类只有两个属性,一个是业务对象,一个是要执行的方法;然后在run方法中就是用反射  执行业务对象的 对应方法而已。 
          获取一个类的方法对象 getClass().getMethod(methodName); 即可; 


动态修改指定定时任务表达式,代码如下: 
Java代码  收藏代码
  1. public class AuditDefectServiceTest {  
  2.     public static void main(String[] args){  
  3.         ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:test-context.xml");  
  4.         appContext.start();  
  5.         try {  
  6.             System.out.println("start..");  
  7.             Thread.sleep(1000*4);  
  8.             System.out.println("end...");  
  9.         } catch (InterruptedException e) {  
  10.             // TODO Auto-generated catch block  
  11.             e.printStackTrace();  
  12.         }  
  13.         //appContext.refresh();  
  14.         ScheduledTaskRegistrar s = appContext.getBean(ScheduledTaskRegistrar.class);  
  15.         try {  
  16.               
  17.             Field f = s.getClass().getDeclaredField("scheduledFutures");  
  18.             ReflectionUtils.makeAccessible(f);  
  19.               
  20.             Set<ScheduledFuture<?>> rt =  (Set<ScheduledFuture<?>>) ReflectionUtils.getField(f, s);  
  21.             for (ScheduledFuture<?> it : rt){  
  22.                 Field f2 = it.getClass().getDeclaredField("trigger");  
  23.                 ReflectionUtils.makeAccessible(f2);  
  24.                 CronTrigger trigger = (CronTrigger) ReflectionUtils.getField(f2, it);  
  25.                 Field f3 = trigger.getClass().getDeclaredField("sequenceGenerator");  
  26.                 ReflectionUtils.makeAccessible(f3);  
  27.                 ReflectionUtils.setField(f3, trigger, new CronSequenceGenerator("0/2 * * 5-29 * ?", TimeZone.getDefault()));  
  28.                   
  29.                 Method scheduleMethod = it.getClass().getDeclaredMethod("schedule");  
  30.                 ReflectionUtils.makeAccessible(scheduleMethod);  
  31.                 ReflectionUtils.invokeMethod(scheduleMethod, it);  
  32.             }  
  33.         } catch (SecurityException e) {  
  34.             // TODO Auto-generated catch block  
  35.             e.printStackTrace();  
  36.         } catch (NoSuchFieldException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.         } catch (NoSuchMethodException e) {  
  40.             // TODO Auto-generated catch block  
  41.             e.printStackTrace();  
  42.         }  
  43.           
  44.     }  
  45. }  
0 0
原创粉丝点击