update version

This commit is contained in:
yuye
2019-11-17 13:44:37 +08:00
parent 1bb3c55c79
commit 09b83d7030
5 changed files with 131 additions and 76 deletions
@@ -1,4 +1,4 @@
package com.mars.core.model;
package com.mars.aop.model;
import com.mars.core.enums.ExecutorType;
import com.mars.core.enums.TractionLevel;
@@ -13,6 +13,11 @@ public class AopModel {
*/
private Class cls;
/**
* Aop执行的对象
*/
private Object obj;
/**
* 事务隔离级别
*/
@@ -31,6 +36,14 @@ public class AopModel {
this.cls = cls;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public TractionLevel getTractionLevel() {
return tractionLevel;
}
@@ -4,10 +4,11 @@ import java.lang.reflect.Method;
import com.mars.aop.proxy.exec.ExecAop;
import com.mars.aop.proxy.exec.ExecRedisLock;
import com.mars.aop.proxy.exec.ExecTraction;
import com.mars.core.annotation.MarsAop;
import com.mars.core.annotation.RedisLock;
import com.mars.core.annotation.Traction;
import com.mars.core.model.AopModel;
import com.mars.aop.model.AopModel;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
@@ -45,50 +46,55 @@ public class MarsBeanProxy implements MethodInterceptor {
*/
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object obj = null;
Class c = null;
AopModel aopModel = null;
AopModel tractionModel = null;
RedisLock redisLock = null;
Boolean hasLock = false;
try {
MarsAop marsAop = method.getAnnotation(MarsAop.class);
Traction traction = method.getAnnotation(Traction.class);
redisLock = method.getAnnotation(RedisLock.class);
/* 校验同一个方法上不能同时存在aop和traction注解 */
ExecAop.checkAnnot(marsAop,traction,method,cls);
tractionModel = ExecTraction.getAopModel(traction);
aopModel = ExecAop.getAopModel(marsAop);
AopModel aopModel = ExecAop.getAopModel(marsAop,traction);
if (aopModel != null) {
c = aopModel.getCls();
}
if (c != null) {
obj = c.getDeclaredConstructor().newInstance();
}
/* 加分布式锁 */
Boolean hasLock = ExecRedisLock.lock(redisLock);
hasLock = ExecRedisLock.lock(redisLock);
if(!hasLock){
return null;
}
/* 开启事务 */
ExecTraction.beginTraction(args, tractionModel);
/* 执行aop的开始方法 */
ExecAop.startMethod(c, obj, args, aopModel);
ExecAop.startMethod(args, aopModel);
/* 执行方法本体 */
Object o1 = methodProxy.invokeSuper(o, args);
/* 执行aop的结束方法 */
ExecAop.endMethod(c, obj, args,o1);
ExecAop.endMethod(args,o1,aopModel);
/* 提交事务 */
ExecTraction.commit(args, tractionModel);
return o1;
} catch (Throwable e) {
ExecAop.exp(c, obj, e);
/* 回滚事务 */
ExecTraction.rollback(tractionModel,e);
/* AOP处理异常 */
ExecAop.exp(aopModel, e);
throw e;
} finally {
/* 解分布式锁, 如果失败了就重试,十次之后还失败,就不管了,由程序员排查问题 */
for(int i = 0;i<10;i++){
Boolean hasUnlock = ExecRedisLock.unlock(redisLock);
if(hasUnlock){
break;
if(hasLock){
for(int i = 0;i<10;i++){
Boolean hasUnlock = ExecRedisLock.unlock(redisLock);
if(hasUnlock){
break;
}
}
}
}
@@ -2,9 +2,7 @@ package com.mars.aop.proxy.exec;
import com.mars.aop.constant.AopConstant;
import com.mars.core.annotation.MarsAop;
import com.mars.core.annotation.Traction;
import com.mars.core.model.AopModel;
import com.mars.core.ncfg.traction.TractionClass;
import com.mars.aop.model.AopModel;
import java.lang.reflect.Method;
@@ -15,90 +13,58 @@ public class ExecAop {
/**
* 方法开始前
* @param c 类
* @param obj 对象
* @param args 参数
* @param aopModel Aop实体
* @throws Exception 异常
*/
public static void startMethod(Class c, Object obj, Object[] args, AopModel aopModel) throws Exception {
if (c == null) {
public static void startMethod(Object[] args, AopModel aopModel) throws Exception {
if (aopModel == null) {
return;
}
if (c.getName().equals(TractionClass.getCls().getName())) {
Method m2 = c.getDeclaredMethod(AopConstant.START_METHOD, new Class[]{Object[].class, AopModel.class});
m2.invoke(obj, new Object[]{args, aopModel});
} else {
Method m2 = c.getDeclaredMethod(AopConstant.START_METHOD, new Class[]{Object[].class});
m2.invoke(obj, new Object[]{args});
}
Method m2 = aopModel.getCls().getDeclaredMethod(AopConstant.START_METHOD, new Class[]{Object[].class});
m2.invoke(aopModel.getObj(), new Object[]{args});
}
/**
* 方法开始后
* @param c 类
* @param obj 对象
* @param args 参数
* @param result 参数
* @param aopModel 对象
* @throws Exception 异常
*/
public static void endMethod(Class c, Object obj, Object[] args, Object result) throws Exception {
if (c == null) {
public static void endMethod(Object[] args, Object result, AopModel aopModel) throws Exception {
if (aopModel == null) {
return;
}
if (c.getName().equals(TractionClass.getCls().getName())) {
Method m2 = c.getDeclaredMethod(AopConstant.END_METHOD, new Class[]{Object[].class});
m2.invoke(obj, new Object[]{args});
} else {
Method m2 = c.getDeclaredMethod(AopConstant.END_METHOD, new Class[]{Object[].class, Object.class});
m2.invoke(obj, new Object[]{args,result});
}
Method m2 = aopModel.getCls().getDeclaredMethod(AopConstant.END_METHOD, new Class[]{Object[].class, Object.class});
m2.invoke(aopModel.getObj(), new Object[]{args,result});
}
/**
* 异常
* @param c 类
* @param obj 对象
* @param aopModel 对象
* @param e 异常对象
* @throws Exception 异常
*/
public static void exp(Class c, Object obj, Throwable e) throws Exception {
if (c == null) {
public static void exp(AopModel aopModel, Throwable e) throws Exception {
if (aopModel == null) {
return;
}
Method m4 = c.getDeclaredMethod(AopConstant.EXP_METHOD, new Class[]{Throwable.class});
m4.invoke(obj, new Object[]{e});
}
/**
* 校验注解是否符合规则
* @param marsAop aop注解
* @param traction 事务监听注解
* @param method 被监听的方法
* @throws Exception 异常
*/
public static void checkAnnot(MarsAop marsAop, Traction traction, Method method,Class<?> cls) throws Exception {
if(marsAop != null && traction != null) {
throw new Exception(cls.getName()+"类中的["+method.getName()+"]方法同时存在MarsAop和Traction注解");
}
Method m4 = aopModel.getCls().getDeclaredMethod(AopConstant.EXP_METHOD, new Class[]{Throwable.class});
m4.invoke(aopModel.getObj(), new Object[]{e});
}
/**
* 获取aop实体
* @param marsAop aop注解
* @param traction 事务监听注解
* @return aop实体
*/
public static AopModel getAopModel(MarsAop marsAop,Traction traction){
AopModel aopModel = null;
public static AopModel getAopModel(MarsAop marsAop) throws Exception {
if(marsAop != null) {
aopModel = new AopModel();
AopModel aopModel = new AopModel();
aopModel.setCls(marsAop.className());
} else if(traction != null) {
aopModel = new AopModel();
aopModel.setCls(TractionClass.getCls());
aopModel.setTractionLevel(traction.level());
aopModel.setExecutorType(traction.executorType());
aopModel.setObj(marsAop.className().getDeclaredConstructor().newInstance());
}
return aopModel;
return null;
}
}
@@ -0,0 +1,70 @@
package com.mars.aop.proxy.exec;
import com.mars.aop.constant.AopConstant;
import com.mars.core.annotation.Traction;
import com.mars.aop.model.AopModel;
import com.mars.core.ncfg.traction.TractionClass;
import java.lang.reflect.Method;
public class ExecTraction {
/**
* 开启事务
* @param args 参数
* @param aopModel Aop实体
* @throws Exception 异常
*/
public static void beginTraction(Object[] args, AopModel aopModel) throws Exception {
if (aopModel == null) {
return;
}
Method m2 = aopModel.getCls().getDeclaredMethod(AopConstant.START_METHOD, new Class[]{Object[].class, AopModel.class});
m2.invoke(aopModel.getObj(), new Object[]{args, aopModel});
}
/**
* 提交事务
* @param aopModel 对象
* @param args 参数
* @throws Exception 异常
*/
public static void commit(Object[] args, AopModel aopModel) throws Exception {
if (aopModel == null) {
return;
}
Method m2 = aopModel.getCls().getDeclaredMethod(AopConstant.END_METHOD, new Class[]{Object[].class});
m2.invoke(aopModel.getObj(), new Object[]{args});
}
/**
* 回滚事务
* @param aopModel 对象
* @param e 异常对象
* @throws Exception 异常
*/
public static void rollback(AopModel aopModel, Throwable e) throws Exception {
if (aopModel == null) {
return;
}
Method m4 = aopModel.getCls().getDeclaredMethod(AopConstant.EXP_METHOD, new Class[]{Throwable.class});
m4.invoke(aopModel.getObj(), new Object[]{e});
}
/**
* 获取aop实体
* @param traction 事务监听注解
* @return aop实体
*/
public static AopModel getAopModel(Traction traction) throws Exception {
if(traction != null){
AopModel aopModel = new AopModel();
aopModel.setCls(TractionClass.getCls());
aopModel.setTractionLevel(traction.level());
aopModel.setExecutorType(traction.executorType());
aopModel.setObj(TractionClass.getCls().getDeclaredConstructor().newInstance());
return aopModel;
}
return null;
}
}
@@ -3,7 +3,7 @@ package com.mars.jdbc.traction;
import com.alibaba.druid.pool.DruidDataSource;
import com.mars.core.constant.MarsConstant;
import com.mars.core.constant.MarsSpace;
import com.mars.core.model.AopModel;
import com.mars.aop.model.AopModel;
import com.mars.core.util.ThreadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;