|
@@ -1,14 +1,18 @@
|
|
|
package com.example.helloworld.config;
|
|
|
|
|
|
-import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
-import com.mongodb.MongoClient;
|
|
|
-import org.hibernate.validator.constraints.NotEmpty;
|
|
|
-
|
|
|
import javax.validation.constraints.Max;
|
|
|
import javax.validation.constraints.Min;
|
|
|
-import java.net.UnknownHostException;
|
|
|
+
|
|
|
+import org.hibernate.validator.constraints.NotEmpty;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
+import com.mongodb.MongoClient;
|
|
|
+import com.mongodb.MongoClientOptions;
|
|
|
+import com.mongodb.ServerAddress;
|
|
|
|
|
|
public class MongoClientFactory {
|
|
|
+ private static final int MAX_DB_REQUEST_CONCURRENCY = 512;
|
|
|
+
|
|
|
@JsonProperty
|
|
|
@NotEmpty
|
|
|
private String host;
|
|
@@ -21,6 +25,11 @@ public class MongoClientFactory {
|
|
|
@JsonProperty
|
|
|
@NotEmpty
|
|
|
private String db;
|
|
|
+
|
|
|
+ @JsonProperty
|
|
|
+ @Min(1)
|
|
|
+ @Max(4096)
|
|
|
+ private int connections;
|
|
|
|
|
|
public String getHost() {
|
|
|
return host;
|
|
@@ -34,7 +43,15 @@ public class MongoClientFactory {
|
|
|
return db;
|
|
|
}
|
|
|
|
|
|
+ public int getConnections() {
|
|
|
+ return connections;
|
|
|
+ }
|
|
|
+
|
|
|
public MongoClient build() {
|
|
|
- return new MongoClient(host, port);
|
|
|
+ MongoClientOptions.Builder options = MongoClientOptions.builder();
|
|
|
+ options.connectionsPerHost(connections);
|
|
|
+ options.threadsAllowedToBlockForConnectionMultiplier(
|
|
|
+ (int) Math.ceil((double) MAX_DB_REQUEST_CONCURRENCY / connections));
|
|
|
+ return new MongoClient(new ServerAddress(host, port), options.build());
|
|
|
}
|
|
|
}
|