Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
L
leader-election-zookeeper
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohamadbashar.disoki
leader-election-zookeeper
Commits
2879dbfc
Commit
2879dbfc
authored
Dec 01, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Watchers, Triggers and Introduction to Failure Detection
parent
4b023b0d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
0 deletions
+92
-0
Readme.md
Readme.md
+1
-0
WatchersAndTriggers.java
src/main/java/WatchersAndTriggers.java
+91
-0
No files found.
Readme.md
View file @
2879dbfc
...
...
@@ -6,6 +6,7 @@
*
Zookeeper Server and Client
*
Zookeeper Client Threading Model & Zookeeper Java API
*
Leader Election Implementation
*
Watchers, Triggers and Introduction to Failure Detection
*Useful links*
...
...
src/main/java/WatchersAndTriggers.java
0 → 100644
View file @
2879dbfc
import
org.apache.zookeeper.KeeperException
;
import
org.apache.zookeeper.WatchedEvent
;
import
org.apache.zookeeper.Watcher
;
import
org.apache.zookeeper.ZooKeeper
;
import
org.apache.zookeeper.data.Stat
;
import
java.io.IOException
;
import
java.util.List
;
public
class
WatchersAndTriggers
implements
Watcher
{
private
static
final
String
address
=
"172.29.3.101:2181"
;
private
static
final
int
SESSION_TIMEOUT
=
3000
;
//dead client
private
static
final
String
TargetZnode
=
"/target_znode"
;
private
ZooKeeper
zooKeeper
;
public
static
void
main
(
String
[]
args
)
throws
IOException
,
InterruptedException
,
KeeperException
{
WatchersAndTriggers
watchersAndTriggers
=
new
WatchersAndTriggers
();
watchersAndTriggers
.
connectToZookeeper
();
watchersAndTriggers
.
testWatcher
();
watchersAndTriggers
.
run
();
watchersAndTriggers
.
close
();
}
private
void
close
()
throws
InterruptedException
{
this
.
zooKeeper
.
close
();
}
private
void
run
()
throws
InterruptedException
{
synchronized
(
zooKeeper
)
{
this
.
zooKeeper
.
wait
();
}
}
private
void
connectToZookeeper
()
throws
IOException
{
this
.
zooKeeper
=
new
ZooKeeper
(
address
,
SESSION_TIMEOUT
,
this
);
}
public
void
testWatcher
()
throws
InterruptedException
,
KeeperException
{
Stat
stat
=
zooKeeper
.
exists
(
TargetZnode
,
this
);
if
(
stat
==
null
)
{
System
.
out
.
println
(
TargetZnode
+
" not exist"
);
return
;
}
byte
[]
data
=
zooKeeper
.
getData
(
TargetZnode
,
this
,
stat
);
List
<
String
>
children
=
zooKeeper
.
getChildren
(
TargetZnode
,
this
);
System
.
out
.
println
(
"Data "
+
new
String
(
data
)
+
"children "
+
children
);
}
@Override
public
void
process
(
WatchedEvent
watchedEvent
)
{
switch
(
watchedEvent
.
getType
())
{
case
None:
if
(
watchedEvent
.
getState
()
==
Event
.
KeeperState
.
SyncConnected
)
{
System
.
out
.
println
(
"Successfully connected to Zookeeper"
);
}
else
if
(
watchedEvent
.
getState
()
==
Event
.
KeeperState
.
Disconnected
)
{
synchronized
(
zooKeeper
)
{
System
.
out
.
println
(
"Disconnected from Zookeeper"
);
zooKeeper
.
notifyAll
();
}
}
else
if
(
watchedEvent
.
getState
()
==
Event
.
KeeperState
.
Closed
)
{
System
.
out
.
println
(
"Closed Successfully"
);
}
case
NodeCreated:
System
.
out
.
println
(
TargetZnode
+
" Created"
);
break
;
case
NodeDeleted:
System
.
out
.
println
(
TargetZnode
+
" Deleted"
);
break
;
case
NodeDataChanged:
System
.
out
.
println
(
TargetZnode
+
" DataChanged"
);
break
;
case
NodeChildrenChanged:
System
.
out
.
println
(
TargetZnode
+
" ChildrenChanged"
);
break
;
}
try
{
testWatcher
();
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
KeeperException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment