Skip to content

Instantly share code, notes, and snippets.

@rhusar
Created August 21, 2019 13:49
Show Gist options
  • Save rhusar/fc5840c5098103657873c7d8f6159475 to your computer and use it in GitHub Desktop.
Save rhusar/fc5840c5098103657873c7d8f6159475 to your computer and use it in GitHub Desktop.
public class CustomSingletonElectionListener implements SingletonElectionListener {
@Override
public void elected(List<Node> candidateMembers, Node electedMember) {
System.out.println("New singleton election: elected member " + electedMember + " from candidates " + candidateMembers);
}
}
public class SingletonServiceActivator implements ServiceActivator {
private static final String CONTAINER_NAME = "server";
public static final ServiceName SERVICE_NAME = ServiceName.JBOSS.append("example", "service", "default");
public static final String PREFERRED_NODE = "node-1";
@Override
public void activate(ServiceActivatorContext context) {
SingletonServiceConfiguratorFactory factory = new ActiveServiceSupplier<SingletonServiceConfiguratorFactory>(context.getServiceRegistry(), ServiceName.parse(SingletonDefaultCacheRequirement.SINGLETON_SERVICE_CONFIGURATOR_FACTORY.resolve(CONTAINER_NAME))).get();
ServiceTarget target = context.getServiceTarget();
SingletonElectionListener listener = new CustomSingletonElectionListener();
factory.createSingletonServiceConfigurator(SERVICE_NAME.append("singleton"))
.electionPolicy(new PreferredSingletonElectionPolicy(new SimpleSingletonElectionPolicy(), new NamePreference(PREFERRED_NODE)))
.electionListener(listener)
.build(target)
.install();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment