Java Code Examples for org.apache.curator.framework.recipes.shared.SharedCount#addListener()
The following examples show how to use
org.apache.curator.framework.recipes.shared.SharedCount#addListener() .
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 |
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 |
/** * 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: SharedCounterExample.java From ZKRecipesByExample with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws IOException, Exception { final Random rand = new Random(); SharedCounterExample example = new SharedCounterExample(); try (TestingServer server = new TestingServer()) { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); SharedCount baseCount = new SharedCount(client, PATH, 0); baseCount.addListener(example); baseCount.start(); List<SharedCount> examples = Lists.newArrayList(); ExecutorService service = Executors.newFixedThreadPool(QTY); for (int i = 0; i < QTY; ++i) { final SharedCount count = new SharedCount(client, PATH, 0); examples.add(count); Callable<Void> task = new Callable<Void>() { @Override public Void call() throws Exception { count.start(); Thread.sleep(rand.nextInt(10000)); System.out.println("Increment:" + count.trySetCount(count.getVersionedValue(), count.getCount() + rand.nextInt(10))); return null; } }; service.submit(task); } service.shutdown(); service.awaitTermination(10, TimeUnit.MINUTES); for (int i = 0; i < QTY; ++i) { examples.get(i).close(); } baseCount.close(); } }