Search in sources :

Example 1 with TwoPhaseBusinessAction

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();
}
Also used : BranchType(io.seata.core.model.BranchType) TwoPhaseBusinessAction(io.seata.rm.tcc.api.TwoPhaseBusinessAction) Method(java.lang.reflect.Method)

Example 2 with TwoPhaseBusinessAction

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();
}
Also used : TwoPhaseBusinessAction(io.seata.rm.tcc.api.TwoPhaseBusinessAction) Method(java.lang.reflect.Method)

Example 3 with TwoPhaseBusinessAction

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;
}
Also used : TCCResource(io.seata.rm.tcc.TCCResource) FrameworkException(io.seata.common.exception.FrameworkException) TwoPhaseBusinessAction(io.seata.rm.tcc.api.TwoPhaseBusinessAction) Method(java.lang.reflect.Method) RemotingDesc(io.seata.rm.tcc.remoting.RemotingDesc) BusinessActionContext(io.seata.rm.tcc.api.BusinessActionContext)

Example 4 with TwoPhaseBusinessAction

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);
}
Also used : BranchType(io.seata.core.model.BranchType) TwoPhaseBusinessAction(io.seata.rm.tcc.api.TwoPhaseBusinessAction) Method(java.lang.reflect.Method)

Example 5 with TwoPhaseBusinessAction

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();
    }
}
Also used : BranchType(io.seata.core.model.BranchType) TwoPhaseBusinessAction(io.seata.rm.tcc.api.TwoPhaseBusinessAction) Method(java.lang.reflect.Method)

Aggregations

TwoPhaseBusinessAction (io.seata.rm.tcc.api.TwoPhaseBusinessAction)5 Method (java.lang.reflect.Method)5 BranchType (io.seata.core.model.BranchType)3 FrameworkException (io.seata.common.exception.FrameworkException)1 TCCResource (io.seata.rm.tcc.TCCResource)1 BusinessActionContext (io.seata.rm.tcc.api.BusinessActionContext)1 RemotingDesc (io.seata.rm.tcc.remoting.RemotingDesc)1