Package io.debezium.connector.mongodb
Class ConnectionContext.MongoPrimary
- java.lang.Object
-
- io.debezium.connector.mongodb.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
-
-
Field Summary
Fields Modifier and Type Field Description private BiConsumer<String,Throwable>errorHandlerprivate Filtersfiltersprivate Supplier<com.mongodb.client.MongoClient>primaryConnectionSupplierprivate ReplicaSetreplicaSetprivate AtomicBooleanrunning
-
Constructor Summary
Constructors Modifier Constructor Description protectedMongoPrimary(ConnectionContext context, ReplicaSet replicaSet, Filters filters, BiConsumer<String,Throwable> errorHandler)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description com.mongodb.ServerAddressaddress()Get the address of the primary node, if there is one.List<CollectionId>collections()Use the primary to get the identifiers of all the collections in the replica set, applying the current collection filter configuration.Set<String>databaseNames()Use the primary to get the names of all the databases in the replica set, applying the current database filter configuration.voidexecute(String desc, Consumer<com.mongodb.client.MongoClient> operation)Execute the supplied operation using the primary, blocking until a primary is available.<T> Texecute(String desc, Function<com.mongodb.client.MongoClient,T> operation)Execute the supplied operation using the primary, blocking until a primary is available.voidexecuteBlocking(String desc, BlockingConsumer<com.mongodb.client.MongoClient> operation)Execute the supplied operation using the primary, blocking until a primary is available.private booleanisRunning()ReplicaSetreplicaSet()Get the replica set.voidstop()Terminates the execution loop of the current primary
-
-
-
Field Detail
-
replicaSet
private final ReplicaSet replicaSet
-
primaryConnectionSupplier
private final Supplier<com.mongodb.client.MongoClient> primaryConnectionSupplier
-
filters
private final Filters filters
-
errorHandler
private final BiConsumer<String,Throwable> errorHandler
-
running
private final AtomicBoolean running
-
-
Constructor Detail
-
MongoPrimary
protected MongoPrimary(ConnectionContext context, ReplicaSet replicaSet, Filters filters, BiConsumer<String,Throwable> errorHandler)
-
-
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
nullif 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 purposesoperation- 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 purposesoperation- 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 purposesoperation- 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
-
-