use of io.seata.rm.tcc.api.TwoPhaseBusinessAction in project seata by seata.
the class TccActionInterceptor method invoke.
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
if (!RootContext.inGlobalTransaction() || disable || RootContext.inSagaBranch()) {
// not in transaction
return invocation.proceed();
}
Method method = getActionInterfaceMethod(invocation);
TwoPhaseBusinessAction businessAction = method.getAnnotation(TwoPhaseBusinessAction.class);
// try method
if (businessAction != null) {
// save the xid
String xid = RootContext.getXID();
// save the previous branchType
BranchType previousBranchType = RootContext.getBranchType();
// if not TCC, bind TCC branchType
if (BranchType.TCC != previousBranchType) {
RootContext.bindBranchType(BranchType.TCC);
}
try {
Object[] methodArgs = invocation.getArguments();
// Handler the TCC Aspect
Map<String, Object> ret = actionInterceptorHandler.proceed(method, methodArgs, xid, businessAction, invocation::proceed);
// return the final result
return ret.get(Constants.TCC_METHOD_RESULT);
} finally {
// if not TCC, unbind branchType
if (BranchType.TCC != previousBranchType) {
RootContext.unbindBranchType();
}
// MDC remove branchId
MDC.remove(RootContext.MDC_KEY_BRANCH_ID);
}
}
return invocation.proceed();
}
use of io.seata.rm.tcc.api.TwoPhaseBusinessAction in project seata by seata.
the class TCCBeanParserUtils method isTccProxyTargetBean.
/**
* is TCC proxy-bean/target-bean: LocalTCC , the proxy bean of sofa:reference/dubbo:reference
*
* @param remotingDesc the remoting desc
* @return boolean boolean
*/
public static boolean isTccProxyTargetBean(RemotingDesc remotingDesc) {
if (remotingDesc == null) {
return false;
}
// check if it is TCC bean
boolean isTccClazz = false;
Class<?> tccInterfaceClazz = remotingDesc.getInterfaceClass();
Method[] methods = tccInterfaceClazz.getMethods();
TwoPhaseBusinessAction twoPhaseBusinessAction;
for (Method method : methods) {
twoPhaseBusinessAction = method.getAnnotation(TwoPhaseBusinessAction.class);
if (twoPhaseBusinessAction != null) {
isTccClazz = true;
break;
}
}
if (!isTccClazz) {
return false;
}
short protocols = remotingDesc.getProtocol();
// LocalTCC
if (Protocols.IN_JVM == protocols) {
// in jvm TCC bean , AOP
return true;
}
// sofa:reference / dubbo:reference, AOP
return remotingDesc.isReference();
}
use of io.seata.rm.tcc.api.TwoPhaseBusinessAction in project seata by seata.
the class DefaultRemotingParser method parserRemotingServiceInfo.
/**
* parse the remoting bean info
*
* @param bean the bean
* @param beanName the bean name
* @param remotingParser the remoting parser
* @return remoting desc
*/
public RemotingDesc parserRemotingServiceInfo(Object bean, String beanName, RemotingParser remotingParser) {
RemotingDesc remotingBeanDesc = remotingParser.getServiceDesc(bean, beanName);
if (remotingBeanDesc == null) {
return null;
}
remotingServiceMap.put(beanName, remotingBeanDesc);
Class<?> interfaceClass = remotingBeanDesc.getInterfaceClass();
Method[] methods = interfaceClass.getMethods();
if (remotingParser.isService(bean, beanName)) {
try {
// service bean, registry resource
Object targetBean = remotingBeanDesc.getTargetBean();
for (Method m : methods) {
TwoPhaseBusinessAction twoPhaseBusinessAction = m.getAnnotation(TwoPhaseBusinessAction.class);
if (twoPhaseBusinessAction != null) {
TCCResource tccResource = new TCCResource();
tccResource.setActionName(twoPhaseBusinessAction.name());
tccResource.setTargetBean(targetBean);
tccResource.setPrepareMethod(m);
tccResource.setCommitMethodName(twoPhaseBusinessAction.commitMethod());
tccResource.setCommitMethod(ReflectionUtil.getMethod(interfaceClass, twoPhaseBusinessAction.commitMethod(), new Class[] { BusinessActionContext.class }));
tccResource.setRollbackMethodName(twoPhaseBusinessAction.rollbackMethod());
tccResource.setRollbackMethod(ReflectionUtil.getMethod(interfaceClass, twoPhaseBusinessAction.rollbackMethod(), new Class[] { BusinessActionContext.class }));
// registry tcc resource
DefaultResourceManager.get().registerResource(tccResource);
}
}
} catch (Throwable t) {
throw new FrameworkException(t, "parser remoting service error");
}
}
if (remotingParser.isReference(bean, beanName)) {
// reference bean, TCC proxy
remotingBeanDesc.setReference(true);
}
return remotingBeanDesc;
}
use of io.seata.rm.tcc.api.TwoPhaseBusinessAction in project framework by wcnnkh.
the class TccActionInterceptor method intercept.
@Override
public Object intercept(MethodInvoker invoker, Object[] args) throws Throwable {
if (!RootContext.inGlobalTransaction() || disable || RootContext.inSagaBranch()) {
// not in transaction
return invoker.invoke(args);
}
Method method = getActionInterfaceMethod(invoker);
TwoPhaseBusinessAction businessAction = method.getAnnotation(TwoPhaseBusinessAction.class);
// try method
if (businessAction != null) {
// save the xid
String xid = RootContext.getXID();
// save the previous branchType
BranchType previousBranchType = RootContext.getBranchType();
// if not TCC, bind TCC branchType
if (BranchType.TCC != previousBranchType) {
RootContext.bindBranchType(BranchType.TCC);
}
try {
// Handler the TCC Aspect
Map<String, Object> ret = actionInterceptorHandler.proceed(method, args, xid, businessAction, () -> {
return invoker.invoke(args);
});
// return the final result
return ret.get(Constants.TCC_METHOD_RESULT);
} finally {
// if not TCC, unbind branchType
if (BranchType.TCC != previousBranchType) {
RootContext.unbindBranchType();
}
}
}
return invoker.invoke(args);
}
use of io.seata.rm.tcc.api.TwoPhaseBusinessAction in project jboot by yangfuhai.
the class TccActionInterceptor method intercept.
@Override
public void intercept(Invocation inv) {
if (!JbootSeataManager.me().isEnable()) {
inv.invoke();
return;
}
if (!RootContext.inGlobalTransaction()) {
// not in transaction
inv.invoke();
return;
}
Method method = inv.getMethod();
TwoPhaseBusinessAction businessAction = method.getAnnotation(TwoPhaseBusinessAction.class);
// try method
if (businessAction != null) {
// save the xid
String xid = RootContext.getXID();
// clear the context
BranchType previousBranchType = RootContext.getBranchType();
if (BranchType.TCC != previousBranchType) {
RootContext.bindBranchType(BranchType.TCC);
}
try {
Object[] methodArgs = inv.getArgs();
// Handler the TCC Aspect
actionInterceptorHandler.proceed(method, methodArgs, xid, businessAction, inv);
} finally {
// if not TCC, unbind branchType
if (BranchType.TCC != previousBranchType) {
RootContext.unbindBranchType();
}
}
} else {
inv.invoke();
}
}
Aggregations