Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以使用其他类中的静态方法吗?_Java_Static_Apache Zookeeper_Apache Curator - Fatal编程技术网

Java 我可以使用其他类中的静态方法吗?

Java 我可以使用其他类中的静态方法吗?,java,static,apache-zookeeper,apache-curator,Java,Static,Apache Zookeeper,Apache Curator,下面是我的CuratorClient类,它连接到Zookeeper并开始领导人选举过程 public class CuratorClient { // can I make this as static? private static CuratorFramework client; private String latchPath; private String id; private LeaderLatch leaderLatch; pub

下面是我的
CuratorClient
类,它连接到Zookeeper并开始领导人选举过程

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

一般来说,这不是馆长或动物园管理员的问题。这基本上是一个设计问题,我试图了解我的做法是否会有任何问题?我假设
CuratorFramework
也将是线程安全的?

是的,您可以从其他类调用
静态方法

您的签名如下所示:

protected static List<String> getChildren(String node) throws Exception
受保护的静态列表getChildren(字符串节点)引发异常
无法从其他类调用此函数的原因是,它是受保护的(对当前类和子类可见),而不是对公共的(对任何地方可见)

如果使其可见,可以使用
CuratorClient.getChildren()
调用它

.

.

你为什么不呢?实用程序类都是这样构建的。编辑:当然方法和类应该是公共的。您不能从另一个类(不是子类)调用该方法,因为该方法受
保护
protected
方法只能从所属类或所属类的子类调用。当然,如果您能够调用该方法(因为它被声明为
public
),它仍将引用
CuratorClient
客户端
静态字段,这可能是你想要的,也可能不是你想要的。@HotLicks:这是我不确定的-
CuratorClient的静态字段,这可能是你想要的,也可能不是你想要的。
这让我很困惑。我的主要工作是使用管理员库从Zookeeper获取特定节点的子节点,我正在构建这个实用程序,这样我就可以通过传递需要获取的节点子节点从不同的类调用它。如果不仔细研究应用程序,很难调用。感谢Jeroen的建议。。现在,如果我这样称呼它,那么当它被声明为静态时,我会有任何问题吗?而
getChildren
方法将从Zookeeper模式中获取节点的子节点?我的头脑中还没有构建编译器,所以最好的测试方法是执行您的程序,看看会发生什么。在观察结果之后,你可以研究可能的原因并得出结论。将字段设为
static
意味着对象的每个实例都将引用
static
字段中对象的同一实例。有时这是你想要的,有时不是:这取决于你。我不可能知道
getChildren()
会做什么,因为我没有这方面的代码(执行它,你会看到它的功能)。读这篇文章时,它说
CuratorFramework
实例是线程安全的。所以我想我可以用它静态的感觉,对吗?请阅读该链接-
重要提示:CuratorFramework实例是完全线程安全的。在应用程序中,每个ZooKeeper集群应共享一个CuratorFramework。
protected static List<String> getChildren(String node) throws Exception