Commit 799dc67f7ee7bcb36e61b6448ba5ab15e58c5a74
1 parent
669aee46
重构,将拦截器的ProceedingJoinPoint的相关调用重构到MethodContext类中
Showing
26 changed files
with
217 additions
and
174 deletions
tcc-transaction-api/src/main/java/org/mengyun/tcctransaction/api/annotation/Compensable.java
| ... | ... | @@ -12,8 +12,12 @@ import java.lang.annotation.Target; |
| 12 | 12 | @Retention(RetentionPolicy.RUNTIME) |
| 13 | 13 | @Target({ ElementType.METHOD }) |
| 14 | 14 | public @interface Compensable { |
| 15 | - | |
| 15 | + /** confirm方法名 */ | |
| 16 | 16 | public String confirmMethod() default ""; |
| 17 | - | |
| 17 | + /** cancel方法名 */ | |
| 18 | 18 | public String cancelMethod() default ""; |
| 19 | + /** 是否异步调用confirm */ | |
| 20 | + public boolean asyncConfirm() default false; | |
| 21 | + /** 是否异步调用cancel */ | |
| 22 | + public boolean asyncCancel() default false; | |
| 19 | 23 | } | ... | ... |
tcc-transaction-api/target/classes/org/mengyun/tcctransaction/api/annotation/Compensable.class
No preview for this file type
tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/Transaction.java
| ... | ... | @@ -3,7 +3,7 @@ package org.mengyun.tcctransaction; |
| 3 | 3 | import org.mengyun.tcctransaction.api.TransactionContext; |
| 4 | 4 | import org.mengyun.tcctransaction.api.TransactionStatus; |
| 5 | 5 | import org.mengyun.tcctransaction.api.TransactionXid; |
| 6 | -import org.mengyun.tcctransaction.interceptor.Participant; | |
| 6 | +import org.mengyun.tcctransaction.interceptor.invoke.Participant; | |
| 7 | 7 | import org.slf4j.Logger; |
| 8 | 8 | import org.slf4j.LoggerFactory; |
| 9 | 9 | ... | ... |
tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/interceptor/CompensableTransactionInterceptor.java
| 1 | 1 | package org.mengyun.tcctransaction.interceptor; |
| 2 | 2 | |
| 3 | 3 | import org.aspectj.lang.ProceedingJoinPoint; |
| 4 | -import org.aspectj.lang.reflect.MethodSignature; | |
| 4 | +import org.mengyun.tcctransaction.TransactionManager; | |
| 5 | 5 | import org.mengyun.tcctransaction.api.TransactionContext; |
| 6 | 6 | import org.mengyun.tcctransaction.api.TransactionStatus; |
| 7 | 7 | import org.mengyun.tcctransaction.exception.NoExistedTransactionException; |
| 8 | 8 | import org.mengyun.tcctransaction.exception.OptimisticLockException; |
| 9 | -import org.mengyun.tcctransaction.interceptor.utils.CompensableMethodUtils; | |
| 10 | -import org.mengyun.tcctransaction.interceptor.utils.MethodType; | |
| 9 | +import org.mengyun.tcctransaction.interceptor.invoke.MethodContext; | |
| 10 | +import org.mengyun.tcctransaction.interceptor.invoke.MethodType; | |
| 11 | 11 | import org.mengyun.tcctransaction.interceptor.utils.ReflectionUtils; |
| 12 | 12 | import org.mengyun.tcctransaction.support.TransactionConfigurator; |
| 13 | 13 | import org.slf4j.Logger; |
| ... | ... | @@ -35,40 +35,39 @@ public class CompensableTransactionInterceptor { |
| 35 | 35 | * @throws Throwable |
| 36 | 36 | */ |
| 37 | 37 | public Object interceptCompensableMethod(ProceedingJoinPoint pjp) throws Throwable { |
| 38 | - | |
| 39 | - // 从拦截方法的参数中获取事务上下文 | |
| 40 | - TransactionContext transactionContext = CompensableMethodUtils.getTransactionContextFromArgs(pjp.getArgs()); | |
| 41 | - | |
| 42 | - // 计算可补偿事务方法类型 | |
| 43 | - MethodType methodType = CompensableMethodUtils.calculateMethodType(transactionContext, true); | |
| 38 | + // 创建方法上下文 | |
| 39 | + MethodContext compensableMethodContext = new MethodContext(pjp); | |
| 40 | + // 获取methodType | |
| 41 | + MethodType methodType = compensableMethodContext.getMethodType(); | |
| 44 | 42 | |
| 45 | 43 | LOG.debug("==>interceptCompensableMethod methodType:" + methodType.toString()); |
| 46 | 44 | |
| 47 | 45 | switch (methodType) { |
| 48 | 46 | case ROOT: |
| 49 | - return rootMethodProceed(pjp); // 主事务方法的处理 | |
| 47 | + return rootMethodProceed(compensableMethodContext); // 主事务方法的处理 | |
| 50 | 48 | case PROVIDER: |
| 51 | - return providerMethodProceed(pjp, transactionContext); // 服务提供者事务方法处理 | |
| 49 | + return providerMethodProceed(compensableMethodContext); // 服务提供者事务方法处理 | |
| 52 | 50 | default: |
| 53 | - return pjp.proceed(); // 其他的方法都是直接执行 | |
| 51 | + return compensableMethodContext.proceed(); // 其他的方法都是直接执行 | |
| 54 | 52 | } |
| 55 | 53 | } |
| 56 | 54 | |
| 57 | 55 | /** |
| 58 | 56 | * 主事务方法的处理. |
| 59 | - * @param pjp | |
| 57 | + * @param compensableMethodContext | |
| 60 | 58 | * @throws Throwable |
| 61 | 59 | */ |
| 62 | - private Object rootMethodProceed(ProceedingJoinPoint pjp) throws Throwable { | |
| 60 | + private Object rootMethodProceed(MethodContext compensableMethodContext) throws Throwable { | |
| 63 | 61 | LOG.debug("==>rootMethodProceed"); |
| 64 | 62 | |
| 65 | - transactionConfigurator.getTransactionManager().begin(); // 事务开始(创建事务日志记录,并在当前线程缓存该事务日志记录) | |
| 63 | + TransactionManager transactionManager = transactionConfigurator.getTransactionManager(); | |
| 64 | + transactionManager.begin(); // 事务开始(创建事务日志记录,并在当前线程缓存该事务日志记录) | |
| 66 | 65 | |
| 67 | 66 | Object returnValue = null; // 返回值 |
| 68 | 67 | try { |
| 69 | 68 | |
| 70 | 69 | LOG.debug("==>rootMethodProceed try begin"); |
| 71 | - returnValue = pjp.proceed(); // Try (开始执行被拦截的方法) | |
| 70 | + returnValue = compensableMethodContext.proceed(); // Try (开始执行被拦截的方法) | |
| 72 | 71 | LOG.debug("==>rootMethodProceed try end"); |
| 73 | 72 | |
| 74 | 73 | } catch (OptimisticLockException e) { |
| ... | ... | @@ -76,25 +75,25 @@ public class CompensableTransactionInterceptor { |
| 76 | 75 | throw e; //do not rollback, waiting for recovery job |
| 77 | 76 | } catch (Throwable tryingException) { |
| 78 | 77 | LOG.warn("compensable transaction trying failed.", tryingException); |
| 79 | - transactionConfigurator.getTransactionManager().rollback(); | |
| 78 | + transactionManager.rollback(); | |
| 80 | 79 | throw tryingException; |
| 81 | 80 | } |
| 82 | 81 | |
| 83 | 82 | LOG.info("===>rootMethodProceed begin commit()"); |
| 84 | - transactionConfigurator.getTransactionManager().commit(); // Try检验正常后提交(事务管理器在控制提交) | |
| 83 | + transactionManager.commit(); // Try检验正常后提交(事务管理器在控制提交) | |
| 85 | 84 | |
| 86 | 85 | return returnValue; |
| 87 | 86 | } |
| 88 | 87 | |
| 89 | 88 | /** |
| 90 | 89 | * 服务提供者事务方法处理. |
| 91 | - * @param pjp | |
| 92 | - * @param transactionContext | |
| 90 | + * @param compensableMethodContext | |
| 93 | 91 | * @throws Throwable |
| 94 | 92 | */ |
| 95 | - private Object providerMethodProceed(ProceedingJoinPoint pjp, TransactionContext transactionContext) throws Throwable { | |
| 93 | + private Object providerMethodProceed(MethodContext compensableMethodContext) throws Throwable { | |
| 94 | + TransactionContext transactionContext = compensableMethodContext.getTransactionContext(); | |
| 96 | 95 | |
| 97 | - LOG.debug("==>providerMethodProceed transactionStatus:" + TransactionStatus.valueOf(transactionContext.getStatus()).toString()); | |
| 96 | + LOG.debug("==>providerMethodProceed transactionStatus:{}", TransactionStatus.valueOf(transactionContext.getStatus()).toString()); | |
| 98 | 97 | |
| 99 | 98 | switch (TransactionStatus.valueOf(transactionContext.getStatus())) { |
| 100 | 99 | case TRYING: |
| ... | ... | @@ -102,7 +101,7 @@ public class CompensableTransactionInterceptor { |
| 102 | 101 | // 基于全局事务ID扩展创建新的分支事务,并存于当前线程的事务局部变量中. |
| 103 | 102 | transactionConfigurator.getTransactionManager().propagationNewBegin(transactionContext); |
| 104 | 103 | LOG.debug("==>providerMethodProceed try end"); |
| 105 | - return pjp.proceed(); | |
| 104 | + return compensableMethodContext.proceed(); | |
| 106 | 105 | case CONFIRMING: |
| 107 | 106 | try { |
| 108 | 107 | LOG.debug("==>providerMethodProceed confirm begin"); |
| ... | ... | @@ -126,8 +125,7 @@ public class CompensableTransactionInterceptor { |
| 126 | 125 | break; |
| 127 | 126 | } |
| 128 | 127 | |
| 129 | - Method method = ((MethodSignature) (pjp.getSignature())).getMethod(); | |
| 130 | - | |
| 128 | + Method method = compensableMethodContext.getMethod(); | |
| 131 | 129 | return ReflectionUtils.getNullValue(method.getReturnType()); |
| 132 | 130 | } |
| 133 | 131 | ... | ... |
tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/interceptor/ResourceCoordinatorInterceptor.java
| 1 | 1 | package org.mengyun.tcctransaction.interceptor; |
| 2 | 2 | |
| 3 | 3 | import org.aspectj.lang.ProceedingJoinPoint; |
| 4 | -import org.aspectj.lang.reflect.MethodSignature; | |
| 5 | 4 | import org.mengyun.tcctransaction.Transaction; |
| 6 | 5 | import org.mengyun.tcctransaction.api.TransactionContext; |
| 7 | 6 | import org.mengyun.tcctransaction.api.TransactionStatus; |
| 8 | 7 | import org.mengyun.tcctransaction.api.TransactionXid; |
| 9 | 8 | import org.mengyun.tcctransaction.api.UuidUtils; |
| 10 | -import org.mengyun.tcctransaction.api.annotation.Compensable; | |
| 11 | 9 | import org.mengyun.tcctransaction.interceptor.invoke.InvocationContext; |
| 10 | +import org.mengyun.tcctransaction.interceptor.invoke.MethodContext; | |
| 11 | +import org.mengyun.tcctransaction.interceptor.invoke.Participant; | |
| 12 | 12 | import org.mengyun.tcctransaction.interceptor.invoke.Terminator; |
| 13 | -import org.mengyun.tcctransaction.interceptor.utils.CompensableMethodUtils; | |
| 14 | -import org.mengyun.tcctransaction.interceptor.utils.MethodType; | |
| 13 | +import org.mengyun.tcctransaction.interceptor.invoke.MethodType; | |
| 15 | 14 | import org.mengyun.tcctransaction.interceptor.utils.ReflectionUtils; |
| 16 | 15 | import org.mengyun.tcctransaction.repository.TransactionRepository; |
| 17 | 16 | import org.mengyun.tcctransaction.support.TransactionConfigurator; |
| ... | ... | @@ -40,6 +39,9 @@ public class ResourceCoordinatorInterceptor { |
| 40 | 39 | * @throws Throwable |
| 41 | 40 | */ |
| 42 | 41 | public Object interceptTransactionContextMethod(ProceedingJoinPoint pjp) throws Throwable { |
| 42 | + // 创建方法上下文 | |
| 43 | + MethodContext methodContext = new MethodContext(pjp); | |
| 44 | + | |
| 43 | 45 | LOG.debug("==>ResourceCoordinatorInterceptor interceptTransactionContextMethod(ProceedingJoinPoint pjp)"); |
| 44 | 46 | // 获取当前事务 |
| 45 | 47 | Transaction transaction = transactionConfigurator.getTransactionManager().getCurrentTransaction(); |
| ... | ... | @@ -47,23 +49,19 @@ public class ResourceCoordinatorInterceptor { |
| 47 | 49 | // Trying(判断是否Try阶段的事务) |
| 48 | 50 | if (transaction != null && transaction.getStatus().equals(TransactionStatus.TRYING)) { |
| 49 | 51 | LOG.debug("==>TransactionStatus:" + transaction.getStatus().toString()); |
| 50 | - // 从参数获取事务上下文 | |
| 51 | - TransactionContext transactionContext = CompensableMethodUtils.getTransactionContextFromArgs(pjp.getArgs()); | |
| 52 | - // 获取事务补偿注解 | |
| 53 | - Compensable compensable = getCompensable(pjp); | |
| 54 | - // 计算方法类型 | |
| 55 | - MethodType methodType = CompensableMethodUtils.calculateMethodType(transactionContext, compensable != null ? true : false); | |
| 52 | + // 获取方法类型 | |
| 53 | + MethodType methodType = methodContext.getMethodType(); | |
| 56 | 54 | LOG.debug("==>methodType:" + methodType.toString()); |
| 57 | 55 | |
| 58 | 56 | switch (methodType) { |
| 59 | 57 | case ROOT: |
| 60 | - generateAndEnlistRootParticipant(pjp); // 生成和登记根参与者 | |
| 58 | + generateAndEnlistRootParticipant(methodContext); // 生成和登记根参与者 | |
| 61 | 59 | break; |
| 62 | 60 | case CONSUMER: |
| 63 | - generateAndEnlistConsumerParticipant(pjp); // 生成并登记消费者的参与者 | |
| 61 | + generateAndEnlistConsumerParticipant(methodContext); // 生成并登记消费者的参与者 | |
| 64 | 62 | break; |
| 65 | 63 | case PROVIDER: |
| 66 | - generateAndEnlistProviderParticipant(pjp); // 生成并登记服务提供者的参与者 | |
| 64 | + generateAndEnlistProviderParticipant(methodContext); // 生成并登记服务提供者的参与者 | |
| 67 | 65 | break; |
| 68 | 66 | } |
| 69 | 67 | } |
| ... | ... | @@ -74,24 +72,26 @@ public class ResourceCoordinatorInterceptor { |
| 74 | 72 | |
| 75 | 73 | /** |
| 76 | 74 | * 生成和登记根参与者. |
| 77 | - * @param pjp | |
| 75 | + * @param methodContext | |
| 78 | 76 | * @return |
| 79 | 77 | */ |
| 80 | - private Participant generateAndEnlistRootParticipant(ProceedingJoinPoint pjp) { | |
| 78 | + private Participant generateAndEnlistRootParticipant(MethodContext methodContext) { | |
| 81 | 79 | LOG.debug("==>ResourceCoordinatorInterceptor generateAndEnlistRootParticipant(ProceedingJoinPoint pjp)"); |
| 82 | - MethodSignature signature = (MethodSignature) pjp.getSignature(); | |
| 83 | - Method method = signature.getMethod(); | |
| 84 | - Compensable compensable = getCompensable(pjp); | |
| 85 | - String confirmMethodName = compensable.confirmMethod(); // 确认方法 | |
| 86 | - String cancelMethodName = compensable.cancelMethod(); // 取消方法 | |
| 80 | + String confirmMethodName = methodContext.getConfirmMethodName(); // 确认方法 | |
| 81 | + String cancelMethodName = methodContext.getCancelMethodName(); // 取消方法 | |
| 87 | 82 | |
| 88 | 83 | Transaction transaction = transactionConfigurator.getTransactionManager().getCurrentTransaction(); // 获取当前事务 |
| 89 | - | |
| 90 | 84 | TransactionXid xid = new TransactionXid(transaction.getXid().getGlobalTransactionId()); // 获取事务Xid |
| 91 | 85 | LOG.debug("==>TransactionXid:" + UuidUtils.byteArrayToUUID(xid.getGlobalTransactionId()).toString() |
| 92 | 86 | + "|" + UuidUtils.byteArrayToUUID(xid.getBranchQualifier()).toString()); |
| 93 | 87 | |
| 94 | - Class targetClass = ReflectionUtils.getDeclaringType(pjp.getTarget().getClass(), method.getName(), method.getParameterTypes()); | |
| 88 | + ProceedingJoinPoint pjp = methodContext.getProceedingJoinPoint(); | |
| 89 | + Method method = methodContext.getMethod(); | |
| 90 | + | |
| 91 | + Class targetClass = ReflectionUtils.getDeclaringType( | |
| 92 | + pjp.getTarget().getClass(), | |
| 93 | + method.getName(), | |
| 94 | + method.getParameterTypes()); | |
| 95 | 95 | |
| 96 | 96 | // 构建确认方法的提交上下文 |
| 97 | 97 | InvocationContext confirmInvocation = new InvocationContext(targetClass, |
| ... | ... | @@ -119,22 +119,22 @@ public class ResourceCoordinatorInterceptor { |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | 121 | * 生成并登记消费者的参与者 |
| 122 | - * @param pjp | |
| 122 | + * @param methodContext | |
| 123 | 123 | * @return |
| 124 | 124 | */ |
| 125 | - private Participant generateAndEnlistConsumerParticipant(ProceedingJoinPoint pjp) { | |
| 125 | + private Participant generateAndEnlistConsumerParticipant(MethodContext methodContext) { | |
| 126 | 126 | LOG.debug("==>ResourceCoordinatorInterceptor generateAndEnlistConsumerParticipant(ProceedingJoinPoint pjp)"); |
| 127 | - MethodSignature signature = (MethodSignature) pjp.getSignature(); | |
| 128 | - Method method = signature.getMethod(); | |
| 129 | 127 | |
| 130 | 128 | Transaction transaction = transactionConfigurator.getTransactionManager().getCurrentTransaction(); // 获取当前事务 |
| 131 | - | |
| 132 | 129 | TransactionXid xid = new TransactionXid(transaction.getXid().getGlobalTransactionId()); // 获取事务Xid |
| 133 | 130 | LOG.debug("==>TransactionXid:" + UuidUtils.byteArrayToUUID(xid.getGlobalTransactionId()).toString() |
| 134 | 131 | + "|" + UuidUtils.byteArrayToUUID(xid.getBranchQualifier()).toString()); |
| 135 | 132 | |
| 136 | 133 | // 获取事务上下文参数的位置 |
| 137 | - int position = CompensableMethodUtils.getTransactionContextParamPosition(((MethodSignature) pjp.getSignature()).getParameterTypes()); | |
| 134 | + int position = methodContext.getTransactionContextParamPosition(); | |
| 135 | + | |
| 136 | + ProceedingJoinPoint pjp = methodContext.getProceedingJoinPoint(); | |
| 137 | + Method method = methodContext.getMethod(); | |
| 138 | 138 | |
| 139 | 139 | // 给服务接口的TransactionContext参数设值 |
| 140 | 140 | pjp.getArgs()[position] = new TransactionContext(xid, transaction.getStatus().getId()); // 构建事务上下文 |
| ... | ... | @@ -173,25 +173,21 @@ public class ResourceCoordinatorInterceptor { |
| 173 | 173 | |
| 174 | 174 | /** |
| 175 | 175 | * 生成并登记服务提供者的参与者 |
| 176 | - * @param pjp | |
| 176 | + * @param methodContext | |
| 177 | 177 | * @return |
| 178 | 178 | */ |
| 179 | - private Participant generateAndEnlistProviderParticipant(ProceedingJoinPoint pjp) { | |
| 180 | - | |
| 181 | - MethodSignature signature = (MethodSignature) pjp.getSignature(); | |
| 182 | - Method method = signature.getMethod(); | |
| 183 | - | |
| 184 | - Compensable compensable = getCompensable(pjp); | |
| 185 | - | |
| 186 | - String confirmMethodName = compensable.confirmMethod(); | |
| 187 | - String cancelMethodName = compensable.cancelMethod(); | |
| 179 | + private Participant generateAndEnlistProviderParticipant(MethodContext methodContext) { | |
| 180 | + String confirmMethodName = methodContext.getConfirmMethodName(); | |
| 181 | + String cancelMethodName = methodContext.getCancelMethodName(); | |
| 188 | 182 | |
| 189 | 183 | Transaction transaction = transactionConfigurator.getTransactionManager().getCurrentTransaction(); |
| 190 | - | |
| 191 | 184 | TransactionXid xid = new TransactionXid(transaction.getXid().getGlobalTransactionId()); |
| 192 | 185 | LOG.debug("==>TransactionXid:" + UuidUtils.byteArrayToUUID(xid.getGlobalTransactionId()).toString() |
| 193 | 186 | + "|" + UuidUtils.byteArrayToUUID(xid.getBranchQualifier()).toString()); |
| 194 | 187 | |
| 188 | + ProceedingJoinPoint pjp = methodContext.getProceedingJoinPoint(); | |
| 189 | + Method method = methodContext.getMethod(); | |
| 190 | + | |
| 195 | 191 | Class targetClass = ReflectionUtils.getDeclaringType(pjp.getTarget().getClass(), method.getName(), method.getParameterTypes()); |
| 196 | 192 | |
| 197 | 193 | // 构建确认方法的提交上下文 |
| ... | ... | @@ -216,33 +212,4 @@ public class ResourceCoordinatorInterceptor { |
| 216 | 212 | return participant; |
| 217 | 213 | } |
| 218 | 214 | |
| 219 | - /** | |
| 220 | - * 根据切点,获取事务注解. | |
| 221 | - * @param pjp | |
| 222 | - * @return | |
| 223 | - */ | |
| 224 | - private Compensable getCompensable(ProceedingJoinPoint pjp) { | |
| 225 | - LOG.debug("==>ResourceCoordinatorInterceptor getCompensable(ProceedingJoinPoint pjp)"); | |
| 226 | - MethodSignature signature = (MethodSignature) pjp.getSignature(); // 获取签名 | |
| 227 | - Method method = signature.getMethod(); // 获取方法 | |
| 228 | - | |
| 229 | - Compensable compensable = method.getAnnotation(Compensable.class); // 获取注解 | |
| 230 | - | |
| 231 | - if (compensable == null) { | |
| 232 | - Method targetMethod = null; | |
| 233 | - try { | |
| 234 | - // 获取目标方法 | |
| 235 | - targetMethod = pjp.getTarget().getClass().getMethod(method.getName(), method.getParameterTypes()); | |
| 236 | - | |
| 237 | - if (targetMethod != null) { | |
| 238 | - compensable = targetMethod.getAnnotation(Compensable.class); | |
| 239 | - } | |
| 240 | - | |
| 241 | - } catch (NoSuchMethodException e) { | |
| 242 | - compensable = null; | |
| 243 | - } | |
| 244 | - | |
| 245 | - } | |
| 246 | - return compensable; | |
| 247 | - } | |
| 248 | 215 | } | ... | ... |
tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/interceptor/invoke/MethodContext.java
0 → 100644
| 1 | +package org.mengyun.tcctransaction.interceptor.invoke; | |
| 2 | + | |
| 3 | +import org.aspectj.lang.ProceedingJoinPoint; | |
| 4 | +import org.aspectj.lang.reflect.MethodSignature; | |
| 5 | +import org.mengyun.tcctransaction.api.TransactionContext; | |
| 6 | +import org.mengyun.tcctransaction.api.annotation.Compensable; | |
| 7 | + | |
| 8 | +import java.lang.reflect.Method; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * 标注的方法上下文。 | |
| 12 | + */ | |
| 13 | +public class MethodContext { | |
| 14 | + /** 切入点 */ | |
| 15 | + private ProceedingJoinPoint proceedingJoinPoint; | |
| 16 | + public ProceedingJoinPoint getProceedingJoinPoint() { | |
| 17 | + return proceedingJoinPoint; | |
| 18 | + } | |
| 19 | + /** 方法 */ | |
| 20 | + private Method method; | |
| 21 | + public Method getMethod() { | |
| 22 | + return method; | |
| 23 | + } | |
| 24 | + /** Compensable标注 */ | |
| 25 | + private Compensable compensable; | |
| 26 | + /** 事务上下文 */ | |
| 27 | + private TransactionContext transactionContext; | |
| 28 | + public TransactionContext getTransactionContext() { | |
| 29 | + return transactionContext; | |
| 30 | + } | |
| 31 | + | |
| 32 | + public MethodContext(ProceedingJoinPoint proceedingJoinPoint) { | |
| 33 | + // 切入点 | |
| 34 | + this.proceedingJoinPoint = proceedingJoinPoint; | |
| 35 | + | |
| 36 | + // 方法 | |
| 37 | + this.method = ((MethodSignature) (this.proceedingJoinPoint.getSignature())).getMethod(); | |
| 38 | + if (this.method.getAnnotation(Compensable.class) == null) { | |
| 39 | + try { | |
| 40 | + this.method = this.proceedingJoinPoint.getTarget().getClass().getMethod( | |
| 41 | + method.getName(), method.getParameterTypes()); | |
| 42 | + } catch (NoSuchMethodException e) { | |
| 43 | + this.method = null; | |
| 44 | + } | |
| 45 | + } | |
| 46 | + | |
| 47 | + // 根据切点,获取事务注解 | |
| 48 | + this.compensable = method.getAnnotation(Compensable.class); // 获取注解 | |
| 49 | + if (this.compensable == null) { | |
| 50 | + Method targetMethod = null; | |
| 51 | + try { | |
| 52 | + // 获取目标方法 | |
| 53 | + targetMethod = this.proceedingJoinPoint.getTarget().getClass().getMethod( | |
| 54 | + this.method.getName(), this.method.getParameterTypes()); | |
| 55 | + | |
| 56 | + if (targetMethod != null) { | |
| 57 | + this.compensable = targetMethod.getAnnotation(Compensable.class); | |
| 58 | + } | |
| 59 | + | |
| 60 | + } catch (NoSuchMethodException e) { | |
| 61 | + this.compensable = null; | |
| 62 | + } | |
| 63 | + | |
| 64 | + } | |
| 65 | + | |
| 66 | + // 从拦截方法的参数中获取事务上下文 | |
| 67 | + this.transactionContext = null; | |
| 68 | + for (Object arg : this.proceedingJoinPoint.getArgs()) { | |
| 69 | + if (arg != null && org.mengyun.tcctransaction.api.TransactionContext.class.isAssignableFrom(arg.getClass())) { | |
| 70 | + | |
| 71 | + this.transactionContext = (org.mengyun.tcctransaction.api.TransactionContext) arg; | |
| 72 | + } | |
| 73 | + } | |
| 74 | + | |
| 75 | + } | |
| 76 | + | |
| 77 | + // 计算方法类型 | |
| 78 | + public MethodType getMethodType() { | |
| 79 | + boolean isCompensable = this.compensable != null; | |
| 80 | + if (this.transactionContext == null && isCompensable) { | |
| 81 | + // 没有事务上下文信息,并且方法有事务注解的,为可补偿事务根方法(也就是事务发起者) | |
| 82 | + //isRootTransactionMethod | |
| 83 | + return MethodType.ROOT; | |
| 84 | + } else if (this.transactionContext == null && !isCompensable) { | |
| 85 | + // 没有事务上下文信息,并且方法没有事务注解的,为可补偿事务服务消费者(参考者)方法(一般为被调用的服务接口) | |
| 86 | + //isSoaConsumer | |
| 87 | + return MethodType.CONSUMER; | |
| 88 | + } else if (isCompensable) { | |
| 89 | + // 有事务上下文信息,并且方法有事务注解的,为可补偿事务服务提供者方法(一般为被调用的服务接口的实现方法) | |
| 90 | + //isSoaProvider | |
| 91 | + return MethodType.PROVIDER; | |
| 92 | + } else { | |
| 93 | + return MethodType.NORMAL; | |
| 94 | + } | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * 获取方法中,参数为TransactionContext类型的参数位置. | |
| 99 | + * @return | |
| 100 | + */ | |
| 101 | + public int getTransactionContextParamPosition() { | |
| 102 | + int i = -1; | |
| 103 | + Class<?>[] parameterTypes = this.method.getParameterTypes(); | |
| 104 | + for (i = 0; i < parameterTypes.length; i++) { | |
| 105 | + if (parameterTypes[i].equals(org.mengyun.tcctransaction.api.TransactionContext.class)) { | |
| 106 | + break; | |
| 107 | + } | |
| 108 | + } | |
| 109 | + return i; | |
| 110 | + } | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * 获取confirm方法名。 | |
| 114 | + * @return | |
| 115 | + */ | |
| 116 | + public String getConfirmMethodName() { | |
| 117 | + return this.compensable.confirmMethod(); | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * 获取cancel方法名。 | |
| 122 | + * @return | |
| 123 | + */ | |
| 124 | + public String getCancelMethodName() { | |
| 125 | + return this.compensable.cancelMethod(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * 执行方法。 | |
| 130 | + * @return | |
| 131 | + * @throws Throwable | |
| 132 | + */ | |
| 133 | + public Object proceed() throws Throwable { | |
| 134 | + return this.proceedingJoinPoint.proceed(); | |
| 135 | + } | |
| 136 | + | |
| 137 | + | |
| 138 | +} | ... | ... |
tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/interceptor/utils/MethodType.java renamed to tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/interceptor/invoke/MethodType.java
tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/interceptor/Participant.java renamed to tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/interceptor/invoke/Participant.java
| 1 | -package org.mengyun.tcctransaction.interceptor; | |
| 1 | +package org.mengyun.tcctransaction.interceptor.invoke; | |
| 2 | 2 | |
| 3 | 3 | import org.mengyun.tcctransaction.api.TransactionXid; |
| 4 | -import org.mengyun.tcctransaction.interceptor.invoke.Terminator; | |
| 5 | 4 | import org.slf4j.Logger; |
| 6 | 5 | import org.slf4j.LoggerFactory; |
| 7 | 6 | ... | ... |
tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/interceptor/utils/CompensableMethodUtils.java deleted
100644 → 0
| 1 | -package org.mengyun.tcctransaction.interceptor.utils; | |
| 2 | - | |
| 3 | -import org.mengyun.tcctransaction.api.TransactionContext; | |
| 4 | - | |
| 5 | -/** | |
| 6 | - * Created by changmingxie on 11/21/15. | |
| 7 | - * 可补偿方法工具类。 | |
| 8 | - */ | |
| 9 | -public class CompensableMethodUtils { | |
| 10 | - /** | |
| 11 | - * 计算方法类型. | |
| 12 | - * @param transactionContext | |
| 13 | - * @param isCompensable | |
| 14 | - * @return | |
| 15 | - */ | |
| 16 | - public static MethodType calculateMethodType(TransactionContext transactionContext, boolean isCompensable) { | |
| 17 | - | |
| 18 | - if (transactionContext == null && isCompensable) { | |
| 19 | - // 没有事务上下文信息,并且方法有事务注解的,为可补偿事务根方法(也就是事务发起者) | |
| 20 | - //isRootTransactionMethod | |
| 21 | - return MethodType.ROOT; | |
| 22 | - } else if (transactionContext == null && !isCompensable) { | |
| 23 | - // 没有事务上下文信息,并且方法没有事务注解的,为可补偿事务服务消费者(参考者)方法(一般为被调用的服务接口) | |
| 24 | - //isSoaConsumer | |
| 25 | - return MethodType.CONSUMER; | |
| 26 | - } else if (transactionContext != null && isCompensable) { | |
| 27 | - // 有事务上下文信息,并且方法有事务注解的,为可补偿事务服务提供者方法(一般为被调用的服务接口的实现方法) | |
| 28 | - //isSoaProvider | |
| 29 | - return MethodType.PROVIDER; | |
| 30 | - } else { | |
| 31 | - return MethodType.NORMAL; | |
| 32 | - } | |
| 33 | - } | |
| 34 | - | |
| 35 | - /** | |
| 36 | - * 获取事务上下文参数的位置. | |
| 37 | - * @param parameterTypes | |
| 38 | - * @return | |
| 39 | - */ | |
| 40 | - public static int getTransactionContextParamPosition(Class<?>[] parameterTypes) { | |
| 41 | - | |
| 42 | - int i = -1; | |
| 43 | - | |
| 44 | - for (i = 0; i < parameterTypes.length; i++) { | |
| 45 | - if (parameterTypes[i].equals(org.mengyun.tcctransaction.api.TransactionContext.class)) { | |
| 46 | - break; | |
| 47 | - } | |
| 48 | - } | |
| 49 | - return i; | |
| 50 | - } | |
| 51 | - | |
| 52 | - /** | |
| 53 | - * 从参数获取事务上下文. | |
| 54 | - * @param args | |
| 55 | - * @return | |
| 56 | - */ | |
| 57 | - public static TransactionContext getTransactionContextFromArgs(Object[] args) { | |
| 58 | - | |
| 59 | - TransactionContext transactionContext = null; | |
| 60 | - | |
| 61 | - for (Object arg : args) { | |
| 62 | - if (arg != null && org.mengyun.tcctransaction.api.TransactionContext.class.isAssignableFrom(arg.getClass())) { | |
| 63 | - | |
| 64 | - transactionContext = (org.mengyun.tcctransaction.api.TransactionContext) arg; | |
| 65 | - } | |
| 66 | - } | |
| 67 | - | |
| 68 | - return transactionContext; | |
| 69 | - } | |
| 70 | -} |
tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/recover/RecoverConfig.java
tcc-transaction-core/src/main/java/org/mengyun/tcctransaction/repository/impl/CachableTransactionRepository.java
| ... | ... | @@ -24,7 +24,7 @@ public abstract class CachableTransactionRepository implements TransactionReposi |
| 24 | 24 | private final static Logger LOG = LoggerFactory.getLogger(CachableTransactionRepository.class); |
| 25 | 25 | |
| 26 | 26 | /** 缓存到期时间(以秒为单位) */ |
| 27 | - private int expireDuration = 300; | |
| 27 | + private int expireDuration = 120; | |
| 28 | 28 | public final void setExpireDuration(int durationInSeconds) { |
| 29 | 29 | this.expireDuration = durationInSeconds; |
| 30 | 30 | } | ... | ... |
tcc-transaction-core/target/classes/org/mengyun/tcctransaction/Transaction.class
No preview for this file type
tcc-transaction-core/target/classes/org/mengyun/tcctransaction/interceptor/CompensableTransactionInterceptor$1.class
No preview for this file type
tcc-transaction-core/target/classes/org/mengyun/tcctransaction/interceptor/CompensableTransactionInterceptor.class
No preview for this file type
tcc-transaction-core/target/classes/org/mengyun/tcctransaction/interceptor/Participant.class deleted
100644 → 0
No preview for this file type
tcc-transaction-core/target/classes/org/mengyun/tcctransaction/interceptor/ResourceCoordinatorInterceptor$1.class
No preview for this file type
tcc-transaction-core/target/classes/org/mengyun/tcctransaction/interceptor/ResourceCoordinatorInterceptor.class
No preview for this file type
tcc-transaction-core/target/classes/org/mengyun/tcctransaction/interceptor/utils/CompensableMethodUtils.class deleted
100644 → 0
No preview for this file type
tcc-transaction-core/target/classes/org/mengyun/tcctransaction/interceptor/utils/MethodType.class deleted
100644 → 0
No preview for this file type
tcc-transaction-core/target/classes/org/mengyun/tcctransaction/repository/impl/CachableTransactionRepository.class
No preview for this file type
tcc-transaction-spring/src/main/java/org/mengyun/tcctransaction/spring/TccCompensableAspect.java
tcc-transaction-spring/src/main/java/org/mengyun/tcctransaction/spring/TccTransactionContextAspect.java
| ... | ... | @@ -21,13 +21,13 @@ public class TccTransactionContextAspect implements Ordered { |
| 21 | 21 | |
| 22 | 22 | /** 优先级顺序(值较低的那个有更高的优先级) */ |
| 23 | 23 | // 对于Around增强,方法前增强较低的优先级先执行,方法后的增强较高的优先级先执行 |
| 24 | - private int order = Ordered.HIGHEST_PRECEDENCE; | |
| 24 | + private int order = Ordered.HIGHEST_PRECEDENCE + 1; | |
| 25 | 25 | public void setOrder(int order) { |
| 26 | 26 | this.order = order; |
| 27 | 27 | } |
| 28 | 28 | @Override |
| 29 | 29 | public int getOrder() { |
| 30 | - return 0; | |
| 30 | + return order; | |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** 资源协调拦截器 */ | ... | ... |
tcc-transaction-spring/src/main/java/org/mengyun/tcctransaction/spring/support/TccTransactionConfigurator.java
| ... | ... | @@ -3,6 +3,7 @@ package org.mengyun.tcctransaction.spring.support; |
| 3 | 3 | import org.mengyun.tcctransaction.TransactionManager; |
| 4 | 4 | import org.mengyun.tcctransaction.recover.RecoverConfig; |
| 5 | 5 | import org.mengyun.tcctransaction.repository.TransactionRepository; |
| 6 | +import org.mengyun.tcctransaction.repository.impl.CachableTransactionRepository; | |
| 6 | 7 | import org.mengyun.tcctransaction.spring.recover.DefaultRecoverConfig; |
| 7 | 8 | import org.mengyun.tcctransaction.support.TransactionConfigurator; |
| 8 | 9 | import org.springframework.beans.factory.annotation.Autowired; |
| ... | ... | @@ -46,9 +47,15 @@ public class TccTransactionConfigurator implements TransactionConfigurator { |
| 46 | 47 | // 1、事务repo注入检测 |
| 47 | 48 | if (transactionRepository == null) { |
| 48 | 49 | throw new RuntimeException("自项目必须注入TransactionRepository相关实现类,如SpringJdbcTransactionRepository"); |
| 50 | + } else if (transactionRepository instanceof CachableTransactionRepository) { | |
| 51 | + ((CachableTransactionRepository) transactionRepository).setExpireDuration( | |
| 52 | + recoverConfig.getRecoverDuration() | |
| 53 | + ); | |
| 49 | 54 | } |
| 50 | 55 | // 2、事务配置器配置事务repo |
| 51 | 56 | transactionManager.setTransactionRepository(transactionRepository); |
| 57 | + | |
| 58 | + // TODO:3、executeService配置 | |
| 52 | 59 | } |
| 53 | 60 | |
| 54 | 61 | ... | ... |
tcc-transaction-spring/target/classes/org/mengyun/tcctransaction/spring/TccCompensableAspect.class
No preview for this file type
tcc-transaction-spring/target/classes/org/mengyun/tcctransaction/spring/TccTransactionContextAspect.class
No preview for this file type
tcc-transaction-spring/target/classes/org/mengyun/tcctransaction/spring/support/TccTransactionConfigurator.class
No preview for this file type