Optimize ThreadContext.java

This commit is contained in:
lancer.hong
2021-08-30 19:03:05 +08:00
parent 5746732bf8
commit d1543f48c3
4 changed files with 14 additions and 16 deletions

View File

@@ -96,9 +96,7 @@ public abstract class ThreadContext {
try {
t = clz.newInstance();
set(key, t);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@@ -113,14 +111,14 @@ public abstract class ThreadContext {
return getMap().remove(key);
}
public static <T> ArrayList<T> getArrayList(String key, Class<T> elementType) {
return getArrayList(key, elementType, true);
public static <T> ArrayList<T> getArrayList(String key) {
return getArrayList(key, true);
}
public static <T> ArrayList<T> getArrayList(String key, Class<T> elementType, boolean clear) {
ArrayList<T> l = (ArrayList) get(key);
public static <T> ArrayList<T> getArrayList(String key, boolean clear) {
ArrayList<T> l = (ArrayList<T>) get(key);
if (l == null) {
l = new ArrayList<>();
l = new ArrayList<T>();
set(key, l);
} else if (clear) {
l.clear();
@@ -128,14 +126,14 @@ public abstract class ThreadContext {
return l;
}
public static <K, V> HashMap<K, V> getHashMap(String key, Class<K> kType, Class<V> vType) {
return getHashMap(key, kType, vType, true);
public static <K, V> HashMap<K, V> getHashMap(String key) {
return getHashMap(key, true);
}
public static <K, V> HashMap<K, V> getHashMap(String key, Class<K> kType, Class<V> vType, boolean clear) {
public static <K, V> HashMap<K, V> getHashMap(String key, boolean clear) {
HashMap<K, V> m = (HashMap<K, V>) get(key);
if (m == null) {
m = new HashMap<>();
m = new HashMap<K, V>();
set(key ,m);
} else if (clear) {
m.clear();

View File

@@ -135,7 +135,7 @@ public class CallbackFilter extends FizzWebFilter {
}
private HashMap<String, ServiceInstance> getService2instMap(ApiConfig ac) {
HashMap<String, ServiceInstance> service2instMap = ThreadContext.getHashMap(s2im, String.class, ServiceInstance.class);
HashMap<String, ServiceInstance> service2instMap = ThreadContext.getHashMap(s2im);
List<Receiver> receivers = ac.callbackConfig.receivers;
for (Receiver r : receivers) {
if (r.type == ApiConfig.Type.SERVICE_DISCOVERY) {

View File

@@ -249,7 +249,7 @@ public class ApiConfigService {
if (sc != null) {
List<ApiConfig> apiConfigs = sc.getApiConfigs(method, path, gatewayGroup);
if (!apiConfigs.isEmpty()) {
List<String> matchPathPatterns = ThreadContext.getArrayList(mpps, String.class);
List<String> matchPathPatterns = ThreadContext.getArrayList(mpps);
for (int i = 0; i < apiConfigs.size(); i++) {
ApiConfig ac = apiConfigs.get(i);
if (ac.checkApp) {

View File

@@ -117,7 +117,7 @@ public class ServiceConfig {
@JsonIgnore
public List<ApiConfig> getApiConfigs(HttpMethod method, String path, String gatewayGroup) {
List<GatewayGroup2apiConfig> matchGatewayGroup2apiConfigs = ThreadContext.getArrayList(gg2acs, GatewayGroup2apiConfig.class);
List<GatewayGroup2apiConfig> matchGatewayGroup2apiConfigs = ThreadContext.getArrayList(gg2acs);
Set<Map.Entry<String, Map<Object, GatewayGroup2apiConfig>>> es = path2methodToApiConfigMapMap.entrySet();
for (Map.Entry<String, Map<Object, GatewayGroup2apiConfig>> e : es) {
@@ -142,7 +142,7 @@ public class ServiceConfig {
ThreadContext.set(ApiConfigService.AUTH_MSG, id + " no route match " + method + ' ' + path);
return Collections.emptyList();
} else {
List<ApiConfig> lst = ThreadContext.getArrayList(acs, ApiConfig.class);
List<ApiConfig> lst = ThreadContext.getArrayList(acs);
for (int i = 0; i < matchGatewayGroup2apiConfigs.size(); i++) {
GatewayGroup2apiConfig gatewayGroup2apiConfig = matchGatewayGroup2apiConfigs.get(i);
Set<ApiConfig> apiConfigs = gatewayGroup2apiConfig.get(gatewayGroup);