zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kumuduharshani <kumuduharsh...@gmail.com>
Subject Simple Java class to talk to ZooKeeper
Date Sun, 01 Apr 2012 05:10:28 GMT
HI..

I am new to Zookeeper and tried reading many tutorials and got a basic
knowledge on it.. I need to an assignment with ZooKeeper, but before I was
just trying to do basic connection to zookeeper via a simple java class.
But, it giving a exception when connecting. This is how i did,,,

[On Windows]
Started ZooKeeper on terminal - zkServer.cmd (it is running now), verified
via zkCli.cmd too.

Written following simple java class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author v-kusama
 */

import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.*;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper.States;

public class zkConnector implements Watcher{
    
    static ZooKeeper zooKeeper ;

    static Integer mutex;
    
     java.util.concurrent.CountDownLatch connectedSignal = new
java.util.concurrent.CountDownLatch(1);
    
    String root;

    public void connect() throws KeeperException, IOException,
InterruptedException
    {
        zooKeeper = new ZooKeeper("localhost:2181", 3000, 
                new Watcher(){

                @Override
                public void process(WatchedEvent event) {
                    if (event.getState() == KeeperState.SyncConnected) {
                                connectedSignal.countDown();
                            }
                }
                    
                });
    }
    
    public static void main (String args[]) throws KeeperException,
IOException, InterruptedException
    {
        zkConnector zk = new zkConnector();
        zk.connect();
        
        String lock1 = zooKeeper.create("/locks", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("Done" + lock1);

        String lock2 = zooKeeper.create("/locks/lock", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
System.out.println("Done" + lock2);

        List children = zooKeeper.getChildren("/locks", false);
        for(Object child:children){
        System.out.println("child:" + child);
        } 
        
    }

    @Override
    public void process(WatchedEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}

#Code compiled well
#Run the class, but failed with an exception
run:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/slf4j/LoggerFactory
	at org.apache.zookeeper.ZooKeeper.<clinit>(ZooKeeper.java:93)
	at zkConnector.connect(zkConnector.java:29)
	at zkConnector.main(zkConnector.java:45)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	... 3 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)


#Exception points to following line in connect() method:
zooKeeper = new ZooKeeper("localhost:2181", 3000, 


Am i missing something.. how to get this done to talk to zookeeper server...


thanks in advance
Kumudu....


--
View this message in context: http://zookeeper-user.578899.n2.nabble.com/Simple-Java-class-to-talk-to-ZooKeeper-tp7426147p7426147.html
Sent from the zookeeper-user mailing list archive at Nabble.com.

Mime
View raw message