-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSDKClient.java
More file actions
114 lines (98 loc) · 4.25 KB
/
SDKClient.java
File metadata and controls
114 lines (98 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package couchbase.sdk;
import java.time.Duration;
import java.util.Properties;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.couchbase.client.core.deps.io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import com.couchbase.client.core.env.IoConfig;
import com.couchbase.client.core.env.SecurityConfig;
import com.couchbase.client.core.env.TimeoutConfig;
import com.couchbase.client.core.error.AuthenticationFailureException;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.ClusterOptions;
import com.couchbase.client.java.Collection;
import com.couchbase.client.java.env.ClusterEnvironment;
public class SDKClient {
static Logger logger = LogManager.getLogger(SDKClient.class);
public Server master;
public String bucket;
public String scope;
public String collection;
private Bucket bucketObj;
private Cluster cluster;
private ClusterEnvironment environment;
public Collection connection;
public SDKClient(Server master, String bucket, String scope, String collection) {
super();
this.master = master;
this.bucket = bucket;
this.scope = scope;
this.collection = collection;
}
public SDKClient(Server master, String bucket) {
super();
this.master = master;
this.bucket = bucket;
this.scope = "_default";
this.collection = "_default";
}
public SDKClient() {
super();
}
public void initialiseSDK() throws Exception {
logger.info("Connection to the cluster");
this.connectCluster();
this.connectBucket(bucket);
this.selectCollection(scope, collection);
}
public void connectCluster(){
try{
// Create per-instance environment for proper connection isolation
// Each SDKClient gets its own connection pool
ClusterOptions cluster_options;
if(this.master.memcached_port.equals("11207")) {
this.environment = ClusterEnvironment.builder()
.timeoutConfig(TimeoutConfig.builder().kvTimeout(Duration.ofSeconds(10)))
.securityConfig(SecurityConfig.enableTls(true)
.trustManagerFactory(InsecureTrustManagerFactory.INSTANCE))
.ioConfig(IoConfig.enableDnsSrv(true))
.ioConfig(IoConfig.numKvConnections(200)) // 200 per client instance
.ioConfig(IoConfig.configPollInterval(Duration.ofSeconds(10)))
.build();
cluster_options = ClusterOptions.clusterOptions(master.rest_username, master.rest_password).environment(this.environment);
} else {
this.environment = ClusterEnvironment.builder()
.timeoutConfig(TimeoutConfig.builder().kvTimeout(Duration.ofSeconds(10)))
.ioConfig(IoConfig.enableDnsSrv(true))
.ioConfig(IoConfig.numKvConnections(200)) // 200 per client instance
.ioConfig(IoConfig.configPollInterval(Duration.ofSeconds(10)))
.build();
cluster_options = ClusterOptions.clusterOptions(master.rest_username, master.rest_password).environment(this.environment);
}
this.cluster = Cluster.connect(master.ip, cluster_options);
logger.info("Cluster connection is successful");
}
catch (AuthenticationFailureException e) {
logger.info(String.format("cannot login from user: %s/%s",master.rest_username, master.rest_password));
}
}
public void disconnectCluster(){
// Disconnect and close all buckets
this.cluster.disconnect();
}
public void shutdownEnv() {
// Close the per-instance environment
if (this.environment != null) {
this.environment.shutdown();
}
}
private void connectBucket(String bucket){
this.bucketObj = this.cluster.bucket(bucket);
}
public void selectCollection(String scope, String collection) {
this.connection = this.bucketObj.scope(scope).collection(collection);
this.scope = scope;
this.collection = collection;
}
}