NebulaGraph Java Client  release-3.8
SessionPoolConfig.java
1 /* Copyright (c) 2022 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 package com.vesoft.nebula.client.graph;
7 
8 import com.vesoft.nebula.client.graph.data.HostAddress;
9 import com.vesoft.nebula.client.graph.data.SSLParam;
10 import java.io.Serializable;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 
15 public class SessionPoolConfig implements Serializable {
16 
17  private static final long serialVersionUID = -2266013330384849132L;
18 
19  private final List<HostAddress> graphAddressList;
20 
21  private final String username;
22  private final String password;
23  private final String spaceName;
24 
25  // The min connections in pool for all addresses
26  private int minSessionSize = 1;
27 
28  // The max connections in pool for all addresses
29  private int maxSessionSize = 10;
30 
31  // Socket timeout and Socket connection timeout, unit: millisecond
32  private int timeout = 0;
33 
34  // The idleTime for clean the idle session
35  // must be less than NebulaGraph's session_idle_timeout_secs, unit: second
36  private int cleanTime = 3600;
37 
38  // The healthCheckTime for schedule check the health of session, unit: second
39  private int healthCheckTime = 600;
40 
41  // retry times to get session
42  private int retryConnectTimes = 1;
43 
44  // The wait time to get idle connection, unit ms
45  private int waitTime = 0;
46 
47  // retry times for failed execute
48  private int retryTimes = 3;
49 
50  // interval time for retry, unit ms
51  private int intervalTime = 0;
52 
53  // whether reconnect when create session using a broken graphd server
54  private boolean reconnect = false;
55 
56  // Set to true to turn on ssl encrypted traffic
57  private boolean enableSsl = false;
58 
59  // SSL param is required if ssl is turned on
60  private SSLParam sslParam = null;
61 
62  private boolean useHttp2 = false;
63 
64  private Map<String, String> customHeaders = new HashMap<>();
65 
66  public SessionPoolConfig(List<HostAddress> addresses,
67  String spaceName,
68  String username,
69  String password) {
70  if (addresses == null || addresses.size() == 0) {
71  throw new IllegalArgumentException("Graph addresses cannot be empty.");
72  }
73  if (spaceName == null || spaceName.trim().isEmpty()) {
74  throw new IllegalArgumentException("space name cannot be blank.");
75  }
76  if (username == null || username.trim().isEmpty()) {
77  throw new IllegalArgumentException("user name cannot be blank.");
78  }
79  if (password == null || password.trim().isEmpty()) {
80  throw new IllegalArgumentException("password cannot be blank.");
81  }
82 
83  this.graphAddressList = addresses;
84  this.spaceName = spaceName;
85  this.username = username;
86  this.password = password;
87  }
88 
89  public String getUsername() {
90  return username;
91  }
92 
93  public String getPassword() {
94  return password;
95  }
96 
97  public List<HostAddress> getGraphAddressList() {
98  return graphAddressList;
99  }
100 
101  public String getSpaceName() {
102  return spaceName;
103  }
104 
105  public int getMinSessionSize() {
106  return minSessionSize;
107  }
108 
109  public SessionPoolConfig setMinSessionSize(int minSessionSize) {
110  if (minSessionSize < 1) {
111  throw new IllegalArgumentException("minSessionSize cannot be less than 1.");
112  }
113  this.minSessionSize = minSessionSize;
114  return this;
115  }
116 
117  public int getMaxSessionSize() {
118  return maxSessionSize;
119  }
120 
121  public SessionPoolConfig setMaxSessionSize(int maxSessionSize) {
122  if (maxSessionSize < 1) {
123  throw new IllegalArgumentException("maxSessionSize cannot be less than 1.");
124  }
125  this.maxSessionSize = maxSessionSize;
126  return this;
127  }
128 
129  public int getTimeout() {
130  return timeout;
131  }
132 
133  public SessionPoolConfig setTimeout(int timeout) {
134  if (timeout < 0) {
135  throw new IllegalArgumentException("timeout cannot be less than 0.");
136  }
137  this.timeout = timeout;
138  return this;
139  }
140 
141  public int getCleanTime() {
142  return cleanTime;
143  }
144 
145  public SessionPoolConfig setCleanTime(int cleanTime) {
146  if (cleanTime < 0) {
147  throw new IllegalArgumentException("cleanTime cannot be less than 0.");
148  }
149  this.cleanTime = cleanTime;
150  return this;
151  }
152 
153  public int getHealthCheckTime() {
154  return healthCheckTime;
155  }
156 
157  public SessionPoolConfig setHealthCheckTime(int healthCheckTime) {
158  if (healthCheckTime < 0) {
159  throw new IllegalArgumentException("cleanTime cannot be less than 0.");
160  }
161  this.healthCheckTime = healthCheckTime;
162  return this;
163  }
164 
165  public int getRetryConnectTimes() {
166  return retryConnectTimes;
167  }
168 
169  public SessionPoolConfig setRetryConnectTimes(int retryConnectTimes) {
170  if (retryConnectTimes < 0) {
171  throw new IllegalArgumentException("retryConnectTimes cannot be less than 0.");
172  }
173  this.retryConnectTimes = retryConnectTimes;
174  return this;
175  }
176 
177  public int getWaitTime() {
178  return waitTime;
179  }
180 
181  public SessionPoolConfig setWaitTime(int waitTime) {
182  if (waitTime < 0) {
183  throw new IllegalArgumentException("waitTime cannot be less than 0.");
184  }
185  this.waitTime = waitTime;
186  return this;
187  }
188 
189  public int getRetryTimes() {
190  return retryTimes;
191  }
192 
193  public SessionPoolConfig setRetryTimes(int retryTimes) {
194  if (retryTimes < 0) {
195  throw new IllegalArgumentException("retryTimes cannot be less than 0.");
196  }
197  this.retryTimes = retryTimes;
198  return this;
199  }
200 
201  public int getIntervalTime() {
202  return intervalTime;
203  }
204 
205  public SessionPoolConfig setIntervalTime(int intervalTime) {
206  if (intervalTime < 0) {
207  throw new IllegalArgumentException("intervalTime cannot be less than 0.");
208  }
209  this.intervalTime = intervalTime;
210  return this;
211  }
212 
213  public boolean isReconnect() {
214  return reconnect;
215  }
216 
217  public SessionPoolConfig setReconnect(boolean reconnect) {
218  this.reconnect = reconnect;
219  return this;
220  }
221 
222  public boolean isEnableSsl() {
223  return enableSsl;
224  }
225 
226  public SessionPoolConfig setEnableSsl(boolean enableSsl) {
227  this.enableSsl = enableSsl;
228  return this;
229  }
230 
231  public SSLParam getSslParam() {
232  return sslParam;
233  }
234 
235  public SessionPoolConfig setSslParam(SSLParam sslParam) {
236  this.sslParam = sslParam;
237  return this;
238  }
239 
240  public boolean isUseHttp2() {
241  return useHttp2;
242  }
243 
244  public SessionPoolConfig setUseHttp2(boolean useHttp2) {
245  this.useHttp2 = useHttp2;
246  return this;
247  }
248 
249  public Map<String, String> getCustomHeaders() {
250  return customHeaders;
251  }
252 
253  public SessionPoolConfig setCustomHeaders(Map<String, String> customHeaders) {
254  this.customHeaders = customHeaders;
255  return this;
256  }
257 
258  @Override
259  public String toString() {
260  return "SessionPoolConfig{"
261  + "username='" + username + '\''
262  + ", graphAddressList=" + graphAddressList
263  + ", spaceName='" + spaceName + '\''
264  + ", minSessionSize=" + minSessionSize
265  + ", maxSessionSize=" + maxSessionSize
266  + ", timeout=" + timeout
267  + ", idleTime=" + cleanTime
268  + ", healthCheckTime=" + healthCheckTime
269  + ", waitTime=" + waitTime
270  + ", retryTimes=" + retryTimes
271  + ", intervalTIme=" + intervalTime
272  + ", reconnect=" + reconnect
273  + ", enableSsl=" + enableSsl
274  + ", sslParam=" + sslParam
275  + ", useHttp2=" + useHttp2
276  + ", customHeaders=" + customHeaders
277  + '}';
278  }
279 }