Я пытаюсь сделать класс как ThreadSafe Singleton
, но почему-то я не могу понять, как сделать класс ThreadSafe Singleton
, который может принимать параметр.Как сделать потокобезопасный одноэлементный класс, который может принимать параметр?
Ниже класс, который я использую из этого GitHub link, который я использую в настоящее время, чтобы сделать подключение к Zookeeper -
public class LeaderLatchExample {
private CuratorFramework client;
private String latchPath;
private String id;
private LeaderLatch leaderLatch;
public LeaderLatchExample(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.getZookeeperClient().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();
}
public CuratorFramework getClient() {
return client;
}
public String getLatchPath() {
return latchPath;
}
public String getId() {
return id;
}
public LeaderLatch getLeaderLatch() {
return leaderLatch;
}
}
И это путь, я звоню выше класс -
public static void main(String[] args) throws Exception {
String latchPath = "/latch";
String connStr = "10.12.136.235:2181";
LeaderLatchExample node1 = new LeaderLatchExample(connStr, latchPath, "node-1"); // this I will be doing only one time at just the initialization time
node1.start();
System.out.println("now node-1 think the leader is " + node1.currentLeader());
}
Теперь мне нужно, если я вызываю эти два метода ниже из любого класса в моей программе, я должен иметь возможность получить его экземпляр. Поэтому я собираюсь сделать выше класс как Thread Safe Singleton, чтобы я мог получить доступ к этим двум методам во всей моей java-программе.
isLeader()
getClient()
Как сделать выше класс, как поточно-одноплодной, а затем использовать isLeader()
и getClient()
во всех моих классов, чтобы увидеть, кто является лидером и получить экземпляр клиента ..
мне нужно сделать это только во время инициализации, и как только это будет сделано, я смогу использовать isLeader()
и getClient()
на всех моих классах. Можно ли это сделать?
// this line I will be doing only one time at just the initialization time
LeaderLatchExample node1 = new LeaderLatchExample(connStr, latchPath, "node-1");
node1.start();
Это больше Java вопроса не Zookeeper материала ..
Есть ли что-нибудь, что вам нужно, помимо шаблонов, перечисленных на http://stackoverflow.com/q/7048198/2636001? – dst