/**
 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.components.rpc;


import io.jboot.Jboot;

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

public abstract class JbootrpcBase implements Jbootrpc {

    protected static final Map<String, Object> objectCache = new ConcurrentHashMap<>();
    protected static JbootrpcConfig rpcConfig = Jboot.config(JbootrpcConfig.class);
    private boolean started = false;


    @Override
    public <T> T serviceObtain(Class<T> interfaceClass, JbootrpcReferenceConfig config) {
        String key = buildKey(interfaceClass, config);
        T object = (T) objectCache.get(key);
        if (object == null) {
            synchronized (this) {
                object = (T) objectCache.get(key);
                if (object == null) {
                    // onStart 方法是在 app 启动完成后，Jboot 主动去调用的
                    // 但是，在某些场景可能存在没有等 app 启动完成就去获取 Service 的情况
                    // 此时，需要主动先调用下 onStart 方法
                    invokeOnStartIfNecessary();

                    object = onServiceCreate(interfaceClass, config);
                    if (object != null) {
                        objectCache.put(key, object);
                    }
                }
            }
        }
        return object;
    }

    protected String buildKey(Class<?> interfaceClass, JbootrpcReferenceConfig config) {
        return interfaceClass.getName() + "@" + System.identityHashCode(config);
    }

    protected synchronized void invokeOnStartIfNecessary() {
        if (!started) {
            onStart();
            setStarted(true);
        }
    }


    public abstract <T> T onServiceCreate(Class<T> serviceClass, JbootrpcReferenceConfig config);

    @Override
    public void onStart() {

    }


    @Override
    public void onStop() {

    }

    public boolean isStarted() {
        return started;
    }

    public void setStarted(boolean started) {
        this.started = started;
    }
}

@@ -1968,78 +1968,211 @@
     
-return interfaceClass.getName() + %22@%22 + System.identityHashCode(config
+StringBuilder sb = new StringBuilder(interfaceClass.getName());%0A        return sb.append(%22:%22).append(config.getGroup())%0A                .append(%22:%22).append(config.getVersion())%0A                .toString(
 );%0A 

修复：rpc注入时，相同group和version的service创建过多重复对象，而motan协议为tcp长连接，导致系统tcp连接数剧增