curator-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Check Peck <comptechge...@gmail.com>
Subject Can I access CuratorFramework in static way from other class?
Date Thu, 20 Feb 2014 20:04:23 GMT
Below is my `CuratorClient` class which is connecting to Zookeeper and
starting the leader election process as well.



                public class CuratorClient {



                                // can I make this as static?

                                private static CuratorFramework client;

                                private String latchPath;

                                private String id;

                                private LeaderLatch leaderLatch;



                                public CuratorClient(String connString,
String latchPath, String id) {

                                                client =
CuratorFrameworkFactory.newClient(connString, new
ExponentialBackoffRetry(1000, Integer.MAX_VALUE));

                                                this.id = id;

                                                this.latchPath = latchPath;

                                }



                                public void start() throws Exception {

                                                client.start();


client.getCuratorClient().blockUntilConnectedOrTimedOut();

                                                leaderLatch = new
LeaderLatch(client, latchPath, id);

                                                leaderLatch.start();

                                }



                                public boolean isLeader() {

                                                return
leaderLatch.hasLeadership();

                                }



                                public Participant currentLeader() throws
Exception {

                                                return
leaderLatch.getLeader();

                                }



                                public void close() throws IOException {

                                                leaderLatch.close();

                                                client.close();

                                }



                                // can I use below method from any other
class ?

                                protected static List<String>
getChildren(String node) throws Exception {

                                                return
client.getChildren().forPath(node);

                                }

                }



When my service gets started up, in the static block I am making a
connection to Zookeeper using CuratorClient and starting the leader
election process as well.



                public class TestService {



                    private static CuratorClient curatorClient = null;

                                static {



                                                try {

                                                                String
connectionString = "some-string";

                                                                String
hostname = "machineA";



                                                                curatorClient
= new CuratorClient(connectionString, "/my/latch", hostname);


curatorClient.start();



                                                } catch (Exception ex) {

                                                                // log
exception

                                                }

                                }



                                ....

                                ....



                                // some method

                                public Map<String, String>
installNewSoftware(String node) {



                                //.. some other code

                                try {

                                                List<String> children =
CuratorClient.getChildren("/my/example");


System.out.println(children);



                                } catch (Exception e) {

                                                e.printStackTrace();

                                }



                                //.. some other code

                                return null;

                                }

                }



Now I have some other class as well which likes to use the `getChildren`
method of `CuratorClient` so in this class, I can directly use like this
`CuratorClient.getChildren("/my/example");` correct?



                public class DifferentClass {



                                ....

                                ....



                                // some new method

                                public Map<String, String>
installNewSoftware(String node) {

                                try {

                                                List<String> children =
CuratorClient.getChildren("/my/example");


System.out.println(children);



                                } catch (Exception e) {

                                                e.printStackTrace();

                                }



                                //.. some other code

                                return null;

                                }

                }



I am trying to understand whether the way I am doing it will have any
problem or not if I use CuratorFramework as static and provide all kind of
utilities in CuratorClient class to get the children of a node or get the
data of a node and call these methods in a static way from other class?


And I am assuming `CuratorFramework` will be thread safe as well?

Mime
View raw message