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

com.codingapi.tx.aop.bean.TxTransactionLocal Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package com.codingapi.tx.aop.bean;

import com.alibaba.fastjson.JSONObject;
import com.codingapi.tx.framework.utils.SocketManager;
import com.codingapi.tx.model.Request;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 分布式事务远程调用控制对象
 * Created by lorne on 2017/6/5.
 */
public class TxTransactionLocal {

    private Logger logger = LoggerFactory.getLogger(TxTransactionLocal.class);

    private final static ThreadLocal currentLocal = new ThreadLocal();

    private String groupId;

    private int maxTimeOut;

    private Map cacheModelInfo = new ConcurrentHashMap<>();

    /**
     * 是否同一个模块被多次请求
     */
    private boolean hasIsGroup = false;

    /**
     * 是否是发起方模块
     */
    private boolean hasStart = false;

    /**
     * 时候已经获取到连接对象
     */
    private boolean hasConnection = false;


    private String kid;

    private String type;

    private boolean readOnly = false;

    public boolean isHasIsGroup() {
        return hasIsGroup;
    }

    public void setHasIsGroup(boolean hasIsGroup) {
        this.hasIsGroup = hasIsGroup;
    }

    public String getKid() {
        return kid;
    }

    public void setKid(String kid) {
        this.kid = kid;
    }

    public boolean isHasStart() {
        return hasStart;
    }

    public void setHasStart(boolean hasStart) {
        this.hasStart = hasStart;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public boolean isHasConnection() {
        return hasConnection;
    }

    public void setHasConnection(boolean hasConnection) {
        this.hasConnection = hasConnection;
    }


    public int getMaxTimeOut() {
        return maxTimeOut;
    }

    public void setMaxTimeOut(int maxTimeOut) {
        this.maxTimeOut = maxTimeOut;
    }


    public static TxTransactionLocal current() {
        return currentLocal.get();
    }

    public static void setCurrent(TxTransactionLocal current) {
        currentLocal.set(current);
    }


    public void putLoadBalance(String key, String data){
        cacheModelInfo.put(key,data);
        //与TxManager通讯
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("g", getGroupId());
        jsonObject.put("k", key);
        jsonObject.put("d", data);
        logger.info("putLoadBalance--> start ");
        Request request = new Request("plb", jsonObject.toString());
        String json =  SocketManager.getInstance().sendMsg(request);
        logger.info("putLoadBalance--> end ,res ->"+json);
    }


    public String getLoadBalance(String key){
        String old =  cacheModelInfo.get(key);
        logger.info("cacheModelInfo->"+old);
        if(old==null){
            //与TxManager通讯
            logger.info("getLoadBalance--> start");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("g", getGroupId());
            jsonObject.put("k", key);
            Request request = new Request("glb", jsonObject.toString());
            String json =  SocketManager.getInstance().sendMsg(request);
            logger.info("getLoadBalance--> end ,res - >" + json);
            if(StringUtils.isNotEmpty(json)){
                return json;
            }
        }
        return old;
    }


    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy