[英]Can I use static method from a different class?
Below is my CuratorClient
class which is connecting to Zookeeper and starting the leader election process as well. 下面是我的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);
}
}
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. 当我的服务启动时,在静态块中,我正在使用CuratorClient与Zookeeper建立连接,并也开始了领导者选举过程。
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");
现在,我还有其他一些类喜欢使用CuratorClient
的getChildren
方法,因此在此类中,我可以像这样直接使用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? 我假设CuratorFramework
也将是线程安全的吗?
Yes, you can call static
methods from other classes. 是的,您可以从其他类调用static
方法。
Your signature looks like this: 您的签名如下所示:
protected static List<String> getChildren(String node) throws Exception
The reason you can't call this from another class is because it's protected
(visible to the current class and subclasses) instead of public
(visible to everywhere). 之所以不能从另一个类中调用它,是因为它是protected
(对当前类和子类可见),而不是public
(对任何地方都可见)。
If you make it visible you can call it with CuratorClient.getChildren()
. 如果使其可见,则可以使用CuratorClient.getChildren()
进行调用。
More information on access modifiers . 有关访问修饰符的更多信息 。
More information on class members ( static
fields) . 有关类成员的更多信息( static
字段) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.