Skip to content

Commit c404d78

Browse files
khatwaniNikhilpivovarit
authored andcommitted
Changes for BAEL-1532 (eugenp#3704)
* Changes for BAEL1532 * Changes for BAEL_1532
1 parent 83b0771 commit c404d78

4 files changed

Lines changed: 156 additions & 0 deletions

File tree

apache-zookeeper/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>apache-zookeeper</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.apache.zookeeper</groupId>
12+
<artifactId>zookeeper</artifactId>
13+
<version>3.3.2</version>
14+
<exclusions>
15+
<exclusion>
16+
<groupId>com.sun.jmx</groupId>
17+
<artifactId>jmxri</artifactId>
18+
</exclusion>
19+
<exclusion>
20+
<groupId>com.sun.jdmk</groupId>
21+
<artifactId>jmxtools</artifactId>
22+
</exclusion>
23+
<exclusion>
24+
<groupId>javax.jms</groupId>
25+
<artifactId>jms</artifactId>
26+
</exclusion>
27+
</exclusions>
28+
</dependency>
29+
</dependencies>
30+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.zookeeper.connection;
2+
3+
import java.io.IOException;
4+
import java.util.concurrent.CountDownLatch;
5+
6+
import org.apache.zookeeper.WatchedEvent;
7+
import org.apache.zookeeper.Watcher;
8+
import org.apache.zookeeper.Watcher.Event.KeeperState;
9+
import org.apache.zookeeper.ZooKeeper;
10+
11+
public class ZKConnection {
12+
private ZooKeeper zoo;
13+
final CountDownLatch connectionLatch = new CountDownLatch(1);
14+
15+
public ZKConnection() {
16+
}
17+
18+
public ZooKeeper connect(String host) throws IOException, InterruptedException {
19+
zoo = new ZooKeeper(host, 2000, new Watcher() {
20+
public void process(WatchedEvent we) {
21+
if (we.getState() == KeeperState.SyncConnected) {
22+
connectionLatch.countDown();
23+
}
24+
}
25+
});
26+
connectionLatch.await();
27+
return zoo;
28+
}
29+
30+
public void close() throws InterruptedException {
31+
zoo.close();
32+
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.zookeeper.manager;
2+
3+
import org.apache.zookeeper.KeeperException;
4+
5+
public interface ZKManager {
6+
/**
7+
* Create a Znode and save some data
8+
*
9+
* @param path
10+
* @param data
11+
* @throws KeeperException
12+
* @throws InterruptedException
13+
*/
14+
public void create(String path, byte[] data) throws KeeperException, InterruptedException;
15+
16+
/**
17+
* Get ZNode Data
18+
*
19+
* @param path
20+
* @param boolean watchFlag
21+
* @throws KeeperException
22+
* @throws InterruptedException
23+
*/
24+
public Object getZNodeData(String path, boolean watchFlag);
25+
26+
/**
27+
* Update the ZNode Data
28+
*
29+
* @param path
30+
* @param data
31+
* @throws KeeperException
32+
* @throws InterruptedException
33+
*/
34+
public void update(String path, byte[] data) throws KeeperException, InterruptedException, KeeperException;
35+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.zookeeper.manager;
2+
3+
import org.apache.zookeeper.CreateMode;
4+
import org.apache.zookeeper.KeeperException;
5+
import org.apache.zookeeper.ZooDefs;
6+
import org.apache.zookeeper.ZooKeeper;
7+
8+
import com.baeldung.zookeeper.connection.ZKConnection;
9+
10+
public class ZKManagerImpl implements ZKManager {
11+
private static ZooKeeper zkeeper;
12+
private static ZKConnection zkConnection;
13+
14+
public ZKManagerImpl() {
15+
initialize();
16+
}
17+
18+
/** * Initialize connection */
19+
private void initialize() {
20+
try {
21+
zkConnection = new ZKConnection();
22+
zkeeper = zkConnection.connect("localhost");
23+
} catch (Exception e) {
24+
System.out.println(e.getMessage());
25+
}
26+
}
27+
28+
public void closeConnection() {
29+
try {
30+
zkConnection.close();
31+
} catch (InterruptedException e) {
32+
System.out.println(e.getMessage());
33+
}
34+
}
35+
36+
public void create(String path, byte[] data) throws KeeperException, InterruptedException {
37+
zkeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
38+
}
39+
40+
public Object getZNodeData(String path, boolean watchFlag) {
41+
try {
42+
byte[] b = null;
43+
b = zkeeper.getData(path, null, null);
44+
String data = new String(b, "UTF-8");
45+
System.out.println(data);
46+
return data;
47+
} catch (Exception e) {
48+
System.out.println(e.getMessage());
49+
}
50+
return null;
51+
}
52+
53+
public void update(String path, byte[] data) throws KeeperException, InterruptedException {
54+
int version = zkeeper.exists(path, true)
55+
.getVersion();
56+
zkeeper.setData(path, data, version);
57+
}
58+
}

0 commit comments

Comments
 (0)