sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-nosql-couchbase-client] 08/14: delay couchbase connection until first access, and make sure it is tried again and again until it succeeds
Date Tue, 07 Nov 2017 09:55:56 GMT
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.nosql.couchbase-client-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-nosql-couchbase-client.git

commit 73535a6901e6bc008bb4bf3e6075ee52411e7879
Author: Stefan Seifert <sseifert@apache.org>
AuthorDate: Fri Aug 14 16:45:43 2015 +0000

    delay couchbase connection until first access, and make sure it is tried again and again
until it succeeds
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/nosql/couchbase-client@1695944
13f79535-47bb-0310-9956-ffa450edef68
---
 .../couchbase/client/impl/CouchbaseClientImpl.java | 36 ++++++++++++++--------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/apache/sling/nosql/couchbase/client/impl/CouchbaseClientImpl.java
b/src/main/java/org/apache/sling/nosql/couchbase/client/impl/CouchbaseClientImpl.java
index 5103ca4..ef9a779 100644
--- a/src/main/java/org/apache/sling/nosql/couchbase/client/impl/CouchbaseClientImpl.java
+++ b/src/main/java/org/apache/sling/nosql/couchbase/client/impl/CouchbaseClientImpl.java
@@ -68,7 +68,11 @@ public class CouchbaseClientImpl implements CouchbaseClient {
 
     private String clientId;
     private boolean enabled;
+    private String[] couchbaseHosts;
     private String bucketName;
+    private String bucketPassword;
+    
+    private volatile boolean initialized;
     private Cluster cluster;
     private Bucket bucket;
 
@@ -76,34 +80,26 @@ public class CouchbaseClientImpl implements CouchbaseClient {
     private void activate(Map<String, Object> config) {
         clientId = PropertiesUtil.toString(config.get(CLIENT_ID_PROPERTY), null);
         enabled = PropertiesUtil.toBoolean(config.get(ENABLED_PROPERTY), ENABLED_PROPERTY_DEFAULT);
+        couchbaseHosts = PropertiesUtil.toStringArray(config.get(COUCHBASE_HOSTS_PROPERTY));
+        bucketName = PropertiesUtil.toString(config.get(CACHE_BUCKET_NAME_PROPERTY), null);
+        bucketPassword = PropertiesUtil.toString(config.get(CACHE_BUCKET_PASSWORD_PROPERTY),
null);
+
         if (!enabled) {
             log.info("Couchbase caching for client '{}' is disabled by configuration.", clientId);
             return;
         }
 
-        String[] couchbaseHosts = PropertiesUtil.toStringArray(config.get(COUCHBASE_HOSTS_PROPERTY));
         if (couchbaseHosts == null || couchbaseHosts.length == 0) {
             enabled = false;
             log.warn("No couchbase host configured, client '{}' is disabled.", clientId);
             return;
         }
 
-        bucketName = PropertiesUtil.toString(config.get(CACHE_BUCKET_NAME_PROPERTY), null);
-        String bucketPassword = PropertiesUtil.toString(config.get(CACHE_BUCKET_PASSWORD_PROPERTY),
null);
         if (bucketName == null) {
             enabled = false;
             log.warn("No couchbase bucket name configured, client '{}' is disabled.", clientId);
             return;
         }
-
-        try {
-            cluster = CouchbaseEnvironmentSingleton.createCluster(couchbaseHosts);
-            bucket = CouchbaseEnvironmentSingleton.openBucket(cluster, bucketName, bucketPassword);
-        }
-        catch (Throwable ex) {
-            enabled = false;
-            log.error("Unable to connect to couchbase cluster or open couchbase bucket, client
'" + clientId + "' is disabled.", ex);
-        }
     }
 
     @Deactivate
@@ -131,11 +127,25 @@ public class CouchbaseClientImpl implements CouchbaseClient {
     }
 
     public Bucket getBucket() {
+        if (!initialized) {
+            synchronized (this) {
+                if (!initialized) {
+                    try {
+                        cluster = CouchbaseEnvironmentSingleton.createCluster(couchbaseHosts);
+                        bucket = CouchbaseEnvironmentSingleton.openBucket(cluster, bucketName,
bucketPassword);
+                        initialized = true;
+                    }
+                    catch (Throwable ex) {
+                        throw new RuntimeException("Unable to connect to couchbase cluster
or open couchbase bucket, client '" + clientId + "'.", ex);
+                    }
+                }
+            }
+        }
         return bucket;
     }
 
     public AsyncBucket getAsyncBucket() {
-        return bucket.async();
+        return getBucket().async();
     }
 
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message