Skip to content

Instantly share code, notes, and snippets.

@rhusar
Created December 3, 2019 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhusar/eebbb49f03917b4a937cba6d8a1d8c89 to your computer and use it in GitHub Desktop.
Save rhusar/eebbb49f03917b4a937cba6d8a1d8c89 to your computer and use it in GitHub Desktop.
class SingletonService implements Service {
private Logger LOG = Logger.getLogger(this.getClass());
private Node node;
private Supplier<Group> groupSupplier;
private Consumer<Node> nodeConsumer;
SingletonService(Supplier<Group> groupSupplier, Consumer<Node> nodeConsumer) {
this.groupSupplier = groupSupplier;
this.nodeConsumer = nodeConsumer;
}
@Override
public void start(StartContext context) {
this.node = this.groupSupplier.get().getLocalMember();
this.nodeConsumer.accept(this.node);
LOG.infof("Singleton service is started on node '%s'.", this.node);
}
@Override
public void stop(StopContext context) {
LOG.infof("Singleton service is stopping on node '%s'.", this.node);
this.node = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment