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

org.jgroups.blocks.atomic.CounterService Maven / Gradle / Ivy

Go to download

This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up with different versions on classes on the class path).

There is a newer version: 34.0.0.Final
Show newest version
package org.jgroups.blocks.atomic;

import java.util.concurrent.CompletionStage;

import org.jgroups.JChannel;
import org.jgroups.protocols.COUNTER;
import org.jgroups.util.CompletableFutures;

/**
 * Provides a distributed counter (similar to AtomicLong) which can be atomically updated across a cluster.
 * @author Bela Ban
 * @since 3.0.0
 */
public class CounterService {
    protected COUNTER  counter_prot;

    public CounterService(JChannel ch) {
        setChannel(ch);
    }

    public void setChannel(JChannel ch) {
        counter_prot=ch.getProtocolStack().findProtocol(COUNTER.class);
        if(counter_prot == null)
            throw new IllegalStateException("channel configuration must include the COUNTER protocol");
    }

    /**
     * Returns an existing counter, or creates a new one if none exists
     * @param name Name of the counter, different counters have to have different names
     * @param initial_value The initial value of a new counter if there is no existing counter. Ignored
     * if the counter already exists
     * @return The counter implementation
     * @deprecated since 5.2. Use {@link #getOrCreateSyncCounter(String, long)} instead.
     */
    @Deprecated
    public Counter getOrCreateCounter(String name, long initial_value) {
        return counter_prot.getOrCreateCounter(name, initial_value);
    }

    public SyncCounter getOrCreateSyncCounter(String name, long initial_value) {
        return CompletableFutures.join(getOrCreateAsyncCounter(name, initial_value).thenApply(AsyncCounter::sync));
    }

    /**
     * Returns an existing counter, or creates a new one if none exists
     *
     * @param name          Name of the counter, different counters have to have different names
     * @param initial_value The initial value of a new counter if there is no existing counter. Ignored
     *                      if the counter already exists
     * @return A {@link CompletionStage} which is completed with the counter implementation.
     */
    public CompletionStage getOrCreateAsyncCounter(String name, long initial_value) {
        return counter_prot.getOrCreateAsyncCounter(name, initial_value);
    }

    /**
     * Deletes a counter instance (on the coordinator)
     * @param name The name of the counter. No-op if the counter doesn't exist
     */
    public void deleteCounter(String name) {
        counter_prot.deleteCounter(name);
    }


    public String printCounters() {
        return counter_prot.printCounters();
    }


    public String dumpPendingRequests() {return counter_prot.dumpPendingRequests();}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy