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

org.bdware.doip.cluster.callback.JoinCountCallback Maven / Gradle / Ivy

The newest version!
package org.bdware.doip.cluster.callback;

import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.cluster.client.DoaClusterClient;
import org.bdware.doip.cluster.client.TraceableDoaClusterClient;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.endpoint.client.DoipMessageCallback;
import org.bdware.sc.bean.ForkInfo;
import org.bdware.sc.bean.JoinInfo;
import wrp.jdk.nashorn.api.scripting.NashornScriptEngineUtil;

import java.util.Map;

public class JoinCountCallback extends CookieJSResultCallback {
    static Logger LOGGER = LogManager.getLogger(JoinCountCallback.class);
    private DoaClusterClient doaClusterClient;
    private NashornScriptEngineUtil engineUtil;
    private DoipMessage msg;
    private JoinInfo joinInfo;

    private ForkInfo forkInfo;
    private String[] doidList;
    private DoipMessageCallback cb;
    private JsonObject appendixes;
    private int serversNum;

    public JoinCountCallback(Map cookie, DoaClusterClient doaClusterClient, NashornScriptEngineUtil engineUtil, DoipMessage msg, JoinInfo joinInfo, ForkInfo forkInfo, String[] doidList, DoipMessageCallback cb, JsonObject appendixes) {
        super(cookie);
        this.doaClusterClient = doaClusterClient;
        this.engineUtil = engineUtil;
        this.msg = msg;
        this.joinInfo = joinInfo;
        this.forkInfo = forkInfo;
        this.doidList = doidList;
        this.cb = cb;
        this.appendixes = appendixes;
        this.serversNum = doidList.length;
    }

    //BDO--> bdtest/repo1/711231
    //版本1{  ... address: tcp://ip:port1; ..} 不会去看doipmessage中的header.id字段。
    //版本2{ ... repoId:bdtest/repo1..., appendix:{..:{address: tcp://ip:port2}}}
    //------------
    // id->N123,234  repoHandler-->根据id->N123,234  去第三方系统调用查询接口。
    // 第一版: 全局只有1个repo,因此,标识解析系统的解析结果{repoid, address, version}
    // 第二版: 全局有多个repo。
    //---> 约定 retrieve N123,234 应该是个DDO呀。是去全局所有仓库的retrieve。
    //     1) DDO repoId bdtest/repoall -> {func/strategy}

    // DDO -->标识解析系统里注册了routeJoin等规则,它会将一组BDO/DDO作为下一跳的节点去访问。
    // BDO--> 解析之后,1)有address, ip:port -->直接访问; 2)有repoId,会拿repoId去定位ip:port
    // 普通的DO --> 解析之后,有repoId,repoId会对应ip:port,可以去访问。
    @Override
    public void onResult(Integer count) {
        if (count > serversNum) count = serversNum;
        if (count > 0) {
            DDOClusterDoipInvocationCallback ddoClusterDoipMessageCallback = new DDOClusterDoipInvocationCallback(cookie, doaClusterClient, doidList, msg, cb, count, engineUtil, joinInfo, appendixes);
            if (forkInfo == null || forkInfo.funcName == null) {
                //执行默认的fork方法
                for (String doid : doidList) {
                    DoipMessage toCallDoipMessage;
                    if (doaClusterClient instanceof TraceableDoaClusterClient) {
                        toCallDoipMessage = ((TraceableDoaClusterClient) doaClusterClient).wrapSpanWithParent(msg.clone(), msg, "JoinCount");
                    } else{
                        toCallDoipMessage = msg.clone();
                    }

                    DOIPInvocationCallback doipInvocationCallback = new DOIPInvocationCallback(doid, toCallDoipMessage, ddoClusterDoipMessageCallback);
                    this.doaClusterClient.sendMessage(doid, toCallDoipMessage, doipInvocationCallback, false);
                }
            } else {
                //执行fork方法
                ForkCallBack forkCallBack = new ForkCallBack(cookie, this.doaClusterClient, msg.clone(), doidList, ddoClusterDoipMessageCallback);
                try {
                    //调用用fork函数
                    engineUtil.invokeFunctionWithObjectAsync(forkInfo.funcName, forkCallBack, msg, doidList);
                } catch (Exception e) {
                    LOGGER.error("Errors occur when executing JavaScript Fork functions!");
                }
            }
        } else {
            LOGGER.error("joinCount is not a positive num");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy