All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.openservices.ons.api.exactlyonce.aop.proxy.MQTransactionScaner Maven / Gradle / Ivy

There is a newer version: 1.9.4.Final
Show newest version
package com.aliyun.openservices.ons.api.exactlyonce.aop.proxy;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;

import com.aliyun.openservices.ons.api.exactlyonce.aop.annotation.MQTransaction;
import com.aliyun.openservices.ons.api.exactlyonce.manager.util.AopUtil;
import org.springframework.aop.Advisor;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextRefreshedEvent;

import static com.aliyun.openservices.ons.api.exactlyonce.manager.util.AopUtil.findTargetClass;
import static com.aliyun.openservices.ons.api.exactlyonce.manager.util.AopUtil.getAdvisedSupport;

/**
 * @author gongshi
 */
public class MQTransactionScaner extends AbstractMQTransactionScanner {
    private boolean enableTransaction = true;
    private MQTransactionInterceptor interceptor;
    private HashSet proxyMethodSet = new HashSet();
    private boolean isEnableTransaction = true;

    private void init() {
//        TransactionManager.start();
    }

    @Override
    protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
        synchronized (proxyMethodSet) {
            if (proxyMethodSet.contains(beanName)) {
                return bean;
            }
            proxyMethodSet.add(beanName);
        }
        try {
            Class targetInterface = findTargetClass(bean);
            Method[] methods = targetInterface.getMethods();
            HashMap methodMap = new HashMap();
            for (Method method : methods) {
                MQTransaction mqTransaction = method.getAnnotation(MQTransaction.class);
                if (mqTransaction != null && isEnableTransaction) {
                    methodMap.put(AopUtil.formatMethod(method), mqTransaction);
                }
            }
            if (!methodMap.isEmpty()) {
                interceptor = new MQTransactionInterceptor(methodMap);
            } else {
                return bean;
            }
            if (!AopUtils.isAopProxy(bean)) {
                bean = super.wrapIfNecessary(bean, beanName, cacheKey);
            } else {
                // add to current proxy
                AdvisedSupport advised = getAdvisedSupport(bean);
                Advisor[] advisor = buildAdvisors(beanName, getAdvicesAndAdvisorsForBean(null, null, null));
                for (Advisor avr : advisor) {
                    advised.addAdvisor(0, avr);
                }
            }
            // proxyed object
            return bean;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected Object[] getAdvicesAndAdvisorsForBean(Class aClass, String s, TargetSource targetSource) throws BeansException {
        return new Object[] {interceptor};
    }

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if (applicationEvent != null && applicationEvent instanceof ContextRefreshedEvent) {

        }
        if (enableTransaction) {
            init();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy