/**
* Classname:zookeeperwatcher
* This class is a watcher class
* date:2017 August 31 9:44:31
* @author Yanlong
*/
public class Zookeeperwatcher implements watcher{
Defining Atomic Variables
Atomicinteger seq = new Atomicinteger ();
Define session Expiration Time
private static final int session_timeout = 1000;
Zookeeper Server address
private static final String connection_addr = "112.124.121.34:2181";
ZK Parent Path Settings
private static final String Parent_path = "/P";
ZK Sub-path settings
private static final String Children_path = "/P/C";
Enter logo
private static final String Log_prefix_of_main = "" MAIN "";
ZK variable
Private ZooKeeper ZK = null;
Semaphore settings are used to wait for the zookeeper connection to notify the blocker to continue down.
Private Countdownlatch Connectedsemaphore = new Countdownlatch (1);
/**
*
* CreateConnection: Create ZK connection
* @author Yanlong
* date:2017 August 31 10:09:16
* @param connectaddr ZK server address List
* @param sessiontimeout Session timeout period
*/
public void CreateConnection (String connectaddr,int sessiontimeout) {
This.releaseconnection ();
try {
ZK = new ZooKeeper (connectaddr, sessiontimeout, this);
System.out.println (log_prefix_of_main+ "Start connection to ZK server");
Connectedsemaphore.await ();
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Close Connection
* Releaseconnection:
* @author Yanlong
* date:2017 August 31 10:10:37
*/
public void Releaseconnection () {
if (THIS.ZK! = null) {
try{
This.zk.close ();
}catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
/**
*
* Createpath: Create node
* @author Yanlong
* date:2017 August 31 10:14:58
* @param Path of path node
* @param data node content
* @return
*/
public boolean Createpath (String path,string Data,boolean needwatch) {
Set up monitoring (since zookeeper monitoring is all-in-one, you must set up monitoring every time)
try {
This.zk.exists (path, needwatch);//pre-monitoring before creating a node
System.out.println (log_prefix_of_main+ "node creation succeeded, Path:" +
This.zk.create (
/** Path **/
Path
/** Data **/
Data.getbytes (),
/** all visible **/
Ids.open_acl_unsafe,
/** Permanent Storage **/
Createmode.persistent) + ", Content:" +data);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
return false;
}
return true;
}
/**
*
* ReadData: Reads the contents of the specified node data
* @author Yanlong
* date:2017 August 31 10:32:09
* @param Path of path node
* @param needwatch
* @return
*/
public string ReadData (string Path,boolean needwatch) {
try {
SYSTEM.OUT.PRINTLN ("read operation ...");
return new String (This.zk.getData (path, needwatch, null));
} catch (Exception e) {
E.printstacktrace ();
Return "";
}
}
/**
*
* WriteData: Update the data content of the specified node
* @author Yanlong
* date:2017 August 31 10:34:55
* @param Path of path node
* @param data content
* @return
*/
public boolean writedata (String path,string data) {
try {
SYSTEM.OUT.PRINTLN (log_prefix_of_main+ "Update data succeeded, path:" +path+ ", stat:" +
This.zk.setData (Path, data.getbytes (),-1));
} catch (Exception e) {
E.printstacktrace ();
}
return false;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.