de.otto.synapse.leaderelection.LeaderElection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of synapse-core Show documentation
Show all versions of synapse-core Show documentation
A library used at otto.de to implement Spring Boot based event-sourcing microservices.
package de.otto.synapse.leaderelection;
import com.google.common.annotations.Beta;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
/**
* Leader-Election for Synapse services.
*
*/
@Beta
public interface LeaderElection {
/**
* Synchronously executes the runnable, if the current thread is able to become the leader
* by optaining the specified lock.
*
* @param lockName the name of the distributed lock used for leader election
* @param runnable the runnable that is executed by the leader
*/
void runIfLeader(String lockName,
Runnable runnable);
/**
* Synchronously executes the supplier, if the current thread is able to become the leader
* by optaining the specified lock.
*
* @param lockName the name of the distributed lock used for leader election
* @param supplier the supplier that is executed by the leader
* @param the type of the object returned by the supplier
* @return CompletableFuture of the object returned by the supplier, or CompletableFuture with value null,
* if the current thread is not the leader
*/
T supplyIfLeader(String lockName,
Supplier supplier);
/**
* Asynchronously executes the runnable, if the current thread is able to become the leader
* by optaining the specified lock.
*
* @param lockName the name of the distributed lock used for leader election
* @param runnable the runnable that is executed by the leader
* @return CompletableFuture<Void> that can be used to {@link CompletableFuture#join()} or further process
* the result.
*/
CompletableFuture runAsyncIfLeader(String lockName,
Runnable runnable);
/**
* Asynchronously executes the runnable, if the current thread is able to become the leader
* by optaining the specified lock.
*
* @param lockName the name of the distributed lock used for leader election
* @param runnable the runnable that is executed by the leader
* @param executor the Executor used to asynchronously run the Runnable
* @return CompletableFuture<Void> that can be used to {@link CompletableFuture#join()} or further process
* the result.
*/
CompletableFuture runAsyncIfLeader(String lockName,
Runnable runnable,
Executor executor);
/**
* Asynchronously executes the supplier, if the current thread is able to become the leader
* by optaining the specified lock.
*
* @param lockName the name of the distributed lock used for leader election
* @param supplier the supplier that is executed by the leader
* @param the type of the object returned by the supplier
* @return CompletableFuture of the object returned by the supplier, or CompletableFuture with value null,
* if the current thread is not the leader
*/
CompletableFuture supplyAsyncIfLeader(String lockName,
Supplier supplier);
/**
* Asynchronously executes the supplier, if the current thread is able to become the leader
* by optaining the specified lock.
*
* @param lockName the name of the distributed lock used for leader election
* @param supplier the supplier that is executed by the leader
* @param executor the Executor used to asynchronously supply the response
* @param the type of the object returned by the supplier
* @return CompletableFuture of the object returned by the supplier, or CompletableFuture with value null,
* if the current thread is not the leader
*/
CompletableFuture supplyAsyncIfLeader(String lockName,
Supplier supplier,
Executor executor);
}