Class MongoClients


  • @ThreadSafe
    public class MongoClients
    extends Object
    A connection pool of MongoClient instances. This pool supports creating clients that communicate explicitly with a single server, or clients that communicate with any members of a replica set or sharded cluster given a set of seed addresses.
    Author:
    Randall Hauch
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  MongoClients.Builder
      Configures and builds a ConnectionPool.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Map<List<com.mongodb.ServerAddress>,​com.mongodb.client.MongoClient> connections  
      private Map<com.mongodb.ServerAddress,​com.mongodb.client.MongoClient> directConnections  
      private com.mongodb.MongoClientSettings.Builder settings  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private MongoClients​(com.mongodb.MongoClientSettings.Builder settings)  
    • Field Detail

      • directConnections

        private final Map<com.mongodb.ServerAddress,​com.mongodb.client.MongoClient> directConnections
      • connections

        private final Map<List<com.mongodb.ServerAddress>,​com.mongodb.client.MongoClient> connections
      • settings

        private final com.mongodb.MongoClientSettings.Builder settings
    • Constructor Detail

      • MongoClients

        private MongoClients​(com.mongodb.MongoClientSettings.Builder settings)
    • Method Detail

      • create

        public static MongoClients.Builder create()
        Obtain a builder that can be used to configure and create a connection pool.
        Returns:
        the new builder; never null
      • clear

        public void clear()
        Clear out and close any open connections.
      • clientFor

        public com.mongodb.client.MongoClient clientFor​(String addressString)
        Obtain a direct client connection to the specified server. This is typically used to connect to a standalone server, but it also can be used to obtain a client that will only use this server, even if the server is a member of a replica set or sharded cluster.

        The format of the supplied string is one of the following:

         host:port
         host
         
        where host contains the resolvable hostname or IP address of the server, and port is the integral port number. If the port is not provided, the default port is used. If neither the host or port are provided (or addressString is null), then an address will use the default host and default port.
        Parameters:
        addressString - the string that contains the host and port of the server
        Returns:
        the MongoClient instance; never null
      • clientFor

        public com.mongodb.client.MongoClient clientFor​(com.mongodb.ServerAddress address)
        Obtain a direct client connection to the specified server. This is typically used to connect to a standalone server, but it also can be used to obtain a client that will only use this server, even if the server is a member of a replica set or sharded cluster.
        Parameters:
        address - the address of the server to use
        Returns:
        the MongoClient instance; never null
      • clientForMembers

        public com.mongodb.client.MongoClient clientForMembers​(String addressList)
        Obtain a client connection to the replica set or cluster. The supplied addresses are used as seeds, and once a connection is established it will discover all of the members.

        The format of the supplied string is one of the following:

         replicaSetName/host:port
         replicaSetName/host:port,host2:port2
         replicaSetName/host:port,host2:port2,host3:port3
         host:port
         host:port,host2:port2
         host:port,host2:port2,host3:port3
         
        where replicaSetName is the name of the replica set, host contains the resolvable hostname or IP address of the server, and port is the integral port number. If the port is not provided, the default port is used. If neither the host or port are provided (or addressString is null), then an address will use the default host and default port.

        This method does not use the replica set name.

        Parameters:
        addressList - the string containing a comma-separated list of host and port pairs, optionally preceded by a replica set name
        Returns:
        the MongoClient instance; never null
      • clientForMembers

        public com.mongodb.client.MongoClient clientForMembers​(com.mongodb.ServerAddress... seeds)
        Obtain a client connection to the replica set or cluster. The supplied addresses are used as seeds, and once a connection is established it will discover all of the members.
        Parameters:
        seeds - the seed addresses
        Returns:
        the MongoClient instance; never null
      • clientForMembers

        public com.mongodb.client.MongoClient clientForMembers​(List<com.mongodb.ServerAddress> seedAddresses)
        Obtain a client connection to the replica set or cluster. The supplied addresses are used as seeds, and once a connection is established it will discover all of the members.
        Parameters:
        seedAddresses - the seed addresses
        Returns:
        the MongoClient instance; never null
      • directConnection

        protected com.mongodb.client.MongoClient directConnection​(com.mongodb.ServerAddress address)
      • connection

        protected com.mongodb.client.MongoClient connection​(List<com.mongodb.ServerAddress> addresses)