All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.otto.synapse.leaderelection.LeaderElection Maven / Gradle / Ivy

Go to download

A library used at otto.de to implement Spring Boot based event-sourcing microservices.

The newest version!
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);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy