Search in sources :

Example 1 with NodeService

use of services.moleculer.internal.NodeService in project moleculer-java by moleculer-java.

the class ServiceBroker method start.

// --- START BROKER INSTANCE ---
/**
 * Start broker. If has a Transporter, transporter.connect() will be called.
 */
public void start() throws Exception {
    // Check state
    if (serviceRegistry != null) {
        throw new IllegalStateException("Moleculer Service Broker has already been started!");
    }
    try {
        // Start internal components, services, middlewares...
        logger.info("Starting Moleculer Service Broker (version " + SOFTWARE_VERSION + ")...");
        // Set internal components
        uidGenerator = start(config.getUidGenerator());
        strategyFactory = start(config.getStrategyFactory());
        contextFactory = start(config.getContextFactory());
        circuitBreaker = start(config.getCircuitBreaker());
        eventbus = start(config.getEventbus());
        serviceRegistry = start(config.getServiceRegistry());
        transporter = start(config.getTransporter());
        // Register enqued middlewares
        Cacher cacher = config.getCacher();
        if (cacher != null) {
            logger.info(nameOf(cacher, true) + " started.");
            middlewares.add(cacher);
        }
        serviceRegistry.use(middlewares);
        // Install internal services
        if (config.isInternalServices()) {
            services.put("$node", new NodeService());
        }
        // Register and start enqued services and listeners
        for (Map.Entry<String, Service> entry : services.entrySet()) {
            Service service = entry.getValue();
            String serviceName = entry.getKey();
            eventbus.addListeners(serviceName, service);
            serviceRegistry.addActions(serviceName, service);
        }
        // Start transporter's connection loop
        if (transporter != null) {
            transporter.connect();
        }
        // Ok, services, transporter and gateway started
        logger.info("Node \"" + config.getNodeID() + "\" started successfully.");
    } catch (Throwable cause) {
        logger.error("Moleculer Service Broker could not be started!", cause);
        stop();
    } finally {
        middlewares.clear();
        services.clear();
    }
}
Also used : NodeService(services.moleculer.internal.NodeService) Service(services.moleculer.service.Service) NodeService(services.moleculer.internal.NodeService) Cacher(services.moleculer.cacher.Cacher) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Cacher (services.moleculer.cacher.Cacher)1 NodeService (services.moleculer.internal.NodeService)1 Service (services.moleculer.service.Service)1