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();
}
}
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);
}
}
}
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);
}
Aggregations