Search in sources :

Example 1 with Service

use of services.moleculer.service.Service 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)

Example 2 with Service

use of services.moleculer.service.Service in project moleculer-java by moleculer-java.

the class SpringRegistrator method setApplicationContext.

@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
    // Get Service Broker
    ServiceBroker broker = ctx.getBean(ServiceBroker.class);
    // Find Services in Spring Application Context
    Map<String, Service> serviceMap = ctx.getBeansOfType(Service.class);
    if (serviceMap != null && !serviceMap.isEmpty()) {
        for (Service service : serviceMap.values()) {
            // Register Service in Broker
            broker.createService(service);
        }
    }
}
Also used : ServiceBroker(services.moleculer.ServiceBroker) Service(services.moleculer.service.Service)

Example 3 with Service

use of services.moleculer.service.Service in project moleculer-java by moleculer-java.

the class CircuitBreakerTest method testSimpleCall.

@Test
public void testSimpleCall() throws Exception {
    br.createService(new Service("math") {

        @Name("add")
        public Action add = ctx -> {
            return ctx.params.get("a", 0) + ctx.params.get("b", 0);
        };
    });
    // cb.setEnabled(true);
    long start = System.currentTimeMillis();
    for (int i = 0; i < 50; i++) {
        int rsp = br.call("math.add", "a", i, "b", 1).waitFor().asInteger();
        assertEquals(i + 1, rsp);
    }
    assertTrue(System.currentTimeMillis() - start < 50);
}
Also used : CallOptions(services.moleculer.context.CallOptions) Action(services.moleculer.service.Action) Collection(java.util.Collection) TimeoutException(java.util.concurrent.TimeoutException) Name(services.moleculer.service.Name) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) ServiceBroker(services.moleculer.ServiceBroker) Promise(services.moleculer.Promise) LinkedHashMap(java.util.LinkedHashMap) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Future(java.util.concurrent.Future) Service(services.moleculer.service.Service) ConstantMonitor(services.moleculer.monitor.ConstantMonitor) Map(java.util.Map) Tree(io.datatree.Tree) TestCase(junit.framework.TestCase) Collections(java.util.Collections) ExecutorService(java.util.concurrent.ExecutorService) DefaultServiceRegistry(services.moleculer.service.DefaultServiceRegistry) Action(services.moleculer.service.Action) Service(services.moleculer.service.Service) ExecutorService(java.util.concurrent.ExecutorService) Name(services.moleculer.service.Name) Test(org.junit.Test)

Aggregations

Service (services.moleculer.service.Service)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ServiceBroker (services.moleculer.ServiceBroker)2 Tree (io.datatree.Tree)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Callable (java.util.concurrent.Callable)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 TestCase (junit.framework.TestCase)1 Test (org.junit.Test)1 Promise (services.moleculer.Promise)1 Cacher (services.moleculer.cacher.Cacher)1 CallOptions (services.moleculer.context.CallOptions)1