use of services.moleculer.ServiceBroker 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.ServiceBroker 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);
}
Aggregations