Class ConnectionContext.MongoPrimary

  • Enclosing class:
    ConnectionContext

    public static class ConnectionContext.MongoPrimary
    extends Object
    A supplier of a client that connects only to the primary of a replica set. Operations on the primary will continue
    • Method Detail

      • replicaSet

        public ReplicaSet replicaSet()
        Get the replica set.
        Returns:
        the replica set; never null
      • address

        public com.mongodb.ServerAddress address()
        Get the address of the primary node, if there is one.
        Returns:
        the address of the replica set's primary node, or null if there is currently no primary
      • execute

        public void execute​(String desc,
                            Consumer<com.mongodb.client.MongoClient> operation)
        Execute the supplied operation using the primary, blocking until a primary is available. Whenever the operation stops (e.g., if the primary is no longer primary), then restart the operation using the current primary.
        Parameters:
        desc - the description of the operation, for logging purposes
        operation - the operation to be performed on the primary.
      • execute

        public <T> T execute​(String desc,
                             Function<com.mongodb.client.MongoClient,​T> operation)
        Execute the supplied operation using the primary, blocking until a primary is available. Whenever the operation stops (e.g., if the primary is no longer primary), then restart the operation using the current primary.
        Parameters:
        desc - the description of the operation, for logging purposes
        operation - the operation to be performed on the primary
        Returns:
        return value of the executed operation
      • executeBlocking

        public void executeBlocking​(String desc,
                                    BlockingConsumer<com.mongodb.client.MongoClient> operation)
                             throws InterruptedException
        Execute the supplied operation using the primary, blocking until a primary is available. Whenever the operation stops (e.g., if the primary is no longer primary), then restart the operation using the current primary.
        Parameters:
        desc - the description of the operation, for logging purposes
        operation - the operation to be performed on the primary.
        Throws:
        InterruptedException - if the operation was interrupted
      • databaseNames

        public Set<String> databaseNames()
        Use the primary to get the names of all the databases in the replica set, applying the current database filter configuration. This method will block until a primary can be obtained to get the names of all databases in the replica set.
        Returns:
        the database names; never null but possibly empty
      • collections

        public List<CollectionId> collections()
        Use the primary to get the identifiers of all the collections in the replica set, applying the current collection filter configuration. This method will block until a primary can be obtained to get the identifiers of all collections in the replica set.
        Returns:
        the collection identifiers; never null
      • isRunning

        private boolean isRunning()
      • stop

        public void stop()
        Terminates the execution loop of the current primary