Search in sources :

Example 1 with ServiceBrokerConfig

use of services.moleculer.config.ServiceBrokerConfig in project moleculer-java by moleculer-java.

the class DefaultEventbus method started.

// --- START EVENT BUS ---
/**
 * Initializes default EventBus instance.
 *
 * @param broker
 *            parent ServiceBroker
 * @param config
 *            optional configuration of the current component
 */
@Override
public void started(ServiceBroker broker) throws Exception {
    super.started(broker);
    // Set nodeID
    this.nodeID = broker.getNodeID();
    // Set components
    ServiceBrokerConfig cfg = broker.getConfig();
    this.strategy = cfg.getStrategyFactory();
    this.transporter = cfg.getTransporter();
    this.executor = cfg.getExecutor();
}
Also used : ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig)

Example 2 with ServiceBrokerConfig

use of services.moleculer.config.ServiceBrokerConfig in project moleculer-java by moleculer-java.

the class DefaultCircuitBreaker method started.

// --- START BREAKER ---
@Override
public void started(ServiceBroker broker) throws Exception {
    super.started(broker);
    // Set components
    ServiceBrokerConfig cfg = broker.getConfig();
    this.serviceRegistry = cfg.getServiceRegistry();
    this.contextFactory = cfg.getContextFactory();
}
Also used : ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig)

Example 3 with ServiceBrokerConfig

use of services.moleculer.config.ServiceBrokerConfig in project moleculer-java by moleculer-java.

the class RedisCacher method started.

/**
 * Initializes cacher instance.
 *
 * @param broker
 *            parent ServiceBroker
 * @param config
 *            optional configuration of the current component
 */
@Override
public void started(ServiceBroker broker) throws Exception {
    super.started(broker);
    logger.info(nameOf(this, true) + " will use " + nameOf(serializer, true) + '.');
    // Get components
    ServiceBrokerConfig cfg = broker.getConfig();
    executor = cfg.getExecutor();
    scheduler = cfg.getScheduler();
    // Connect to Redis server
    connect();
}
Also used : ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig)

Example 4 with ServiceBrokerConfig

use of services.moleculer.config.ServiceBrokerConfig in project moleculer-java by moleculer-java.

the class NodeService method started.

// --- START SERVICE ---
@Override
public void started(ServiceBroker broker) throws Exception {
    super.started(broker);
    // Set components
    ServiceBrokerConfig cfg = broker.getConfig();
    this.transporter = cfg.getTransporter();
    this.monitor = cfg.getMonitor();
    this.localNodeID = broker.getNodeID();
}
Also used : ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig)

Example 5 with ServiceBrokerConfig

use of services.moleculer.config.ServiceBrokerConfig in project moleculer-java by moleculer-java.

the class ClusterTest method run.

public void run() {
    System.out.println("START");
    try {
        // Load Sigar DLLs
        String nativeDir = "./native";
        System.setProperty("java.library.path", nativeDir);
        ExecutorService executor = Executors.newFixedThreadPool(Math.min(NODES, 100));
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(Math.min(NODES, 100));
        String[] urls = new String[NODES];
        for (int i = 0; i < NODES; i++) {
            int port = 6000 + i;
            urls[i] = "tcp://127.0.0.1:" + port + "/node-" + i;
        }
        ServiceBroker[] brokers = new ServiceBroker[NODES];
        for (int i = 0; i < NODES; i++) {
            // urls
            TcpTransporter transporter = new TcpTransporter();
            transporter.setGossipPeriod(3);
            transporter.setDebug(false);
            transporter.setOfflineTimeout(0);
            ServiceBrokerConfig settings = new ServiceBrokerConfig();
            settings.setShutDownThreadPools(false);
            settings.setExecutor(executor);
            settings.setScheduler(scheduler);
            settings.setTransporter(transporter);
            settings.setNodeID("node-" + i);
            ServiceBroker broker = new ServiceBroker(settings);
            brokers[i] = broker;
        }
        for (int i = 0; i < NODES; i++) {
            brokers[i].start();
            System.out.println("node-" + i + " started.");
        }
        boolean turnOn = true;
        HashMap<String, Long> maxSeqs = new HashMap<>();
        ServiceBroker broker;
        TcpTransporter transporter;
        int counter = 0;
        while (true) {
            Thread.sleep(1000);
            counter++;
            if (counter % 70 == 0) {
                if (turnOn) {
                    for (int n = 0; n < NODES / 3; n++) {
                        int i = rnd.nextInt(NODES);
                        broker = brokers[i];
                        transporter = (TcpTransporter) broker.getConfig().getTransporter();
                        if (transporter.writer == null) {
                            transporter.connect();
                        }
                    }
                }
                int i = rnd.nextInt(NODES);
                broker = brokers[i];
                transporter = (TcpTransporter) broker.getConfig().getTransporter();
                if (transporter.writer != null) {
                    transporter.nodes.clear();
                    transporter.disconnect();
                }
            }
            maxSeqs.clear();
            for (int i = 0; i < NODES; i++) {
                broker = brokers[i];
                transporter = (TcpTransporter) broker.getConfig().getTransporter();
                maxSeqs.put(broker.getNodeID(), transporter.getDescriptor().cpuSeq);
            }
            for (int i = 0; i < NODES; i++) {
                broker = brokers[i];
                transporter = (TcpTransporter) broker.getConfig().getTransporter();
                for (int n = 0; n < NODES; n++) {
                    NodeDescriptor d = transporter.nodes.get("node-" + n);
                    boolean online = transporter.isOnline("node-" + n);
                    Color color;
                    Long seq = null;
                    if (online) {
                        if (d != null) {
                            long cpuSeq = d.cpuSeq;
                            long maxSeq = maxSeqs.get("node-" + n);
                            if (maxSeq > 0) {
                                if (maxSeq < cpuSeq) {
                                    maxSeq = cpuSeq;
                                    maxSeqs.put("node-" + n, maxSeq);
                                }
                                long c = Math.max(0, 255 - 20 * (maxSeq - cpuSeq));
                                color = new Color(0, (int) c, 0);
                            } else {
                                color = Color.WHITE;
                            }
                            seq = d.seq;
                        } else {
                            color = Color.YELLOW;
                        }
                    } else {
                        color = Color.RED;
                        if (d != null) {
                            seq = d.seq;
                        }
                    }
                    image.draw(n, i, color, seq);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("STOP");
    System.exit(0);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HashMap(java.util.HashMap) Color(java.awt.Color) NodeDescriptor(services.moleculer.transporter.tcp.NodeDescriptor) ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig) ServiceBroker(services.moleculer.ServiceBroker) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

ServiceBrokerConfig (services.moleculer.config.ServiceBrokerConfig)9 Tree (io.datatree.Tree)1 Color (java.awt.Color)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ServiceBroker (services.moleculer.ServiceBroker)1 Cache (services.moleculer.cacher.Cache)1 CallOptions (services.moleculer.context.CallOptions)1 Context (services.moleculer.context.Context)1 DefaultContextFactory (services.moleculer.context.DefaultContextFactory)1 Listener (services.moleculer.eventbus.Listener)1 Subscribe (services.moleculer.eventbus.Subscribe)1 Action (services.moleculer.service.Action)1 Dependencies (services.moleculer.service.Dependencies)1 Middleware (services.moleculer.service.Middleware)1 Name (services.moleculer.service.Name)1 Service (services.moleculer.service.Service)1 Version (services.moleculer.service.Version)1 NodeDescriptor (services.moleculer.transporter.tcp.NodeDescriptor)1