1

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;
    }
}

In general, this is not a curator question or zookeeper question. It's basically a design question and I am trying to understand whether the way I am doing it will have any problem or not? And I am assuming CuratorFramework will be thread safe as well?

4

1 回答 1

2

是的,您可以调用static其他类的方法。

您的签名如下所示:

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

您不能从另一个类调用它的原因是因为它protected(对当前类和子类可见)而不是public(对任何地方可见)。

如果你让它可见,你可以用CuratorClient.getChildren().

有关访问修饰符的更多信息
有关类成员(static字段)的更多信息。

于 2014-02-20T19:48:05.830 回答