mirror of
https://github.com/wahyd4/Martian.git
synced 2026-08-09 05:16:21 +10:00
add mars-timer
This commit is contained in:
@@ -44,8 +44,12 @@ public class CglibProxy implements MethodInterceptor {
|
||||
@Override
|
||||
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
|
||||
Object obj = null;
|
||||
Class c = null;
|
||||
|
||||
AopModel aopModel = list.get(method.getName());
|
||||
Class c = aopModel.getCls();
|
||||
if(aopModel != null){
|
||||
c = aopModel.getCls();
|
||||
}
|
||||
if(c != null){
|
||||
obj = c.getDeclaredConstructor().newInstance();
|
||||
if(c.getName().equals(MarsConstant.TRACTION_CLASS)){
|
||||
|
||||
+15
-1
@@ -5,6 +5,7 @@
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
@@ -14,8 +15,21 @@
|
||||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-jdk14:1.7.25" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.11.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-core:2.11.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.jyaml:jyaml:1.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.24" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.14.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.17.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-module-junit4:1.6.6" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-module-junit4-common:1.6.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.powermock:powermock-core:1.6.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.powermock:powermock-reflect:1.6.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.diffblue:deeptestutils:1.9.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.javassist:javassist:3.21.0-GA" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.objenesis:objenesis:2.5.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.powermock:powermock-api-mockito:1.6.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mockito:mockito-core:1.10.19" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.powermock:powermock-api-mockito-common:1.6.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.powermock:powermock-api-support:1.6.5" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -2,9 +2,13 @@ package com.mars.core.after;
|
||||
|
||||
import com.mars.core.constant.MarsConstant;
|
||||
import com.mars.core.constant.MarsSpace;
|
||||
import com.mars.core.load.LoadHelper;
|
||||
import com.mars.core.load.WriteFields;
|
||||
import com.mars.core.model.MarsBeanModel;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 框架启动后立刻执行
|
||||
@@ -24,8 +28,15 @@ public class StartAfter {
|
||||
if(afterObjects != null) {
|
||||
List<Class> easyLoads = (List<Class>)afterObjects;
|
||||
|
||||
Map<String, MarsBeanModel> beanModelMap = LoadHelper.getBeanObjectMap();
|
||||
|
||||
for(Class cls : easyLoads){
|
||||
Object obj = cls.getDeclaredConstructor().newInstance();
|
||||
|
||||
/* 注入属性 */
|
||||
WriteFields.writeFields(cls,obj,beanModelMap);
|
||||
|
||||
/* 执行after方法 */
|
||||
Method method2 = cls.getDeclaredMethod("after");
|
||||
method2.invoke(obj);
|
||||
}
|
||||
|
||||
@@ -11,5 +11,5 @@ public @interface MarsTimer {
|
||||
* 循环间隔
|
||||
* @return
|
||||
*/
|
||||
long loop();
|
||||
int loop() default 60000;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ public class MarsConstant {
|
||||
* 记录是否已经调用完createBean方法了
|
||||
*/
|
||||
public static final String HAS_START="hasStart";
|
||||
/**
|
||||
* 记录netty是否已经启动了
|
||||
*/
|
||||
public static final String HAS_NETTY_START="hasNettyStart";
|
||||
/**
|
||||
* 记录是否已经启动过单测了
|
||||
*/
|
||||
@@ -68,6 +72,11 @@ public class MarsConstant {
|
||||
*/
|
||||
public static final String MARS_BEAN_OBJECTS = "marsBeanObjects";
|
||||
|
||||
/**
|
||||
* 所有加了MarsTimer注解的方法
|
||||
*/
|
||||
public static final String MARS_TIMER_OBJECTS = "marsTimerObjects";
|
||||
|
||||
/**
|
||||
* 接受远程配置中心通知的controller
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.mars.core.load;
|
||||
|
||||
import com.mars.core.annotation.MarsBean;
|
||||
import com.mars.core.annotation.Resource;
|
||||
import com.mars.core.constant.MarsConstant;
|
||||
import com.mars.core.constant.MarsSpace;
|
||||
import com.mars.core.model.MarsBeanModel;
|
||||
import com.mars.core.model.MarsTimerModel;
|
||||
import com.mars.core.util.StringUtil;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 加载工具类
|
||||
*/
|
||||
public class LoadHelper {
|
||||
|
||||
/**
|
||||
* 获取全局存储空间
|
||||
*/
|
||||
private static MarsSpace constants = MarsSpace.getEasySpace();
|
||||
|
||||
/**
|
||||
* 获取Bean的名称
|
||||
* @param map
|
||||
* @param cls
|
||||
* @return
|
||||
*/
|
||||
public static String getBeanName(Map<String,Object> map,Class<?> cls){
|
||||
MarsBean marsBean = (MarsBean)map.get("annotation");
|
||||
String beanName = marsBean.value();
|
||||
return getBeanName(beanName,cls);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Bean的名称
|
||||
* @param cls
|
||||
* @return
|
||||
*/
|
||||
public static String getBeanName(String beanName,Class<?> cls){
|
||||
if(beanName == null || beanName.equals("")) {
|
||||
beanName = StringUtil.getFirstLowerCase(cls.getSimpleName());
|
||||
}
|
||||
return beanName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字段名
|
||||
* @param resource
|
||||
* @param f
|
||||
* @return
|
||||
*/
|
||||
public static String getResourceName(Resource resource, Field f){
|
||||
String filedName = resource.value();
|
||||
if(filedName == null || filedName.equals("")) {
|
||||
filedName = f.getName();
|
||||
}
|
||||
return filedName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的加了MarsBean注解的基础数据
|
||||
* @return
|
||||
*/
|
||||
public static List<Map<String,Object>> getBeanList(){
|
||||
/* 获取所有的bean数据 */
|
||||
Object marsBeans = constants.getAttr(MarsConstant.MARS_BEANS);
|
||||
List<Map<String,Object>> marsBeansList = new ArrayList<>();
|
||||
if(marsBeans != null) {
|
||||
marsBeansList = (List<Map<String,Object>>)marsBeans;
|
||||
}
|
||||
return marsBeansList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的bean对象
|
||||
*/
|
||||
public static Map<String, MarsBeanModel> getBeanObjectMap(){
|
||||
/* 创建bean对象,并保存起来 */
|
||||
Object objs2 = constants.getAttr(MarsConstant.MARS_BEAN_OBJECTS);
|
||||
Map<String, MarsBeanModel> marsBeanObjects = new HashMap<>();
|
||||
if(objs2 != null) {
|
||||
marsBeanObjects = (Map<String, MarsBeanModel>)objs2;
|
||||
}
|
||||
return marsBeanObjects;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有配了定时任务的基础数据
|
||||
* @return
|
||||
*/
|
||||
public static List<MarsTimerModel> getMarsTimersList(){
|
||||
Object objs = constants.getAttr(MarsConstant.MARS_TIMER_OBJECTS);
|
||||
List<MarsTimerModel> marsTimerObjects = new ArrayList<>();
|
||||
if(objs != null) {
|
||||
marsTimerObjects = (List<MarsTimerModel>)objs;
|
||||
}
|
||||
return marsTimerObjects;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的加了Controller注解的基础数据
|
||||
* @return
|
||||
*/
|
||||
public static List<Map<String,Object>> getControllerList(){
|
||||
/* 获取所有的controller数据 */
|
||||
Object objs = constants.getAttr(MarsConstant.CONTROLLERS);
|
||||
List<Map<String,Object>> contorls = new ArrayList<>();
|
||||
if(objs != null) {
|
||||
contorls = (List<Map<String,Object>>)objs;
|
||||
}
|
||||
return contorls;
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,7 @@ public class WriteFields {
|
||||
if(resource!=null){
|
||||
f.setAccessible(true);
|
||||
|
||||
String filedName = resource.value();
|
||||
if(filedName == null || filedName.equals("")) {
|
||||
filedName = f.getName();
|
||||
}
|
||||
String filedName = LoadHelper.getResourceName(resource,f);
|
||||
|
||||
MarsBeanModel beanModel = marsBeanObjects.get(filedName);
|
||||
if(beanModel!=null){
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.mars.core.model;
|
||||
|
||||
import com.mars.core.annotation.MarsTimer;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 定时任务实体类
|
||||
*/
|
||||
public class MarsTimerModel {
|
||||
|
||||
/**
|
||||
* method名称
|
||||
*/
|
||||
private String methodName;
|
||||
|
||||
/**
|
||||
* 要执行的方法
|
||||
*/
|
||||
private Method method;
|
||||
|
||||
/**
|
||||
* 定时任务注解
|
||||
*/
|
||||
private MarsTimer marsTimer;
|
||||
|
||||
/**
|
||||
* bean对象
|
||||
*/
|
||||
private Object obj;
|
||||
|
||||
/**
|
||||
* class对象
|
||||
*/
|
||||
private Class<?> cls;
|
||||
|
||||
public String getMethodName() {
|
||||
return methodName;
|
||||
}
|
||||
|
||||
public void setMethodName(String methodName) {
|
||||
this.methodName = methodName;
|
||||
}
|
||||
|
||||
public Object getObj() {
|
||||
return obj;
|
||||
}
|
||||
|
||||
public void setObj(Object obj) {
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
public Class<?> getCls() {
|
||||
return cls;
|
||||
}
|
||||
|
||||
public void setCls(Class<?> cls) {
|
||||
this.cls = cls;
|
||||
}
|
||||
|
||||
public Method getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(Method method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public MarsTimer getMarsTimer() {
|
||||
return marsTimer;
|
||||
}
|
||||
|
||||
public void setMarsTimer(MarsTimer marsTimer) {
|
||||
this.marsTimer = marsTimer;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import com.mars.core.constant.MarsSpace;
|
||||
import com.mars.core.logger.MarsLogger;
|
||||
import com.mars.core.model.AopModel;
|
||||
import com.mars.core.model.MarsBeanModel;
|
||||
import com.mars.core.load.LoadHelper;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
@@ -109,13 +110,7 @@ public class BeanFactory {
|
||||
*/
|
||||
public static <T> T getBean(String name,Class<T> cls) throws Exception {
|
||||
try {
|
||||
|
||||
Object marsBeans = constants.getAttr(MarsConstant.MARS_BEAN_OBJECTS);
|
||||
Map<String, MarsBeanModel> marsBeanObjects = new HashMap<>();
|
||||
if(marsBeans != null) {
|
||||
marsBeanObjects = (Map<String, MarsBeanModel>)marsBeans;
|
||||
}
|
||||
|
||||
Map<String, MarsBeanModel> marsBeanObjects = LoadHelper.getBeanObjectMap();
|
||||
return (T)marsBeanObjects.get(name).getObj();
|
||||
} catch (Exception e) {
|
||||
throw new Exception("找不到name为["+name+"]的bean",e);
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package com.mars.ioc.load;
|
||||
|
||||
import com.mars.core.annotation.MarsBean;
|
||||
import com.mars.core.constant.MarsConstant;
|
||||
import com.mars.core.constant.MarsSpace;
|
||||
import com.mars.core.load.WriteFields;
|
||||
import com.mars.core.model.MarsBeanModel;
|
||||
import com.mars.core.util.StringUtil;
|
||||
import com.mars.core.load.LoadHelper;
|
||||
import com.mars.ioc.factory.BeanFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -30,29 +28,16 @@ public class LoadEasyBean {
|
||||
public static void loadBean() throws Exception{
|
||||
try {
|
||||
/* 获取所有的bean数据 */
|
||||
Object marsBeans = constants.getAttr(MarsConstant.MARS_BEANS);
|
||||
List<Map<String,Object>> marsBeansList = null;
|
||||
if(marsBeans != null) {
|
||||
marsBeansList = (List<Map<String,Object>>)marsBeans;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
List<Map<String,Object>> marsBeansList = LoadHelper.getBeanList();
|
||||
|
||||
/* 创建bean对象,并保存起来 */
|
||||
Object objs2 = constants.getAttr(MarsConstant.MARS_BEAN_OBJECTS);
|
||||
Map<String, MarsBeanModel> marsBeanObjects = new HashMap<>();
|
||||
if(objs2 != null) {
|
||||
marsBeanObjects = (Map<String, MarsBeanModel>)objs2;
|
||||
}
|
||||
Map<String, MarsBeanModel> marsBeanObjects = LoadHelper.getBeanObjectMap();
|
||||
|
||||
for(Map<String,Object> map : marsBeansList) {
|
||||
|
||||
|
||||
Class<?> cls = (Class<?>)map.get("className");
|
||||
MarsBean marsBean = (MarsBean)map.get("annotation");
|
||||
|
||||
String beanName = marsBean.value();
|
||||
if(beanName == null || beanName.equals("")) {
|
||||
beanName = StringUtil.getFirstLowerCase(cls.getSimpleName());
|
||||
}
|
||||
String beanName = LoadHelper.getBeanName(map,cls);
|
||||
|
||||
if(marsBeanObjects.get(beanName) == null) {
|
||||
MarsBeanModel beanModel = new MarsBeanModel();
|
||||
beanModel.setName(beanName);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.mars.mvc.load;
|
||||
|
||||
import com.mars.core.load.WriteFields;
|
||||
import com.mars.core.load.LoadHelper;
|
||||
import com.mars.mvc.proxy.MvcCglibProxy;
|
||||
import com.mars.core.annotation.MarsMapping;
|
||||
import com.mars.core.constant.MarsConstant;
|
||||
@@ -34,17 +35,11 @@ public class LoadController {
|
||||
Map<String, MarsMappingModel> controlObjects = new HashMap<>();
|
||||
|
||||
/* 获取所有的controller数据 */
|
||||
Object objs = constants.getAttr(MarsConstant.CONTROLLERS);
|
||||
List<Map<String,Object>> contorls = null;
|
||||
if(objs != null) {
|
||||
contorls = (List<Map<String,Object>>)objs;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
List<Map<String,Object>> controlList = LoadHelper.getControllerList();
|
||||
|
||||
Map<String, MarsBeanModel> marsBeanObjs = getMarsBeans();
|
||||
|
||||
for(Map<String,Object> map : contorls) {
|
||||
for(Map<String,Object> map : controlList) {
|
||||
|
||||
Class<?> cls = (Class<?>)map.get("className");
|
||||
|
||||
@@ -104,11 +99,6 @@ public class LoadController {
|
||||
* @return duix
|
||||
*/
|
||||
private static Map<String, MarsBeanModel> getMarsBeans() {
|
||||
Object objs = constants.getAttr(MarsConstant.MARS_BEAN_OBJECTS);
|
||||
Map<String, MarsBeanModel> easyBeanObjs = new HashMap<>();
|
||||
if(objs != null) {
|
||||
easyBeanObjs = (Map<String, MarsBeanModel>)objs;
|
||||
}
|
||||
return easyBeanObjs;
|
||||
return LoadHelper.getBeanObjectMap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.mars.netty.server;
|
||||
|
||||
import com.mars.core.constant.MarsConstant;
|
||||
import com.mars.core.constant.MarsSpace;
|
||||
import com.mars.core.logger.MarsLogger;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
@@ -15,6 +17,11 @@ import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
public class MarsServer {
|
||||
|
||||
private static MarsLogger log = MarsLogger.getLogger(MarsServer.class);
|
||||
|
||||
/**
|
||||
* 获取全局存储空间
|
||||
*/
|
||||
private static MarsSpace constants = MarsSpace.getEasySpace();
|
||||
|
||||
/**
|
||||
* 启动netty服务
|
||||
@@ -35,6 +42,9 @@ public class MarsServer {
|
||||
|
||||
log.info("启动成功");
|
||||
|
||||
/* 标识netty是否已经启动 */
|
||||
constants.setAttr(MarsConstant.HAS_NETTY_START,"yes");
|
||||
|
||||
f.channel().closeFuture().sync();
|
||||
} catch (Exception e) {
|
||||
log.error("启动netty报错",e);
|
||||
|
||||
@@ -27,6 +27,11 @@
|
||||
<artifactId>mars-junit</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<artifactId>mars-timer</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -7,12 +7,17 @@ import com.mars.core.constant.MarsSpace;
|
||||
import com.mars.core.load.LoadClass;
|
||||
import com.mars.core.logger.MarsLogger;
|
||||
import com.mars.core.util.ConfigUtil;
|
||||
import com.mars.junit.StartList;
|
||||
import com.mars.mvc.load.LoadInters;
|
||||
import com.mars.netty.server.MarsServer;
|
||||
import com.mars.ioc.load.LoadEasyBean;
|
||||
import com.mars.jdbc.base.BaseInitJdbc;
|
||||
import com.mars.mvc.load.LoadController;
|
||||
import com.mars.mvc.servlet.MarsCoreServlet;
|
||||
import com.mars.timer.execute.ExecuteMarsTimer;
|
||||
import com.mars.timer.load.LoadMarsTimer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 启动Mars框架
|
||||
@@ -32,7 +37,7 @@ public class BaseStartMars {
|
||||
* 启动Mars框架
|
||||
* @param clazz
|
||||
*/
|
||||
public static void start(Class<?> clazz, BaseInitJdbc baseInitJdbc,String suffix) {
|
||||
public static void start(Class<?> clazz, BaseInitJdbc baseInitJdbc, String suffix, List<StartList> startList) {
|
||||
try {
|
||||
|
||||
log.info("程序启动中......");
|
||||
@@ -40,12 +45,19 @@ public class BaseStartMars {
|
||||
/* 加载框架数据 */
|
||||
load(clazz,baseInitJdbc,suffix);
|
||||
|
||||
/* 标识createBean方法已经调用完毕 */
|
||||
constants.setAttr(MarsConstant.HAS_START,"yes");
|
||||
/* 扩展要加载的东西 */
|
||||
if(startList != null){
|
||||
for(StartList item : startList){
|
||||
item.load();
|
||||
}
|
||||
}
|
||||
|
||||
/* 启动after方法 */
|
||||
StartAfter.after();
|
||||
|
||||
/* 执行定时任务 */
|
||||
ExecuteMarsTimer.execute();
|
||||
|
||||
/* 启动netty */
|
||||
MarsServer.start(getPort());
|
||||
|
||||
@@ -89,6 +101,12 @@ public class BaseStartMars {
|
||||
|
||||
/* 创建interceptor对象 */
|
||||
LoadInters.loadIntersList();
|
||||
|
||||
/* 标识createBean方法已经调用完毕 */
|
||||
constants.setAttr(MarsConstant.HAS_START,"yes");
|
||||
|
||||
/* 加载timer对象 */
|
||||
LoadMarsTimer.loadMarsTimers();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,7 @@ timeOut:
|
||||
maxContentLength: 10
|
||||
#配置跨域请求
|
||||
cross_domain:
|
||||
origin: *
|
||||
origin: "*"
|
||||
methods: "GET,POST"
|
||||
maxAge: 9
|
||||
headers: "x-requested-with,Cache-Control,Pragma,Content-Type,Token"
|
||||
|
||||
@@ -15,7 +15,7 @@ public class StartMars {
|
||||
*/
|
||||
public static void start(Class<?> clazz,String[] args){
|
||||
if(args != null && args[0] != null){
|
||||
BaseStartMars.start(clazz,new InitJdbc(),args[0]);
|
||||
BaseStartMars.start(clazz,new InitJdbc(),args[0],null);
|
||||
} else {
|
||||
start(clazz);
|
||||
}
|
||||
@@ -26,6 +26,6 @@ public class StartMars {
|
||||
* @param clazz
|
||||
*/
|
||||
public static void start(Class<?> clazz){
|
||||
BaseStartMars.start(clazz,new InitJdbc(),null);
|
||||
BaseStartMars.start(clazz,new InitJdbc(),null,null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class StartMars {
|
||||
*/
|
||||
public static void start(Class<?> clazz,String[] args) {
|
||||
if(args != null && args[0] != null){
|
||||
BaseStartMars.start(clazz,new InitJdbc(),args[0]);
|
||||
BaseStartMars.start(clazz,new InitJdbc(),args[0],null);
|
||||
} else {
|
||||
start(clazz);
|
||||
}
|
||||
@@ -26,6 +26,6 @@ public class StartMars {
|
||||
* @param clazz
|
||||
*/
|
||||
public static void start(Class<?> clazz) {
|
||||
BaseStartMars.start(clazz,new InitJdbc(),null);
|
||||
BaseStartMars.start(clazz,new InitJdbc(),null,null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ public class StartMars {
|
||||
*/
|
||||
public static void start(Class<?> clazz,String[] args) {
|
||||
if(args != null && args[0] != null){
|
||||
BaseStartMars.start(clazz,null,args[0]);
|
||||
BaseStartMars.start(clazz,null,args[0],null);
|
||||
} else {
|
||||
start(clazz);
|
||||
}
|
||||
@@ -25,6 +25,6 @@ public class StartMars {
|
||||
* @param clazz
|
||||
*/
|
||||
public static void start(Class<?> clazz) {
|
||||
BaseStartMars.start(clazz,null,null);
|
||||
BaseStartMars.start(clazz,null,null,null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>Mars-java</artifactId>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<version>2.1.3</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mars-timer</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<artifactId>mars-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<artifactId>mars-ioc</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,8 @@
|
||||
<module>mars-start</module>
|
||||
<module>mars-jdbc</module>
|
||||
<module>mars-junit</module>
|
||||
</modules>
|
||||
<module>mars-timer</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
|
||||
@@ -60,6 +61,11 @@
|
||||
<artifactId>mars-junit</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<artifactId>mars-timer</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
@@ -123,11 +129,11 @@
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.10</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper</artifactId>
|
||||
<version>4.1.3</version>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper</artifactId>
|
||||
<version>4.1.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
Reference in New Issue
Block a user