Skip to content

Commit 54ccc78

Browse files
samples project containing small zookeeper code samples added
samples project containing small zookeeper code samples added
1 parent ba2eaaf commit 54ccc78

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

apache-zookeeper/samples/.classpath

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
</attributes>
34+
</classpathentry>
35+
<classpathentry kind="output" path="target/classes"/>
36+
</classpath>

apache-zookeeper/samples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

apache-zookeeper/samples/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>samples</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

apache-zookeeper/samples/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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.sts.allprogtutorials.apachezookeeper</groupId>
5+
<artifactId>samples</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>org.apache.zookeeper</groupId>
11+
<artifactId>zookeeper</artifactId>
12+
<version>3.4.6</version>
13+
</dependency>
14+
15+
</dependencies>
16+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.sts.allprogtutorials.apachezookeeper.main;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.log4j.Logger;
6+
import org.apache.zookeeper.CreateMode;
7+
import org.apache.zookeeper.KeeperException;
8+
import org.apache.zookeeper.WatchedEvent;
9+
import org.apache.zookeeper.Watcher;
10+
import org.apache.zookeeper.ZooDefs.Ids;
11+
import org.apache.zookeeper.ZooKeeper;
12+
13+
public class ZooKeeperDemo {
14+
15+
private static final Logger LOG = Logger.getLogger(ZooKeeperDemo.class);
16+
17+
public static void main(String[] args) throws IOException {
18+
19+
final ZooKeeper zooKeeper = new ZooKeeper("192.168.111.144:2181", 2000, new ZooKeeperWatcher());
20+
21+
try {
22+
//create a node with path "/demo", data as blank, ACL giving everyone rights on this node and create mode as Ephemeral
23+
final String createdNodePath = zooKeeper.create("/demo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
24+
25+
LOG.info("Node created with path: " + createdNodePath);
26+
27+
//Delete a node with path "/demo" and version as -1 means don't care for current version
28+
zooKeeper.delete(createdNodePath, -1);
29+
30+
} catch (KeeperException | InterruptedException e) {
31+
e.printStackTrace();
32+
}
33+
34+
}
35+
36+
public static class ZooKeeperWatcher implements Watcher {
37+
38+
public void process(WatchedEvent event) {
39+
//process the event
40+
}
41+
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3+
<log4j:configuration debug="true"
4+
xmlns:log4j='http://jakarta.apache.org/log4j/'>
5+
6+
<appender name="console" class="org.apache.log4j.ConsoleAppender">
7+
<layout class="org.apache.log4j.PatternLayout">
8+
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
9+
</layout>
10+
</appender>
11+
12+
<logger name="org.apache.zookeeper">
13+
<level value="WARN" />
14+
</logger>
15+
16+
<root>
17+
<level value="DEBUG" />
18+
<appender-ref ref="console" />
19+
</root>
20+
21+
</log4j:configuration>

0 commit comments

Comments
 (0)