Search in sources :

Example 1 with Context

use of services.moleculer.context.Context in project moleculer-java by moleculer-java.

the class DefaultCircuitBreaker method callWithoutBreaker.

// --- CALL SERVICE WITHOUT BREAKER FUNCTION ---
protected Promise callWithoutBreaker(String name, Tree params, CallOptions.Options opts, int remaining, Context parent) {
    try {
        String targetID = opts == null ? null : opts.nodeID;
        Action action = serviceRegistry.getAction(name, targetID);
        Context ctx = contextFactory.create(name, params, opts, parent);
        if (remaining < 1) {
            return Promise.resolve(action.handler(ctx));
        }
        return Promise.resolve(action.handler(ctx)).catchError(cause -> {
            return retryWithoutBreaker(cause, name, params, opts, remaining, parent);
        });
    } catch (Throwable cause) {
        if (remaining < 1) {
            return Promise.reject(cause);
        }
        return retryWithoutBreaker(cause, name, params, opts, remaining, parent);
    }
}
Also used : Context(services.moleculer.context.Context) Action(services.moleculer.service.Action)

Example 2 with Context

use of services.moleculer.context.Context in project moleculer-java by moleculer-java.

the class Cacher method install.

// --- ADD MIDDLEWARE TO ACTION ---
@Override
public Action install(Action action, Tree config) {
    // Is caching enabled?
    Tree cacheNode = config.get("cache");
    if (cacheNode == null) {
        return null;
    }
    // Get cache keys
    Tree keyNode = cacheNode.get("keys");
    final String[] keys;
    if (keyNode == null) {
        keys = null;
    } else {
        List<String> list = keyNode.asList(String.class);
        if (list.isEmpty()) {
            keys = null;
        } else {
            keys = new String[list.size()];
            list.toArray(keys);
        }
    }
    // Get TTL (0 = use default TTL)
    final int ttl = cacheNode.get("ttl", 0);
    return new Action() {

        @Override
        public Object handler(Context ctx) throws Exception {
            String key = getCacheKey(ctx.name, ctx.params, keys);
            return new Promise(resolver -> {
                get(key).then(in -> {
                    if (in == null || in.isNull()) {
                        new Promise(action.handler(ctx)).then(tree -> {
                            set(key, tree, ttl);
                            resolver.resolve(tree);
                        }).catchError(err -> {
                            resolver.reject(err);
                        });
                    } else {
                        resolver.resolve(in);
                    }
                }).catchError(err -> {
                    resolver.reject(err);
                });
            });
        }
    };
}
Also used : Context(services.moleculer.context.Context) List(java.util.List) Action(services.moleculer.service.Action) Middleware(services.moleculer.service.Middleware) Tree(io.datatree.Tree) Context(services.moleculer.context.Context) Name(services.moleculer.service.Name) Promise(services.moleculer.Promise) Promise(services.moleculer.Promise) Action(services.moleculer.service.Action) Tree(io.datatree.Tree)

Example 3 with Context

use of services.moleculer.context.Context 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 4 with Context

use of services.moleculer.context.Context in project moleculer-java by moleculer-java.

the class DefaultServiceRegistry method receiveRequest.

// --- RECEIVE REQUEST FROM REMOTE SERVICE ---
@Override
public void receiveRequest(Tree message) {
    // Verify protocol version
    if (checkVersion) {
        String ver = message.get("ver", "unknown");
        if (!ServiceBroker.PROTOCOL_VERSION.equals(ver)) {
            logger.warn("Invalid protocol version (" + ver + ")!");
            return;
        }
    }
    // Get action property
    String action = message.get("action", (String) null);
    if (action == null || action.isEmpty()) {
        logger.warn("Missing \"action\" property!");
        return;
    }
    // Get strategy (action endpoint array) by action name
    Strategy<ActionEndpoint> strategy;
    readLock.lock();
    try {
        strategy = strategies.get(action);
    } finally {
        readLock.unlock();
    }
    if (strategy == null) {
        logger.warn("Invalid action name (" + action + ")!");
        return;
    }
    // Get local action endpoint (with cache handling)
    ActionEndpoint endpoint = strategy.getEndpoint(nodeID);
    if (endpoint == null) {
        logger.warn("Not a local action (" + action + ")!");
        return;
    }
    // Get request's unique ID
    String id = message.get("id", (String) null);
    if (id == null || id.isEmpty()) {
        logger.warn("Missing \"id\" property!");
        return;
    }
    // Get sender's nodeID
    String sender = message.get("sender", (String) null);
    if (sender == null || sender.isEmpty()) {
        logger.warn("Missing \"sender\" property!");
        return;
    }
    // Create CallOptions
    int timeout = message.get("timeout", 0);
    Tree params = message.get("params");
    // TODO Process other properties:
    // Tree meta = message.get("meta");
    // int level = message.get("level", 1);
    // boolean metrics = message.get("metrics", false);
    // String parentID = message.get("parentID", (String) null);
    // String requestID = message.get("requestID", (String) null);
    CallOptions.Options opts = CallOptions.nodeID(nodeID).timeout(timeout);
    Context ctx = contextFactory.create(action, params, opts, null);
    // Invoke action
    try {
        new Promise(endpoint.handler(ctx)).then(data -> {
            // Send response
            FastBuildTree msg = new FastBuildTree(5);
            msg.putUnsafe("sender", nodeID);
            msg.putUnsafe("id", id);
            msg.putUnsafe("ver", ServiceBroker.PROTOCOL_VERSION);
            msg.putUnsafe("success", true);
            msg.putUnsafe("data", data);
            transporter.publish(Transporter.PACKET_RESPONSE, sender, msg);
        }).catchError(error -> {
            // Send error
            transporter.publish(Transporter.PACKET_RESPONSE, sender, throwableToTree(id, sender, error));
        });
    } catch (Throwable error) {
        // Send error
        transporter.publish(Transporter.PACKET_RESPONSE, sender, throwableToTree(id, sender, error));
    }
}
Also used : Context(services.moleculer.context.Context) CallOptions(services.moleculer.context.CallOptions) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) Enumeration(java.util.Enumeration) FastBuildTree(services.moleculer.util.FastBuildTree) CommonUtils.convertAnnotations(services.moleculer.util.CommonUtils.convertAnnotations) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServiceBroker(services.moleculer.ServiceBroker) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ContextFactory(services.moleculer.context.ContextFactory) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig) CommonUtils.getHostName(services.moleculer.util.CommonUtils.getHostName) LinkedHashSet(java.util.LinkedHashSet) PrintWriter(java.io.PrintWriter) Eventbus(services.moleculer.eventbus.Eventbus) Iterator(java.util.Iterator) StringWriter(java.io.StringWriter) Collection(java.util.Collection) NetworkInterface(java.net.NetworkInterface) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UIDGenerator(services.moleculer.uid.UIDGenerator) Field(java.lang.reflect.Field) RemoteException(java.rmi.RemoteException) Promise(services.moleculer.Promise) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Lock(java.util.concurrent.locks.Lock) Strategy(services.moleculer.strategy.Strategy) Tree(io.datatree.Tree) Transporter(services.moleculer.transporter.Transporter) Annotation(java.lang.annotation.Annotation) Context(services.moleculer.context.Context) StrategyFactory(services.moleculer.strategy.StrategyFactory) CommonUtils.nameOf(services.moleculer.util.CommonUtils.nameOf) Collections(java.util.Collections) Promise(services.moleculer.Promise) FastBuildTree(services.moleculer.util.FastBuildTree) FastBuildTree(services.moleculer.util.FastBuildTree) Tree(io.datatree.Tree) CallOptions(services.moleculer.context.CallOptions)

Example 5 with Context

use of services.moleculer.context.Context in project moleculer-java by moleculer-java.

the class DefaultCircuitBreaker method callWithBreaker.

// --- CALL SERVICE WITH BREAKER FUNCTION ---
protected Promise callWithBreaker(String name, Tree params, CallOptions.Options opts, int remaining, Context parent) {
    EndpointKey endpointKey = null;
    ErrorCounter errorCounter = null;
    try {
        // Get the first recommended Endpoint and Error Counter
        String targetID = opts == null ? null : opts.nodeID;
        ActionEndpoint action = (ActionEndpoint) serviceRegistry.getAction(name, targetID);
        String nodeID = action.getNodeID();
        endpointKey = new EndpointKey(nodeID, name);
        errorCounter = errorCounters.get(endpointKey);
        // Check availability of the Endpoint (if endpoint isn't targetted)
        if (targetID == null) {
            LinkedHashSet<String> nodeIDs = new LinkedHashSet<>(maxSameNodes * 2);
            int sameNodeCounter = 0;
            long now;
            if (errorCounter == null) {
                now = 0;
            } else {
                now = System.currentTimeMillis();
            }
            for (int i = 0; i < maxTries; i++) {
                if (errorCounter == null || errorCounter.isAvailable(now)) {
                    // Endpoint is available
                    break;
                }
                // Store nodeID
                if (!nodeIDs.add(nodeID)) {
                    sameNodeCounter++;
                    if (sameNodeCounter >= maxSameNodes) {
                        // The "maxSameNodes" limit is reached
                        break;
                    }
                }
                // Try to choose another endpoint
                action = (ActionEndpoint) serviceRegistry.getAction(name, targetID);
                nodeID = action.getNodeID();
                endpointKey = new EndpointKey(nodeID, name);
                errorCounter = errorCounters.get(endpointKey);
            }
        }
        // Create new Context
        Context ctx = contextFactory.create(name, params, opts, parent);
        // Invoke Endpoint
        final ErrorCounter currentCounter = errorCounter;
        final EndpointKey currentKey = endpointKey;
        return Promise.resolve(action.handler(ctx)).then(rsp -> {
            // Reset error counter
            if (currentCounter != null) {
                currentCounter.reset();
            }
            // Return response
            return rsp;
        }).catchError(cause -> {
            // Increment error counter
            increment(currentCounter, currentKey, cause, System.currentTimeMillis());
            // Return with error
            if (remaining < 1) {
                return cause;
            }
            // Retry
            return retryWithBreaker(cause, name, params, opts, remaining, parent);
        });
    } catch (Throwable cause) {
        // Increment error counter
        increment(errorCounter, endpointKey, cause, System.currentTimeMillis());
        // Reject
        if (remaining < 1) {
            return Promise.reject(cause);
        }
        // Retry
        return retryWithBreaker(cause, name, params, opts, remaining, parent);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Context(services.moleculer.context.Context) CallOptions(services.moleculer.context.CallOptions) Action(services.moleculer.service.Action) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Name(services.moleculer.service.Name) ActionEndpoint(services.moleculer.service.ActionEndpoint) ServiceBroker(services.moleculer.ServiceBroker) Promise(services.moleculer.Promise) HashSet(java.util.HashSet) Objects(java.util.Objects) ServiceRegistry(services.moleculer.service.ServiceRegistry) Tree(io.datatree.Tree) ContextFactory(services.moleculer.context.ContextFactory) Context(services.moleculer.context.Context) ServiceBrokerConfig(services.moleculer.config.ServiceBrokerConfig) LinkedHashSet(java.util.LinkedHashSet) ActionEndpoint(services.moleculer.service.ActionEndpoint) ActionEndpoint(services.moleculer.service.ActionEndpoint)

Aggregations

Context (services.moleculer.context.Context)5 Tree (io.datatree.Tree)4 Action (services.moleculer.service.Action)4 Promise (services.moleculer.Promise)3 ServiceBrokerConfig (services.moleculer.config.ServiceBrokerConfig)3 CallOptions (services.moleculer.context.CallOptions)3 Name (services.moleculer.service.Name)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Objects (java.util.Objects)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ServiceBroker (services.moleculer.ServiceBroker)2 ContextFactory (services.moleculer.context.ContextFactory)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 InetAddress (java.net.InetAddress)1 NetworkInterface (java.net.NetworkInterface)1