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

org.kiwiproject.curator.leader.resource.LeaderResource Maven / Gradle / Ivy

Go to download

This is a small library that integrates Apache Curator's Leader Latch recipe into a Dropwizard service.

There is a newer version: 2.3.1
Show newest version
package org.kiwiproject.curator.leader.resource;

import org.kiwiproject.curator.leader.ManagedLeaderLatch;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;

/**
 * JAX-RS resource providing endpoints for checking leadership and participant information.
 */
@Path("/kiwi/leader-latch")
@Produces(MediaType.APPLICATION_JSON)
public class LeaderResource {

    private final ManagedLeaderLatch leaderLatch;

    public LeaderResource(ManagedLeaderLatch leaderLatch) {
        this.leaderLatch = leaderLatch;
    }

    /**
     * Checks whether this service is the latch leader.
     *
     * @return the JSON {@link Response}
     */
    @GET
    @Path("/leader")
    public Response hasLeadership() {
        var entity = Map.of(
                "leader", leaderLatch.hasLeadership()
        );
        return Response.ok(entity).build();
    }

    /**
     * Get information about the leader latch that this service participates in.
     *
     * @return the JSON {@link Response}
     */
    @GET
    @Path("/latch")
    public Response getLatchState() {
        var entity = Map.of(
                "id", leaderLatch.getId(),
                "leader", leaderLatch.hasLeadership(),
                "latchPath", leaderLatch.getLatchPath(),
                "participants", leaderLatch.getParticipants(),
                "state", leaderLatch.getLatchState()
        );
        return Response.ok(entity).build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy