use of services.moleculer.context.DefaultContextFactory in project moleculer-java by moleculer-java.
the class Sample method main.
public static void main(String[] args) throws Exception {
System.out.println("START");
try {
ServiceBrokerConfig cfg = new ServiceBrokerConfig();
// RedisTransporter t = new RedisTransporter();
// t.setDebug(false);
// cfg.setTransporter(t);
ServiceBroker broker = new ServiceBroker(cfg);
MathService math = new MathService();
broker.createService(math);
broker.start();
broker.use(new Middleware() {
@Override
public Action install(Action action, Tree config) {
if (config.get("name", "?").equals("v1.math.test")) {
return new Action() {
@Override
public Object handler(Context ctx) throws Exception {
Object original = action.handler(ctx);
Object replaced = System.currentTimeMillis();
broker.getLogger().info("Middleware invoked! Replacing " + original + " to " + replaced);
return replaced;
}
};
}
return null;
}
});
broker.waitForServices("v1.math").then(ok -> {
for (int i = 0; i < 2; i++) {
broker.call("v1.math.add", "a", 3, "b", 5).then(in -> {
broker.getLogger(Sample.class).info("Result: " + in);
}).catchError(err -> {
broker.getLogger(Sample.class).error("Error: " + err);
});
}
System.out.println("FIRST CALL ->3");
broker.call("service2.test", new Tree(), CallOptions.retryCount(3)).catchError(cause -> {
cause.printStackTrace();
});
});
((DefaultContextFactory) broker.getConfig().getContextFactory()).setMaxCallLevel(3);
Thread.sleep(1000);
broker.createService(new Service2Service());
Thread.sleep(1000);
broker.createService(new Service3Service());
Thread.sleep(60000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("STOP");
}
Aggregations