mirror of
https://github.com/wahyd4/Martian.git
synced 2026-08-26 22:15:59 +10:00
add mars-timer
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package com.mars.timer.execute;
|
||||
|
||||
import com.mars.core.constant.MarsConstant;
|
||||
import com.mars.core.constant.MarsSpace;
|
||||
import com.mars.core.load.LoadHelper;
|
||||
import com.mars.core.logger.MarsLogger;
|
||||
import com.mars.core.model.MarsTimerModel;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* 执行定时任务
|
||||
*/
|
||||
public class ExecuteMarsTimer {
|
||||
|
||||
private static MarsLogger marsLogger = MarsLogger.getLogger(ExecuteMarsTimer.class);
|
||||
|
||||
/**
|
||||
* 获取全局存储空间
|
||||
*/
|
||||
private static MarsSpace constants = MarsSpace.getEasySpace();
|
||||
|
||||
/**
|
||||
* 执行
|
||||
*/
|
||||
public static void execute() {
|
||||
try {
|
||||
List<MarsTimerModel> marsTimerModelList = LoadHelper.getMarsTimersList();
|
||||
Date nowDate = new Date();
|
||||
for(MarsTimerModel marsTimerModel : marsTimerModelList){
|
||||
int loop = marsTimerModel.getMarsTimer().loop();
|
||||
/* 开启定时任务 */
|
||||
new Timer(marsTimerModel.getMethodName()).scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Object hasNettyStart = constants.getAttr(MarsConstant.HAS_NETTY_START);
|
||||
if(hasNettyStart != null){
|
||||
Object beanObject = marsTimerModel.getObj();
|
||||
Method method = marsTimerModel.getMethod();
|
||||
method.invoke(beanObject);
|
||||
}
|
||||
} catch (Exception e){
|
||||
marsLogger.error("执行定时任务出错,方法名:"+marsTimerModel.getCls().getName()+"."+marsTimerModel.getMethod().getName(),e);
|
||||
}
|
||||
}
|
||||
}, nowDate,loop);
|
||||
}
|
||||
} catch (Exception e){
|
||||
marsLogger.error("加载定时任务出错",e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.mars.timer.load;
|
||||
|
||||
import com.mars.core.annotation.MarsTimer;
|
||||
import com.mars.core.constant.MarsConstant;
|
||||
import com.mars.core.constant.MarsSpace;
|
||||
import com.mars.core.model.MarsTimerModel;
|
||||
import com.mars.core.load.LoadHelper;
|
||||
import com.mars.ioc.factory.BeanFactory;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 加载所有加了MarsTimer注解的bean
|
||||
*/
|
||||
public class LoadMarsTimer {
|
||||
|
||||
/**
|
||||
* 获取全局存储空间
|
||||
*/
|
||||
private static MarsSpace constants = MarsSpace.getEasySpace();
|
||||
|
||||
/**
|
||||
* 加载所有加了MarsTimer注解的对象
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void loadMarsTimers() throws Exception {
|
||||
/* 获取所有的bean数据 */
|
||||
List<Map<String,Object>> marsBeansList = LoadHelper.getBeanList();
|
||||
|
||||
List<MarsTimerModel> marsTimerObjects = LoadHelper.getMarsTimersList();
|
||||
|
||||
for(Map<String,Object> map : marsBeansList) {
|
||||
|
||||
Class<?> cls = (Class<?>)map.get("className");
|
||||
String beanName = LoadHelper.getBeanName(map,cls);
|
||||
loadMethods(cls,marsTimerObjects,beanName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载加了MarsTimer注解的方法,并保存
|
||||
* @param cls
|
||||
* @param marsTimerObjects
|
||||
*/
|
||||
private static void loadMethods(Class<?> cls, List<MarsTimerModel> marsTimerObjects,String beanName) throws Exception {
|
||||
Method[] methods = cls.getDeclaredMethods();
|
||||
for(Method method : methods){
|
||||
MarsTimer marsTimer = method.getAnnotation(MarsTimer.class);
|
||||
if(marsTimer != null){
|
||||
if(method.getParameterCount() > 0){
|
||||
throw new Exception("有参数的方法不可以添加定时任务,方法名:"+cls.getName()+"."+ method.getName());
|
||||
}
|
||||
Object beanObject = BeanFactory.getBean(beanName,cls);
|
||||
|
||||
MarsTimerModel marsTimerModel = new MarsTimerModel();
|
||||
marsTimerModel.setCls(cls);
|
||||
marsTimerModel.setMethodName(method.getName());
|
||||
marsTimerModel.setObj(beanObject);
|
||||
marsTimerModel.setMethod(method);
|
||||
marsTimerModel.setMarsTimer(marsTimer);
|
||||
marsTimerObjects.add(marsTimerModel);
|
||||
}
|
||||
}
|
||||
/* 保险起见,重新插入数据 */
|
||||
constants.setAttr(MarsConstant.MARS_TIMER_OBJECTS, marsTimerObjects);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user