Search in sources :

Example 6 with ServiceBrokerConfig

use of services.moleculer.config.ServiceBrokerConfig 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");
}
Also used : Context(services.moleculer.context.Context) DefaultContextFactory(services.moleculer.context.DefaultContextFactory) CallOptions(services.moleculer.context.CallOptions) Listener(services.moleculer.eventbus.Listener) Cache(services.moleculer.cacher.Cache) Action(services.moleculer.service.Action) Middleware(services.moleculer.service.Middleware) Version(services.moleculer.service.Version) Name(services.moleculer.service.Name) Subscribe(services.moleculer.eventbus.Subscribe) Dependencies(services.moleculer.service.Dependencies) Service(services.moleculer.service.Service) Tree(io.datatree.Tree) Context(services.moleculer.context.Context) ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig) Action(services.moleculer.service.Action) ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig) Middleware(services.moleculer.service.Middleware) DefaultContextFactory(services.moleculer.context.DefaultContextFactory) Tree(io.datatree.Tree)

Example 7 with ServiceBrokerConfig

use of services.moleculer.config.ServiceBrokerConfig in project moleculer-java by moleculer-java.

the class DefaultServiceRegistry method started.

// --- INIT SERVICE REGISTRY ---
/**
 * Initializes ServiceRegistry instance.
 *
 * @param broker
 *            parent ServiceBroker
 * @param config
 *            optional configuration of the current component
 */
@Override
public void started(ServiceBroker broker) throws Exception {
    super.started(broker);
    // Local nodeID
    this.nodeID = broker.getNodeID();
    // Set components
    ServiceBrokerConfig cfg = broker.getConfig();
    this.scheduler = cfg.getScheduler();
    this.strategyFactory = cfg.getStrategyFactory();
    this.contextFactory = cfg.getContextFactory();
    this.transporter = cfg.getTransporter();
    this.eventbus = cfg.getEventbus();
    this.uid = cfg.getUidGenerator();
}
Also used : ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig)

Example 8 with ServiceBrokerConfig

use of services.moleculer.config.ServiceBrokerConfig in project moleculer-java by moleculer-java.

the class Transporter method started.

// --- START TRANSPORTER ---
/**
 * Initializes transporter instance.
 *
 * @param broker
 *            parent ServiceBroker
 * @param config
 *            optional configuration of the current component
 */
@Override
public void started(ServiceBroker broker) throws Exception {
    super.started(broker);
    // Process config
    ServiceBrokerConfig cfg = broker.getConfig();
    namespace = cfg.getNamespace();
    if (namespace != null && !namespace.isEmpty()) {
        prefix = prefix + '-' + namespace;
    }
    nodeID = broker.getNodeID();
    // Log serializer info
    logger.info(nameOf(this, true) + " will use " + nameOf(serializer, true) + '.');
    // Get components
    executor = cfg.getExecutor();
    scheduler = cfg.getScheduler();
    registry = cfg.getServiceRegistry();
    monitor = cfg.getMonitor();
    eventbus = cfg.getEventbus();
    uid = cfg.getUidGenerator();
    // Set channel names
    eventChannel = channel(PACKET_EVENT, nodeID);
    requestChannel = channel(PACKET_REQUEST, nodeID);
    responseChannel = channel(PACKET_RESPONSE, nodeID);
    discoverBroadcastChannel = channel(PACKET_DISCOVER, null);
    discoverChannel = channel(PACKET_DISCOVER, nodeID);
    infoBroadcastChannel = channel(PACKET_INFO, null);
    infoChannel = channel(PACKET_INFO, nodeID);
    disconnectChannel = channel(PACKET_DISCONNECT, null);
    heartbeatChannel = channel(PACKET_HEARTBEAT, null);
    pingChannel = channel(PACKET_PING, nodeID);
    pongChannel = channel(PACKET_PONG, nodeID);
}
Also used : ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig)

Example 9 with ServiceBrokerConfig

use of services.moleculer.config.ServiceBrokerConfig in project moleculer-java by moleculer-java.

the class DefaultContextFactory method started.

// --- START CONTEXT FACTORY ---
/**
 * Initializes Default Context Factory instance.
 *
 * @param broker
 *            parent ServiceBroker
 * @param config
 *            optional configuration of the current component
 */
@Override
public void started(ServiceBroker broker) throws Exception {
    super.started(broker);
    // Set components
    ServiceBrokerConfig cfg = broker.getConfig();
    circuitBreaker = cfg.getCircuitBreaker();
    eventbus = cfg.getEventbus();
    uid = cfg.getUidGenerator();
}
Also used : ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig)

Aggregations

ServiceBrokerConfig (services.moleculer.config.ServiceBrokerConfig)9 Tree (io.datatree.Tree)1 Color (java.awt.Color)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ServiceBroker (services.moleculer.ServiceBroker)1 Cache (services.moleculer.cacher.Cache)1 CallOptions (services.moleculer.context.CallOptions)1 Context (services.moleculer.context.Context)1 DefaultContextFactory (services.moleculer.context.DefaultContextFactory)1 Listener (services.moleculer.eventbus.Listener)1 Subscribe (services.moleculer.eventbus.Subscribe)1 Action (services.moleculer.service.Action)1 Dependencies (services.moleculer.service.Dependencies)1 Middleware (services.moleculer.service.Middleware)1 Name (services.moleculer.service.Name)1 Service (services.moleculer.service.Service)1 Version (services.moleculer.service.Version)1 NodeDescriptor (services.moleculer.transporter.tcp.NodeDescriptor)1