use of com.hortonworks.registries.common.ha.LeadershipParticipant in project registry by hortonworks.
the class RegistryApplication method registerHA.
private void registerHA(HAConfiguration haConfiguration, Environment environment) throws Exception {
if (haConfiguration != null) {
environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
@Override
public void serverStarted(Server server) {
String serverUrl = server.getURI().toString();
LOG.info("Received callback as server is started with server URL:[{}]", server);
LOG.info("HA configuration: [{}]", haConfiguration);
String className = haConfiguration.getClassName();
LeadershipParticipant leadershipParticipant = null;
try {
leadershipParticipant = (LeadershipParticipant) Class.forName(className).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
leadershipParticipant.init(haConfiguration.getConfig(), serverUrl);
leadershipParticipantRef.set(leadershipParticipant);
LOG.info("Registering for leadership with participant [{}]", leadershipParticipant);
try {
leadershipParticipantRef.get().participateForLeadership();
} catch (Exception e) {
LOG.error("Error occurred while participating for leadership with serverUrl [{}]", serverUrl, e);
throw new RuntimeException(e);
}
LOG.info("Registered for leadership with participant [{}]", leadershipParticipant);
}
});
} else {
leadershipParticipantRef.set(LocalLeader.getInstance());
LOG.info("No HA configuration exists, using [{}]", leadershipParticipantRef);
}
}
Aggregations