1 package weaver.interfaces.workflow.action; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.lang.reflect.Method; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 import weaver.conn.RecordSet; 13 import weaver.interfaces.schedule.BaseCronJob; 14 15 /** 16 * 定时任务:执行多个计划任务,凡是要修改requestid的都要放到这里执行 17 * @author Administrator 18 * 19 */ 20 public class TimerTask extends BaseCronJob{ 21 22 // 定时任务固定变量声明 23 private String requestid; 24 private String mainid; 25 private String sqr; 26 private String sqbm; 27 private Log log; 28 private static final String ClassName = "TimerTask"; 29 30 public TimerTask() { 31 log = LogFactory.getLog(TimerTask.class.getName()); 32 } 33 34 RecordSet recordSet = new RecordSet(); 35 36 /* 37 * 定时执行的任务,业务逻辑 38 */ 39 public void execute(){ 40 /* log.info("==========开始定时任务============="); 41 new NoticeOfDelivery().doTask(); 42 new Warehousing().doTask(); 43 log.info("==========结束定时任务=============");*/ 44 45 log.info("=======================开始定时任务========================"); 46 List<Class> classList = new ArrayList<Class>();//实现对应接口类的集合 47 log.info("====================TimerTask:1==========================="); 48 //path = d:/weaver/ecology/classbean/weaver/interfaces/workflow/action 49 //String path = TimerTask.class.getResource("").toString().substring(6);//TimerTask类目录 50 String path = "d:/weaver/ecology/classbean/weaver/interfaces/workflow/action"; 51 log.info("path = " + path); 52 try{ 53 List<String> fileList = find(path);//先获得path下所有的.class文件 54 log.info("====================TimerTask:2==========================="); 55 String packageName = TimerTask.class.getPackage().getName(); 56 log.info("packageName = " + packageName); 57 //获得所有的实现接口BaseTimerTask的类,加到classList 58 for (String className : fileList) { 59 if ("AccountUserAction".equals(className) || "AccountUserActionT".equals(className) || "BaseTimerTask".equals(className)) { 60 continue; 61 } 62 Class<Object> classObj = (Class<Object>) Class.forName(packageName+ "." +className); 63 if (BaseTimerTask.class.isAssignableFrom(classObj)) {//实现了doTask接口 64 classList.add(classObj); 65 } 66 } 67 log.info("====================TimerTask:3==========================="); 68 //调用符合条件的接口方法 69 for (Class Obj : classList) { 70 Method method = Obj.getMethod("doTask"); 71 method.invoke(Obj.newInstance()); 72 } 73 log.info("=======================结束定时任务========================"); 74 }catch (Exception e) { 75 e.printStackTrace(); 76 } 77 78 } 79 80 81 /* 82 * 得到一个目录下所有的文件 83 */ 84 public static List<String> find(String path) throws IOException{ 85 86 List<String> list = new ArrayList<String>(); 87 //获取pathName的File对象 88 File dirFile = new File(path); 89 //判断该文件或目录是否存在,不存在时在控制台输出提醒 90 if (!dirFile.exists()) { 91 return null; 92 } 93 //获取此目录下的所有文件名与目录名 94 String[] fileList = dirFile.list(); 95 for (int i = 0; i < fileList.length; i++) { 96 //遍历文件目录 97 String string = fileList[i]; 98 //File("documentName","fileName")是File的另一个构造器 99 File file = new File(string); 100 if (file.getName().endsWith(".class")) { 101 list.add(file.getName().substring(0, file.getName().indexOf("."))); 102 } 103 } 104 return list; 105 } 106 }