org.apache.curator.framework.recipes.shared.SharedCountListener Java Examples

The following examples show how to use org.apache.curator.framework.recipes.shared.SharedCountListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SharedCacheCoordinator.java    From datawave with Apache License 2.0 6 votes vote down vote up
private void reregisterCounter(String counterName, SharedCountListener listener, int seedValue) throws Exception {
    ArgumentChecker.notNull(counterName, listener);
    
    SharedCount count = new SharedCount(curatorClient, ZKPaths.makePath("/counters", counterName), seedValue);
    count.start();
    sharedCounters.put(counterName, count);
    sharedCountListeners.put(counterName, listener);
    localCounters.put(counterName, count.getCount());
    
    count.addListener(new SharedCountListener() {
        @Override
        public void countHasChanged(SharedCountReader sharedCountReader, int i) throws Exception {}
        
        @Override
        public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
            if (connectionState == ConnectionState.RECONNECTED) {
                try {
                    reregisterCounter(counterName, sharedCountListeners.get(counterName), localCounters.get(counterName));
                } catch (Exception e) {
                    System.err.println("Unable to re-register counter " + counterName + ": " + e.getMessage());
                }
            }
        }
    });
    count.addListener(listener);
}
 
Example #2
Source File: TestSharedCacheCoordinator.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Registers a distributed shared counter named {@code counterName}. This counter can be watched on many servers, and can be used to coordinate local
 * in-memory global operations.
 * 
 * @param counterName
 *            the name of the counter
 * @param listener
 *            a listener that is called when the counter value changes
 */
public void registerCounter(String counterName, SharedCountListener listener) throws Exception {
    ArgumentChecker.notNull(counterName, listener);
    
    SharedCount count = new SharedCount(curatorClient, ZKPaths.makePath("/counters", counterName), 1);
    count.start();
    sharedCounters.put(counterName, count);
    localCounters.put(counterName, count.getCount());
    
    count.addListener(listener);
}
 
Example #3
Source File: SharedCacheCoordinator.java    From datawave with Apache License 2.0 2 votes vote down vote up
/**
 * Registers a distributed shared counter named {@code counterName}. This counter can be watched on many servers, and can be used to coordinate local
 * in-memory global operations.
 * 
 * @param counterName
 *            the name of the counter
 * @param listener
 *            a listener that is called when the counter value changes
 */
public void registerCounter(String counterName, SharedCountListener listener) throws Exception {
    Preconditions.checkArgument(!sharedCounters.containsKey(counterName), "Counter " + counterName + " has already been registered!");
    reregisterCounter(counterName, listener, 1);
}