1. 程式人生 > >IT忍者神龜之Spring Task動態新增任務

IT忍者神龜之Spring Task動態新增任務

<task:scheduled-tasks>標籤的作用

 <!-- <task:scheduled-tasks> 自動註冊的 -->
  <value>org.springframework.scheduling.config.ContextLifecycleScheduledTaskRegistrar</value>
 <!-- <task:annotation-driven/> 自動註冊的 -->
 <value>org.springframework.context.annotation.internalAsyncAnnotationProcessor</value>
 <value>org.springframework.context.annotation.internalScheduledAnnotationProcessor</value>
 <value>org.springframework.scheduling.config.internalAsyncExecutionAspect</value>

String currentTaskId="taskJob";

WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
Map<String, String> webApplicationContextMap = GenerateCorrespondTaskMethod(currentTaskId,webApplicationContext);

StringBuffer sb=new StringBuffer();
for (Map.Entry<String, String> entry : webApplicationContextMap.entrySet()) {  
sb.append(entry.getKey()+","+entry.getValue());
}  
String[] aS = sb.toString().split(",");
ContextLifecycleScheduledTaskRegistrar contextLifecycleScheduledTaskRegistrar = new ContextLifecycleScheduledTaskRegistrar();
contextLifecycleScheduledTaskRegistrar.setApplicationContext(webApplicationContext);  
//contextLifecycleScheduledTaskRegistrar.addCronTask(new ScheduledMethodRunnableDongao(SpringContextHolder.getType("orderDeleteScheduledController").newInstance(),"updateExpiredOrders",oStringArrary,contextLifecycleScheduledTaskRegistrar,argsClass),"0/5 * * * * *");  
contextLifecycleScheduledTaskRegistrar.addCronTask(new ScheduledMethodRunnable(SpringContextHolder.getBean(aS[0]),aS[1]),"0/5 * * * * *");  
contextLifecycleScheduledTaskRegistrar.onApplicationEvent(new ContextRefreshedEvent(webApplicationContext));