Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How ZooKeeper Java client API updates data

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to update data on ZooKeeper Java client API. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Synchronously update data import java.util.concurrent.CountDownLatch;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.KeeperState;import org.apache.zookeeper.Watcher.Event.EventType;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.data.Stat Public class SetData_API_Sync_Usage implements Watcher {private static CountDownLatch connectedSemaphore = new CountDownLatch (1); private static ZooKeeper zk = null; public static void main (String [] args) throws Exception {String path = "/ titush-book2"; zk = new ZooKeeper ("202.201.purve2100", 5000, new SetData_API_Sync_Usage ()) ConnectedSemaphore.await (); zk.create (path, "1231" .getBytes (), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.getData (path, true, null); Stat stat = zk.setData (path, "4561" .getBytes (),-1) System.out.println (stat.getCzxid () + "," + stat.getMzxid () + "," + stat.getVersion ()); Stat stat2 = zk.setData (path, "4561" .getBytes (), stat.getVersion ()); System.out.println (stat2.getCzxid () + "," + stat2.getMzxid () + "," + stat2.getVersion () Try {zk.setData (path, "456" .getBytes (), stat.getVersion ());} catch (KeeperException e) {System.out.println ("Error:" + e.code () + "," + e.getMessage ());} Thread.sleep (Integer.MAX_VALUE) @ Override public void process (WatchedEvent event) {if (KeeperState.SyncConnected = = event.getState ()) {if (EventType.None = = event.getType () & & null = = event.getPath ()) {connectedSemaphore.countDown ();}

Result printing

111PerErrorCode = BadVersion for / titush-book2

The current latest version of the data node is represented as-1, and the first version is represented as 0.

The version is updated to 1 after the first setting of data and to 2 after the second setting.

An error occurred when updating with version 1 for the third time, because the version at this time was already 2.

This is the use of optimistic locks, and stat.getVersion () is used for write checking.

On the ZooKeeper Java client API how to update the data to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report