Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException for ZookeeperAutoServiceRegistration #310

Open
ivan-zaitsev opened this issue Feb 5, 2023 · 1 comment
Open

NullPointerException for ZookeeperAutoServiceRegistration #310

ivan-zaitsev opened this issue Feb 5, 2023 · 1 comment

Comments

@ivan-zaitsev
Copy link

ivan-zaitsev commented Feb 5, 2023

ZookeeperAutoServiceRegistration throws an exception when ServiceInstanceRegistration method getServiceInstance() is called before ZookeeperAutoServiceRegistration method getPort()

It happens because method getServiceInstance() calls build() and when getPort() will be called it will return null instead 0.

public ServiceInstance<ZookeeperInstance> getServiceInstance() {
if (this.serviceInstance == null) {
build();
}
return this.serviceInstance;
}


public int getPort() {
if (this.serviceInstance == null) {
return 0;
}
return this.serviceInstance.getPort();
}

protected void register() {
if (!this.properties.isRegister()) {
log.debug("Registration disabled.");
return;
}
if (this.registration.getPort() == 0) {
this.registration.setPort(getPort().get());
}
super.register();
}

Also getInstanceId() method returns null.

Use case when we need to call getServiceInstance for example to create leader candidate and associate with registration id for further understanding which node is a leader.

@Bean
public DefaultCandidate bean(ServiceInstanceRegistration registration) {
    return return new DefaultCandidate(registration.getServiceInstance().getId(), "default");
}
@ivan-zaitsev
Copy link
Author

ivan-zaitsev commented Feb 5, 2023

As workaround for fixing this: before calling registration.getServiceInstance() this line should be called registration.setPort(0);

For example:

@Bean
public DefaultCandidate bean(ServiceInstanceRegistration registration) {
    registration.setPort(0);
    return return new DefaultCandidate(registration.getServiceInstance().getId(), "default");
}

Not so elegant solution, but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants