
io.jsync.app.core.Cluster Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsync.io Show documentation
Show all versions of jsync.io Show documentation
jsync.io is a non-blocking, event-driven networking framework for Java
package io.jsync.app.core;
import com.hazelcast.core.HazelcastInstance;
import io.jsync.Async;
import io.jsync.AsyncFactory;
import io.jsync.Handler;
import io.jsync.VoidHandler;
import io.jsync.app.core.service.ClusterService;
import io.jsync.eventbus.EventBus;
import io.jsync.impl.ConcurrentHashSet;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* This is the main control centre of the cluster.
* This exposes everything you need to build a ClusterApp and communicate within the cluster.
*/
public class Cluster {
protected Config config;
protected Async clusteredAsync;
private Async localAsync = AsyncFactory.newAsync();
private Logger logger = new Logger();
private Set clusterServices = new LinkedHashSet<>();
private Set startedServices = new ConcurrentHashSet<>();
private Manager manager;
private Data data;
private boolean joined = false;
/**
* IMPORTANT: Using Cluster manually is highly frowned upon.
*/
public Cluster() {
manager = new Manager(this);
data = new Data(this);
}
/**
* This method is used to join the cluster.
*
* When calling {@see io.jsync.app.ClusterApp#initialize()} you can pass the parameter "--join" to tell ClusterApp to
* automatically do this as that is the recommended usage.
*
* @param config the Config you wish use for the cluster
* @param preJoinHandler this can be used to execute code right before the cluster joins
*/
public void join(Config config, Handler preJoinHandler) {
if (joined) {
logger.fatal("We have already joined the cluster.. we cannot not join it again.");
return;
}
this.config = config;
if (preJoinHandler != null) {
preJoinHandler.handle(null);
}
manager.start();
joined = true;
}
/**
* @param config the Config you wish use for the cluster
* @see io.jsync.app.core.Cluster#join(Config, io.jsync.Handler)
*/
public void join(Config config) {
join(config, null);
}
protected void join() {
join(config, null);
}
/**
* You can call this method to leave the Cluster.
*
* This method exists to properly shut down the cluster when needed.
*/
public void leave() {
if (!joined) {
logger.info("We have not joined the cluster.. we will not leave anything.");
return;
}
manager.stop();
}
public String addService(ClusterService service) throws Exception {
if (service == null) {
throw new NullPointerException();
}
if (service.name() == null) {
throw new NullPointerException();
}
if (containsService(service.name())) {
throw new Exception("The service with the name " + service.name() + " already exists in this cluster instance");
}
clusterServices.add(service);
return service.name();
}
public void removeService(String name) throws Exception {
if (!containsService(name)) {
throw new Exception("The service with the name " + name + " does not exist in this cluster instance");
}
ClusterService service = getService(name);
stopService(service);
clusterServices.remove(service);
}
public boolean containsService(String name) {
for (ClusterService service : clusterServices) {
if (service.name().equals(name)) {
return true;
}
}
return false;
}
public ClusterService getService(String name) throws Exception {
if (!containsService(name)) {
return null;
}
for (ClusterService service : clusterServices) {
if (service.name().equals(name)) {
return service;
}
}
return null;
}
protected void startService(ClusterService service) {
if (startedServices.contains(service.name())) return;
try {
service.start(this);
startedServices.add(service.name());
} catch (Exception e) {
throw new RuntimeException("Error starting the service " + service.name(), e);
}
}
protected void stopService(ClusterService service) {
if (!startedServices.contains(service.name())) return;
try {
service.stop();
} catch (Exception e) {
logger.error("Error stopping the service " + service.name(), e);
}
startedServices.remove(service.name());
}
protected void startServices() {
logger.info("Starting cluster services...");
for (ClusterService service : clusterServices) {
logger.info("Starting service: " + service.name());
startService(service);
}
}
protected void stopServices() {
logger.info("Stopping services...");
for (ClusterService service : clusterServices) {
logger.info("Stopping service: " + service.name());
stopService(service);
}
}
public Config config() {
return config;
}
public Logger logger() {
return logger;
}
public Manager manager() {
return manager;
}
protected Async localAsync() {
return localAsync;
}
public HazelcastInstance hazelcast() {
return manager.hazelcast();
}
public Async async() {
return clusteredAsync;
}
public EventBus eventBus() {
return clusteredAsync.eventBus();
}
public Data data() {
return data;
}
public String identifier() {
return "cluster.app." + config.clusterName();
}
public boolean ready() {
return clusteredAsync != null && manager.hazelcast() != null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy