1616package io .opensergo ;
1717
1818import java .util .concurrent .ConcurrentHashMap ;
19+ import java .util .concurrent .ConcurrentMap ;
1920
2021/**
21- * .
22- *
2322 * @author Jiangnan Jia
24- ** /
23+ */
2524public class OpenSergoClientManager {
2625
2726 private static volatile OpenSergoClientManager instance ;
2827
2928 /**
3029 * Cached all the shared OpenSergoClients.
3130 */
32- private ConcurrentHashMap <String , OpenSergoClient > sharedOpenSergoClientCache = new ConcurrentHashMap <>();
33-
31+ private final ConcurrentMap <String , OpenSergoClient > sharedClientCache = new ConcurrentHashMap <>();
3432
35- private OpenSergoClientManager () {
36-
37- }
33+ private OpenSergoClientManager () {}
3834
3935 /**
40- * get OpenSergoClientManager by DCL (Double Check Lock)
41- * @return
36+ * Get OpenSergoClientManager by DCL (Double Check Lock).
37+ *
38+ * @return the OpenSergoClientManager singleton
4239 */
4340 public static OpenSergoClientManager get () {
4441 if (instance == null ) {
@@ -56,38 +53,42 @@ private String buildSharedCacheKey(String host, int port) {
5653 }
5754
5855 /**
59- * get the instance from sharedOpenSergoClientCache,
60- * if there is no one, will create a new instance and return it.
56+ * Get the instance from sharedOpenSergoClientCache.
57+ * If there is no one, the manager will create a new client instance and return it.
6158 *
6259 * @param host endpoint of the OpenSergo Control Plane
6360 * @param port port of the OpenSergo Control Plane
6461 * @return OpenSergoClient
6562 */
6663 public OpenSergoClient getOrCreateClient (String host , int port ) {
67- return this .getOrCreateClient (host , port , new OpenSergoConfig ());
64+ return this .getOrCreateClient (host , port , new OpenSergoClientConfig ());
6865 }
6966
7067 /**
71- * get the instance from sharedOpenSergoClientCache with config.
68+ * Get the instance from sharedOpenSergoClientCache with config.
7269 * If instance can be found by host and port, the one will be returned, whether the config is matched or not.
7370 *
74- * @param host endpoint of the OpenSergo Control Plane
75- * @param port port of the OpenSergo Control Plane
76- * @param config OpenSergoConfig
71+ * @param host endpoint of the OpenSergo Control Plane
72+ * @param port port of the OpenSergo Control Plane
73+ * @param config client config
7774 * @return OpenSergoClient
7875 */
79- public OpenSergoClient getOrCreateClient (String host , int port , OpenSergoConfig config ) {
76+ public OpenSergoClient getOrCreateClient (String host , int port , OpenSergoClientConfig config ) {
8077 String sharedOpenSergoClientKey = buildSharedCacheKey (host , port );
81- OpenSergoClient openSergoClient = sharedOpenSergoClientCache .get (sharedOpenSergoClientKey );
78+ synchronized (this ) {
79+ OpenSergoClient openSergoClient = sharedClientCache .get (sharedOpenSergoClientKey );
80+ if (openSergoClient != null ) {
81+ return openSergoClient ;
82+ }
8283
83- if (openSergoClient != null ) {
84- return openSergoClient ;
84+ if (config == null ) {
85+ config = new OpenSergoClientConfig ();
86+ }
87+ openSergoClient = new OpenSergoClient .Builder ().endpoint (host , port )
88+ .openSergoConfig (config ).build ();
89+ sharedClientCache .putIfAbsent (sharedOpenSergoClientKey , openSergoClient );
90+ return sharedClientCache .get (sharedOpenSergoClientKey );
8591 }
86-
87- config = config == null ? new OpenSergoConfig () : config ;
88- openSergoClient = new OpenSergoClient .Builder ().endpoint (host , port ).openSergoConfig (config ).build ();
89- sharedOpenSergoClientCache .putIfAbsent (sharedOpenSergoClientKey , openSergoClient );
90- return sharedOpenSergoClientCache .get (sharedOpenSergoClientKey );
9192 }
9293
9394}
0 commit comments