Search in sources :

Example 6 with CallerSideMethod

use of org.nustaq.kontraktor.annotations.CallerSideMethod in project kontraktor by RuedigerMoeller.

the class Actor method __addStopHandler.

// //////////////////////////// internals ///////////////////////////////////////////////////////////////////
@CallerSideMethod
public void __addStopHandler(Callback<SELF> cb) {
    if (__stopHandlers == null) {
        getActorRef().__stopHandlers = new ConcurrentLinkedQueue();
        getActor().__stopHandlers = getActorRef().__stopHandlers;
    }
    __stopHandlers.add(cb);
}
Also used : ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 7 with CallerSideMethod

use of org.nustaq.kontraktor.annotations.CallerSideMethod in project kontraktor by RuedigerMoeller.

the class KxPublisher method iterator.

@CallerSideMethod
default void iterator(int batchSize, Consumer<Iterator<T>> iteratingCode) {
    if (Actor.inside()) {
        try {
            iteratingCode.accept(getKxStreamsInstance().iterator(KxPublisher.this, batchSize));
        } catch (Throwable ce) {
            Subscription subscription = KxSubscriber.subsToCancel.get();
            subscription.cancel();
            throw ce;
        }
    } else {
        new Thread("Iterator Consumer") {

            @Override
            public void run() {
                try {
                    iteratingCode.accept(getKxStreamsInstance().iterator(KxPublisher.this, batchSize));
                } catch (Throwable ce) {
                    Subscription subscription = KxSubscriber.subsToCancel.get();
                    subscription.cancel();
                    throw ce;
                }
            }
        }.start();
    }
}
Also used : Subscription(org.reactivestreams.Subscription) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 8 with CallerSideMethod

use of org.nustaq.kontraktor.annotations.CallerSideMethod in project kontraktor by RuedigerMoeller.

the class AbstractKrouter method createErrorPromiseResponse.

@CallerSideMethod
protected RemoteCallEntry createErrorPromiseResponse(RemoteCallEntry rce, ConnectionRegistry clientRemoteRegistry) {
    RemoteCallEntry cbrce = new RemoteCallEntry();
    cbrce.setReceiverKey(rce.getFutureKey());
    cbrce.setSerializedArgs(clientRemoteRegistry.getConf().asByteArray(new Object[] { null, SERVICE_UNAVAILABLE }));
    cbrce.setQueue(1);
    return cbrce;
}
Also used : RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 9 with CallerSideMethod

use of org.nustaq.kontraktor.annotations.CallerSideMethod in project kontraktor by RuedigerMoeller.

the class Actor method __dispatchRemoteCall.

/**
 * called if a message invokation from remote is received
 * @return true if a new promise has been created
 */
@CallerSideMethod
public boolean __dispatchRemoteCall(ObjectSocket objSocket, RemoteCallEntry rce, ConnectionRegistry registry, List<IPromise> createdFutures, Object authContext, BiFunction<Actor, String, Boolean> callInterceptor) {
    rce.unpackArgs(registry.getConf());
    try {
        Object future = getScheduler().enqueueCallFromRemote(registry, null, self(), rce.getMethod(), rce.getArgs(), false, null, callInterceptor);
        if (future instanceof IPromise) {
            Promise p = null;
            if (createdFutures != null) {
                p = new Promise();
                createdFutures.add(p);
            }
            final Promise finalP = p;
            final RemoteCallEntry finalRce = rce;
            ((IPromise) future).then((r, e) -> {
                Runnable runnable = () -> {
                    try {
                        registry.receiveCBResult(objSocket, finalRce.getFutureKey(), r, e);
                        if (finalP != null)
                            finalP.resolve();
                    } catch (Exception ex) {
                        Log.Warn(this, ex, "--");
                    }
                };
                if (Thread.currentThread() != __currentDispatcher)
                    self().execute(runnable);
                else
                    runnable.run();
            });
        }
    } catch (Throwable th) {
        Log.Warn(this, th);
        if (rce.getFutureKey() != 0) {
            self().execute(() -> {
                try {
                    registry.receiveCBResult(objSocket, rce.getFutureKey(), null, FSTUtil.toString(th));
                } catch (Exception e) {
                    Log.Error(this, e);
                }
            });
        } else {
            FSTUtil.<RuntimeException>rethrow(th);
        }
    }
    return createdFutures != null && createdFutures.size() > 0;
}
Also used : RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 10 with CallerSideMethod

use of org.nustaq.kontraktor.annotations.CallerSideMethod in project kontraktor by RuedigerMoeller.

the class Actor method __getCachedMethod.

@CallerSideMethod
public Method __getCachedMethod(String methodName, Actor actor, BiFunction<Actor, String, Boolean> callInterceptor) {
    // assumption: only remote calls can be intercepted, interceptor != null => remote call
    if (callInterceptor != null) {
        if (interceptedCache == null) {
            interceptedCache = new ConcurrentHashMap(7);
        }
    } else {
        if (methodCache == null) {
            methodCache = new ConcurrentHashMap(7);
        }
    }
    ConcurrentHashMap mcache = callInterceptor != null ? interceptedCache : methodCache;
    Method method = (Method) mcache.get(methodName);
    if (method == null) {
        Method[] methods = actor.getActor().getClass().getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method m = methods[i];
            if (m.getName().equals(methodName)) {
                if (callInterceptor == null || callInterceptor.apply(actor, methodName)) {
                    mcache.put(methodName, m);
                    method = m;
                    break;
                }
            }
        }
    }
    return method;
}
Also used : CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod) Method(java.lang.reflect.Method) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Aggregations

CallerSideMethod (org.nustaq.kontraktor.annotations.CallerSideMethod)10 RemoteCallEntry (org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry)4 Subscription (org.reactivestreams.Subscription)2 Method (java.lang.reflect.Method)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 BiFunction (java.util.function.BiFunction)1 Stream (java.util.stream.Stream)1 org.nustaq.kontraktor (org.nustaq.kontraktor)1 Local (org.nustaq.kontraktor.annotations.Local)1 CallbackWrapper (org.nustaq.kontraktor.impl.CallbackWrapper)1 org.nustaq.kontraktor.remoting.base (org.nustaq.kontraktor.remoting.base)1 CallbackRefSerializer (org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer)1 Log (org.nustaq.kontraktor.util.Log)1 QueryPredicate (org.nustaq.reallive.impl.QueryPredicate)1