Optimize code design

This commit is contained in:
得少 2019-03-05 10:08:28 +08:00
parent a05be5f97b
commit a9ff49692f
3 changed files with 28 additions and 22 deletions

View File

@ -20,11 +20,7 @@ import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent; import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.utils.IOUtils; import com.alibaba.nacos.client.config.utils.IOUtils;
import com.alibaba.nacos.client.utils.TemplateUtils; import com.alibaba.nacos.client.utils.*;
import com.alibaba.nacos.client.utils.EnvUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.StringUtils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -171,7 +167,7 @@ public class ServerListManager {
private String initEndpoint(String endpointTmp) { private String initEndpoint(String endpointTmp) {
String endpointPortTmp = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT); String endpointPortTmp = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT);
if (StringUtils.isNotEmpty(endpointPortTmp)) { if (StringUtils.isNotBlank(endpointPortTmp)) {
endpointPort = Integer.parseInt(endpointPortTmp); endpointPort = Integer.parseInt(endpointPortTmp);
} }

View File

@ -97,11 +97,11 @@ public class NacosNamingService implements NamingService {
eventDispatcher = new EventDispatcher(); eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList); serverProxy = new NamingProxy(namespace, endpoint, serverList);
serverProxy.setProperties(properties); serverProxy.setProperties(properties);
beatReactor = new BeatReactor(serverProxy, clientBeatThreadCount(properties)); beatReactor = new BeatReactor(serverProxy, initClientBeatThreadCount(properties));
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), pollingThreadCount(properties)); hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties));
} }
private int clientBeatThreadCount(Properties properties) { private int initClientBeatThreadCount(Properties properties) {
if (properties == null) { if (properties == null) {
return UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT; return UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT;
@ -113,7 +113,7 @@ public class NacosNamingService implements NamingService {
return clientBeatThreadCount; return clientBeatThreadCount;
} }
private int pollingThreadCount(Properties properties) { private int initPollingThreadCount(Properties properties) {
if (properties == null) { if (properties == null) {
return UtilAndComs.DEFAULT_POLLING_THREAD_COUNT; return UtilAndComs.DEFAULT_POLLING_THREAD_COUNT;
@ -234,19 +234,17 @@ public class NacosNamingService implements NamingService {
private void initWebRootContext() { private void initWebRootContext() {
// support the web context with ali-yun if the app deploy by EDAS // support the web context with ali-yun if the app deploy by EDAS
String webContext = System.getProperty(SystemPropertyKeyConst.NAMING_WEB_CONTEXT); final String webContext = System.getProperty(SystemPropertyKeyConst.NAMING_WEB_CONTEXT);
boolean isSetting = false; TemplateUtils.stringNotEmptyAndThenExecute(webContext, new Runnable() {
if (StringUtils.isNotEmpty(webContext)) { @Override
public void run() {
UtilAndComs.WEB_CONTEXT = webContext.indexOf("/") > -1 ? webContext UtilAndComs.WEB_CONTEXT = webContext.indexOf("/") > -1 ? webContext
: "/" + webContext; : "/" + webContext;
isSetting = true;
}
if (isSetting) {
UtilAndComs.NACOS_URL_BASE = UtilAndComs.WEB_CONTEXT + "/v1/ns"; UtilAndComs.NACOS_URL_BASE = UtilAndComs.WEB_CONTEXT + "/v1/ns";
UtilAndComs.NACOS_URL_INSTANCE = UtilAndComs.NACOS_URL_BASE + "/instance"; UtilAndComs.NACOS_URL_INSTANCE = UtilAndComs.NACOS_URL_BASE + "/instance";
} }
});
} }
@Override @Override

View File

@ -23,6 +23,18 @@ import java.util.concurrent.Callable;
*/ */
public class TemplateUtils { public class TemplateUtils {
public static final void stringNotEmptyAndThenExecute(String source, Runnable runnable) {
if (StringUtils.isNotEmpty(source)) {
try {
runnable.run();
} catch (Exception e) {
LogUtils.NAMING_LOGGER.error("string empty and then execute cause an exception.", e);
}
}
}
public static final String stringEmptyAndThenExecute(String source, Callable<String> callable) { public static final String stringEmptyAndThenExecute(String source, Callable<String> callable) {
if (StringUtils.isEmpty(source)) { if (StringUtils.isEmpty(source)) {