All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jsync.app.ClusterApp Maven / Gradle / Ivy

There is a newer version: 1.10.13
Show newest version
package io.jsync.app;

import io.jsync.app.core.Cluster;
import io.jsync.app.core.Config;
import io.jsync.app.core.Manager;
import io.jsync.app.core.service.ClusterService;
import io.jsync.json.JsonObject;
import io.jsync.logging.Logger;
import io.jsync.logging.impl.LoggerFactory;
import org.apache.commons.lang3.ArrayUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * This class is used to create clustered applications using the jsync.io clustered app api.
 */
public abstract class ClusterApp {

    private static Logger logger = LoggerFactory.getLogger(ClusterApp.class);
    private static boolean initialized = false;
    private static ClusterApp instance;

    static {
        System.setProperty(LoggerFactory.LOGGER_PROPERTIES_FILE, "jsyncio-logging.properties");
        System.setProperty("java.net.preferIPv4Stack", "true");
    }

    // Create a new Cluster instance via new Cluster();
    private Cluster cluster = new Cluster();

    /**
     * This method returns the current ClusterApp instance. 

* There can only be one ClusterApp instance per JVM. * * @return ClusterApp instance */ public static ClusterApp activeInstance() { return instance; } public static boolean initialized() { return initialized && instance != null; } /** * This method allows you to start and initialize the specified ClusterApp instance.

* By adding "--join" in the arguments it will automatically the cluster. * * @param args the set of arguments you wish to use to initialize your ClusterApp * @throws Exception * @see io.jsync.app.core.Cluster */ public static void initialize(ClusterApp instance, final String... args) { Cluster cluster = instance.cluster; String configFile = "cluster.json"; Config config; if (ClusterApp.initialized) { logger.fatal("A ClusterApp instance has already been initialized. There can only be one ClusterApp instance per JVM."); return; } logger.info("Initializing a new instance..."); if (ArrayUtils.contains(args, "--config") && args.length > ArrayUtils.indexOf(args, "--config") + 1) { logger.debug("Using config file " + args[ArrayUtils.indexOf(args, "--config") + 1]); configFile = args[ArrayUtils.indexOf(args, "--config") + 1]; } if (configFile.equals("cluster.json")) { Path oldConfig = Paths.get("jcluster.json"); if (Files.exists(oldConfig) && !Files.exists(Paths.get(configFile))) { try { Files.copy(oldConfig, Paths.get(configFile), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { logger.error("Failed to copy old configuration to new location.", e); } } } config = Config.bind(cluster) .open(configFile); JsonObject clusterConfig = config.rawConfig().getObject("cluster"); if (ArrayUtils.contains(args, "--debug-enabled") && args.length > ArrayUtils.indexOf(args, "--debug-enabled") + 1) { logger.debug("Using cluster host " + args[ArrayUtils.indexOf(args, "--debug-enabled") + 1]); config.rawConfig().putBoolean("debug", Boolean.valueOf(args[ArrayUtils.indexOf(args, "--debug-enabled") + 1])); } if (ArrayUtils.contains(args, "--cluster-name") && args.length > ArrayUtils.indexOf(args, "--cluster-name") + 1) { logger.debug("Using cluster name " + args[ArrayUtils.indexOf(args, "--cluster-name") + 1]); clusterConfig.putString("name", args[ArrayUtils.indexOf(args, "--cluster-name") + 1]); } if (ArrayUtils.contains(args, "--cluster-password") && args.length > ArrayUtils.indexOf(args, "--cluster-password") + 1) { logger.debug("Using cluster password " + args[ArrayUtils.indexOf(args, "--cluster-password") + 1]); clusterConfig.putString("password", args[ArrayUtils.indexOf(args, "--cluster-password") + 1]); } if (ArrayUtils.contains(args, "--cluster-host") && args.length > ArrayUtils.indexOf(args, "--cluster-host") + 1) { logger.debug("Using cluster host " + args[ArrayUtils.indexOf(args, "--cluster-host") + 1]); clusterConfig.putString("host", args[ArrayUtils.indexOf(args, "--cluster-host") + 1]); } if (ArrayUtils.contains(args, "--cluster-role") && args.length > ArrayUtils.indexOf(args, "--cluster-role") + 1) { logger.debug("Using role " + args[ArrayUtils.indexOf(args, "--cluster-role") + 1]); config.setRole(args[ArrayUtils.indexOf(args, "--cluster-role") + 1]); } if (ArrayUtils.contains(args, "--persistence-type") && args.length > ArrayUtils.indexOf(args, "--persistence-type") + 1) { String type = args[ArrayUtils.indexOf(args, "--persistence-type") + 1]; logger.debug("Using cluster persistence_type " + type); clusterConfig.putString("persistence_type", type); } if (ArrayUtils.contains(args, "--set-hazelcast-mode") && args.length > ArrayUtils.indexOf(args, "--set-hazelcast-mode") + 1) { clusterConfig.putString("hazelcast_mode", String.valueOf(args[ArrayUtils.indexOf(args, "--set-hazelcast-mode") + 1])); } // Begin MongoDB Options JsonObject mongodbConfig = clusterConfig.getObject("mongo", new JsonObject()); if (ArrayUtils.contains(args, "--mongo-server-host") && args.length > ArrayUtils.indexOf(args, "--mongo-server-host") + 1) { String host = args[ArrayUtils.indexOf(args, "--mongo-server-host") + 1]; mongodbConfig.putString("server_host", host); } if (ArrayUtils.contains(args, "--mongo-server-port") && args.length > ArrayUtils.indexOf(args, "--mongo-server-port") + 1) { int port = Integer.parseInt(args[ArrayUtils.indexOf(args, "--mongo-server-port") + 1]); mongodbConfig.putNumber("server_port", port); } if (ArrayUtils.contains(args, "--mongo-set-loadall") && args.length > ArrayUtils.indexOf(args, "--mongo-set-loadall") + 1) { boolean loadAll = Boolean.parseBoolean(args[ArrayUtils.indexOf(args, "--mongo-set-loadall") + 1]); mongodbConfig.putBoolean("loadAll", loadAll); } if (ArrayUtils.contains(args, "--disable-mongodb") && args.length > ArrayUtils.indexOf(args, "--disable-mongodb") + 1) { if (Boolean.valueOf(args[ArrayUtils.indexOf(args, "--disable-mongodb") + 1])) { clusterConfig.removeField("mongo"); } } // End MongoDB Options clusterConfig.putObject("mongo", mongodbConfig); // Begin DynamoDB Options JsonObject dynamodbConfig = clusterConfig.getObject("dynamodb", new JsonObject()); if (ArrayUtils.contains(args, "--dynamo-access-id") && args.length > ArrayUtils.indexOf(args, "--dynamo-access-id") + 1) { String accessId = args[ArrayUtils.indexOf(args, "--dynamo-access-id") + 1]; dynamodbConfig.putString("access_id", accessId); } if (ArrayUtils.contains(args, "--dynamo-access-token") && args.length > ArrayUtils.indexOf(args, "--dynamo-access-token") + 1) { String accessId = args[ArrayUtils.indexOf(args, "--dynamo-access-token") + 1]; dynamodbConfig.putString("access_token", accessId); } if (ArrayUtils.contains(args, "--dynamo-region") && args.length > ArrayUtils.indexOf(args, "--dynamo-region") + 1) { String accessId = args[ArrayUtils.indexOf(args, "--dynamo-region") + 1]; dynamodbConfig.putString("region", accessId); } // End MongoDB Options clusterConfig.putObject("dynamodb", dynamodbConfig); if (ArrayUtils.contains(args, "--disable-dynamodb") && args.length > ArrayUtils.indexOf(args, "--disable-dynamodb") + 1) { if (Boolean.valueOf(args[ArrayUtils.indexOf(args, "--disable-dynamodb") + 1])) { clusterConfig.removeField("dynamodb"); } } config.rawConfig().putObject("cluster", clusterConfig); instance.prepareConfig(config); // We need to save some changes in case we need to update some stuff config.save(); if (ArrayUtils.contains(args, "--create-config")) { System.exit(0); return; } logger.info("Preparing new instance..."); instance.prepareCluster(cluster); ClusterApp.initialized = true; ClusterApp.instance = instance; // The data role is not checked since the only point is to be a part of the cluster and every so often sync all the data in the cluster // This allows you to automatically join the cluster without manually doing it. if (ArrayUtils.contains(args, "--join")) { logger.info("Joining the cluster with the config " + configFile); cluster.join(config); } } /** * This method is used to facilitate customizations to the Config used by the internal Cluster before it is started

* You can implement this to validate custom configurations or even set defaults.

* {@see #startInternal} automatically calls this method before saving the configuration file. * * @param config the configuration associated with the internal Cluster * @see io.jsync.app.core.Cluster */ protected abstract void prepareConfig(Config config); /** * This method is used to allow you to make changes to the internal Cluster before it is started.

* You can implement this to do things like add custom handlers to the {@see io.jsync.app.core.Manager}.

* {@see #startInternal} automatically calls this method after saving the configuration file.

* This is a great place to add {@see io.jsync.app.core.services.ClusterService}'s to the cluster also. * * @param cluster the Cluster that you are preparing * @see Manager * @see ClusterService */ protected abstract void prepareCluster(Cluster cluster); /** * This returns the internal Cluster associated with the ClusterApp instance. * * @return Cluster instance associated */ final public Cluster cluster() { if (cluster == null) { cluster = new Cluster(); } return cluster; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy