public class MySingletonElectionListener implements SingletonElectionListener {
@Override
public void elected(List<Node> candidates, Node primary) {
// ...
}
}
public class MyServiceActivator implements ServiceActivator {
@Override
public void activate(ServiceActivatorContext context) {
String containerName = "foo";
SingletonElectionPolicy policy = new MySingletonElectionPolicy();
SingletonElectionListener listener = new MySingletonElectionListener();
int quorum = 3;
ServiceName name = ServiceName.parse("my.service.name");
// Use a SingletonServiceConfiguratorFactory backed by default cache of "foo" container
Supplier<SingletonServiceConfiguratorFactory> factory = new ActiveServiceSupplier<SingletonServiceConfiguratorFactory>(context.getServiceRegistry(), ServiceName.parse(SingletonDefaultCacheRequirement.SINGLETON_SERVICE_CONFIGURATOR_FACTORY.resolve(containerName)));
ServiceBuilder<?> builder = factory.get().createSingletonServiceConfigurator(name)
.electionListener(listener)
.electionPolicy(policy)
.requireQuorum(quorum)
.build(context.getServiceTarget());
Service service = new MyService();
builder.setInstance(service).install();
}
}
public class MySingletonElectionListener implements SingletonElectionListener {
@Override
public void elected(List<Node> candidates, Node primary) {
// ...
}
}
public class MyServiceActivator implements ServiceActivator {
@Override
public void activate(ServiceActivatorContext context) {
String containerName = "foo";
SingletonElectionPolicy policy = new MySingletonElectionPolicy();
SingletonElectionListener listener = new MySingletonElectionListener();
int quorum = 3;
ServiceName name = ServiceName.parse("my.service.name");
// Use a SingletonServiceConfiguratorFactory backed by default cache of "foo" container
Supplier<SingletonServiceConfiguratorFactory> factory = new ActiveServiceSupplier<SingletonServiceConfiguratorFactory>(context.getServiceRegistry(), ServiceName.parse(SingletonDefaultCacheRequirement.SINGLETON_SERVICE_CONFIGURATOR_FACTORY.resolve(containerName)));
ServiceBuilder<?> builder = factory.get().createSingletonServiceConfigurator(name)
.electionListener(listener)
.electionPolicy(policy)
.requireQuorum(quorum)
.build(context.getServiceTarget());
Service service = new MyService();
builder.setInstance(service).install();
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
class QueryingService implements Service {
private Logger LOG = Logger.getLogger(this.getClass());
private ScheduledExecutorService executor;
@Override
public void start(StartContext context) throws {
LOG.info("Querying service is starting.");
executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> {
Supplier<Node> node = new PassiveServiceSupplier<>(context.getController().getServiceContainer(), SingletonServiceActivator.SINGLETON_SERVICE_NAME);
if (node.get() != null) {
LOG.infof("Singleton service is running on this (%s) node.", node.get());
} else {
LOG.infof("Singleton service is not running on this node.");
}
}, 5, 5, TimeUnit.SECONDS);
}
@Override
public void stop(StopContext context) {
LOG.info("Querying service is stopping.");
executor.shutdown();
}
}
class QueryingService implements Service {
private Logger LOG = Logger.getLogger(this.getClass());
private ScheduledExecutorService executor;
@Override
public void start(StartContext context) throws {
LOG.info("Querying service is starting.");
executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> {
Supplier<Node> node = new PassiveServiceSupplier<>(context.getController().getServiceContainer(), SingletonServiceActivator.SINGLETON_SERVICE_NAME);
if (node.get() != null) {
LOG.infof("Singleton service is running on this (%s) node.", node.get());
} else {
LOG.infof("Singleton service is not running on this node.");
}
}, 5, 5, TimeUnit.SECONDS);
}
@Override
public void stop(StopContext context) {
LOG.info("Querying service is stopping.");
executor.shutdown();
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
实施 SingletonServiceActivator 类,以构建和安装单例服务和查询服务。
public class SingletonServiceActivator implements ServiceActivator {
private final Logger LOG = Logger.getLogger(SingletonServiceActivator.class);
static final ServiceName SINGLETON_SERVICE_NAME =
ServiceName.parse("org.jboss.as.quickstarts.ha.singleton.service");
private static final ServiceName QUERYING_SERVICE_NAME =
ServiceName.parse("org.jboss.as.quickstarts.ha.singleton.service.querying");
@Override
public void activate(ServiceActivatorContext serviceActivatorContext) {
SingletonPolicy policy = new ActiveServiceSupplier<SingletonPolicy>(
serviceActivatorContext.getServiceRegistry(),
ServiceName.parse(SingletonDefaultRequirement.POLICY.getName())).get();
ServiceTarget target = serviceActivatorContext.getServiceTarget();
ServiceBuilder<?> builder = policy.createSingletonServiceConfigurator(SINGLETON_SERVICE_NAME).build(target);
Consumer<Node> member = builder.provides(SINGLETON_SERVICE_NAME);
Supplier<Group> group = builder.requires(ServiceName.parse("org.wildfly.clustering.default-group"));
builder.setInstance(new SingletonService(group, member)).install();
serviceActivatorContext.getServiceTarget()
.addService(QUERYING_SERVICE_NAME, new QueryingService())
.setInitialMode(ServiceController.Mode.ACTIVE)
.install();
serviceActivatorContext.getServiceTarget().addService(QUERYING_SERVICE_NAME).setInstance(new QueryingService()).install();
LOG.info("Singleton and querying services activated.");
}
}
public class SingletonServiceActivator implements ServiceActivator {
private final Logger LOG = Logger.getLogger(SingletonServiceActivator.class);
static final ServiceName SINGLETON_SERVICE_NAME =
ServiceName.parse("org.jboss.as.quickstarts.ha.singleton.service");
private static final ServiceName QUERYING_SERVICE_NAME =
ServiceName.parse("org.jboss.as.quickstarts.ha.singleton.service.querying");
@Override
public void activate(ServiceActivatorContext serviceActivatorContext) {
SingletonPolicy policy = new ActiveServiceSupplier<SingletonPolicy>(
serviceActivatorContext.getServiceRegistry(),
ServiceName.parse(SingletonDefaultRequirement.POLICY.getName())).get();
ServiceTarget target = serviceActivatorContext.getServiceTarget();
ServiceBuilder<?> builder = policy.createSingletonServiceConfigurator(SINGLETON_SERVICE_NAME).build(target);
Consumer<Node> member = builder.provides(SINGLETON_SERVICE_NAME);
Supplier<Group> group = builder.requires(ServiceName.parse("org.wildfly.clustering.default-group"));
builder.setInstance(new SingletonService(group, member)).install();
serviceActivatorContext.getServiceTarget()
.addService(QUERYING_SERVICE_NAME, new QueryingService())
.setInitialMode(ServiceController.Mode.ACTIVE)
.install();
serviceActivatorContext.getServiceTarget().addService(QUERYING_SERVICE_NAME).setInstance(new QueryingService()).install();
LOG.info("Singleton and querying services activated.");
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow