com.qq.tars.client.Communicator Maven / Gradle / Ivy
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 com.qq.tars.client;
import com.qq.tars.client.support.ClientPoolManager;
import com.qq.tars.client.util.ClientLogger;
import com.qq.tars.common.support.ScheduledExecutorManager;
import com.qq.tars.common.util.StringUtils;
import com.qq.tars.net.client.ticket.TicketManager;
import com.qq.tars.rpc.common.LoadBalance;
import com.qq.tars.rpc.common.ProtocolInvoker;
import com.qq.tars.rpc.exc.CommunicatorConfigException;
import com.qq.tars.support.query.QueryHelper;
import com.qq.tars.support.query.prx.EndpointF;
import com.qq.tars.support.stat.StatHelper;
import java.lang.reflect.Proxy;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
public final class Communicator {
private volatile String id;
private volatile CommunicatorConfig communicatorConfig;
private volatile ThreadPoolExecutor threadPoolExecutor;
private final ServantProxyFactory servantProxyFactory = new ServantProxyFactory(this);
private final ObjectProxyFactory objectProxyFactory = new ObjectProxyFactory(this);
private final QueryHelper queryHelper = new QueryHelper(this);
private final StatHelper statHelper = new StatHelper(this);
private final ReentrantLock lock = new ReentrantLock();
private final AtomicBoolean inited = new AtomicBoolean(false);
Communicator(CommunicatorConfig config) {
if (config != null) {
this.initCommunicator(config);
}
}
public T stringToProxy(Class clazz, String objName) throws CommunicatorConfigException {
return stringToProxy(clazz, objName, null, null, null, null);
}
public T stringToProxy(Class clazz, String objName, String setDivision) throws CommunicatorConfigException {
return stringToProxy(clazz, objName, setDivision, null, null, null);
}
public T stringToProxy(Class clazz, ServantProxyConfig servantProxyConfig) throws CommunicatorConfigException {
return stringToProxy(clazz, servantProxyConfig.getObjectName(), servantProxyConfig.getSetDivision(), servantProxyConfig, null, null);
}
@Deprecated
public T stringToProxy(Class clazz, ServantProxyConfig servantProxyConfig, LoadBalance loadBalance) throws CommunicatorConfigException {
return stringToProxy(clazz, servantProxyConfig.getObjectName(), servantProxyConfig.getSetDivision(), servantProxyConfig, loadBalance, null);
}
@SuppressWarnings("unchecked")
private T stringToProxy(Class clazz, String objName, String setDivision, ServantProxyConfig servantProxyConfig,
LoadBalance loadBalance, ProtocolInvoker protocolInvoker) throws CommunicatorConfigException {
if (!inited.get()) {
throw new CommunicatorConfigException("communicator uninitialized!");
}
return (T) getServantProxyFactory().getServantProxy(clazz, objName, setDivision, servantProxyConfig, loadBalance, protocolInvoker);
}
@Deprecated
public void initialize(CommunicatorConfig config) throws CommunicatorConfigException {
this.initCommunicator(config);
}
public void shutdown() {
this.threadPoolExecutor.shutdownNow();
ScheduledExecutorManager.getInstance().shutdownNow();
TicketManager.shutdown();
for (Iterator