Search in sources :

Example 1 with CallerSideMethod

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

the class AbstractKrouter method forwardMultiCallInternal.

// tweak handling such it can deal with double results, runs in caller thread (but single threaded => remoteref registry)
@CallerSideMethod
protected void forwardMultiCallInternal(RemoteCallEntry rceIn, Actor remoteRef, ConnectionRegistry clientRemoteRegistry, boolean[] done, Callback[] selected) {
    RemoteCallEntry rce = rceIn.createCopy();
    Runnable toRun = () -> {
        ConnectionRegistry serviceRemoteReg = remoteRef.__self.__clientConnection;
        if (// targets krouter facade
        rce.getReceiverKey() == 1)
            rce.setReceiverKey(remoteRef.__remoteId);
        if (rce.getFutureKey() != 0) {
            long prevFuturekey = rce.getFutureKey();
            Promise p = new Promise();
            long cbid = serviceRemoteReg.registerPublishedCallback(p);
            rce.setFutureKey(-cbid);
            p.then((r, e) -> {
                self().execute(() -> {
                    serviceRemoteReg.removePublishedObject(cbid);
                    // websocket close is unreliable
                    if (serviceRemoteReg.isTerminated()) {
                        handleServiceDiscon(remoteRef);
                        return;
                    }
                    if (!done[0]) {
                        done[0] = true;
                        RemoteCallEntry cbrce = (RemoteCallEntry) r;
                        cbrce.setReceiverKey(prevFuturekey);
                        clientRemoteRegistry.forwardRemoteMessage(cbrce);
                    }
                });
            });
        }
        if (rce.getCB() != null) {
            Callback cb = rce.getCB();
            CallbackWrapper[] wrapperTrick = { null };
            CallbackWrapper wrapper = wrapperTrick[0] = new CallbackWrapper(self(), (r, e) -> {
                if (selected[0] == null) {
                    selected[0] = wrapperTrick[0];
                }
                if (selected[0] == wrapperTrick[0])
                    cb.complete(r, e);
            });
            rce.setCB(wrapper);
        }
        // websocket close is unreliable
        if (!serviceRemoteReg.isTerminated())
            serviceRemoteReg.forwardRemoteMessage(rce);
        else {
            handleServiceDiscon(remoteRef);
        }
    };
    if (Thread.currentThread() != getCurrentDispatcher())
        self().execute(toRun);
    else
        toRun.run();
}
Also used : java.util(java.util) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod) BiFunction(java.util.function.BiFunction) CallbackRefSerializer(org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer) Log(org.nustaq.kontraktor.util.Log) org.nustaq.kontraktor(org.nustaq.kontraktor) Local(org.nustaq.kontraktor.annotations.Local) org.nustaq.kontraktor.remoting.base(org.nustaq.kontraktor.remoting.base) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 2 with CallerSideMethod

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

the class AbstractKrouter method __dispatchRemoteCall.

@Override
@CallerSideMethod
public boolean __dispatchRemoteCall(ObjectSocket objSocket, RemoteCallEntry rce, // registry of client connection
ConnectionRegistry clientRemoteRegistry, List<IPromise> createdFutures, Object authContext, BiFunction<Actor, String, Boolean> callInterceptor) {
    boolean isCB = rce.getMethod() != null;
    if (isCB && rce.getMethod().startsWith("router$")) {
        return super.__dispatchRemoteCall(objSocket, rce, clientRemoteRegistry, createdFutures, authContext, callInterceptor);
    }
    // if ( isCB ) {
    // getActor().responseCounter.count();
    // } else {
    // getActor().requestCounter.count();
    // }
    // getActor().trafficCounter.count(rce.getSerializedArgs().length);
    boolean success = dispatchRemoteCall(rce, clientRemoteRegistry);
    if (!success) {
        if (rce.getCB() != null) {
            rce.getCB().reject(SERVICE_UNAVAILABLE);
        }
        if (rce.getFutureKey() != 0) {
            RemoteCallEntry cbrce = createErrorPromiseResponse(rce, clientRemoteRegistry);
            clientRemoteRegistry.inFacadeThread(() -> {
                clientRemoteRegistry.forwardRemoteMessage(cbrce);
            });
        }
    }
    return false;
}
Also used : RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 3 with CallerSideMethod

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

the class HotColdFailoverKrouter method setRemoteRef.

@CallerSideMethod
protected void setRemoteRef(Actor remoteRef) {
    ArrayList services = new ArrayList();
    services.add(remoteRef);
    services.addAll(getActor().remoteServices);
    getActor().remoteServices = services;
    Log.Info(this, "service added. #services " + services.size());
}
Also used : ArrayList(java.util.ArrayList) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 4 with CallerSideMethod

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

the class ChangeStream method subscribeOn.

@CallerSideMethod
default Subscriber subscribeOn(String query, ChangeReceiver receiver) throws QParseException {
    Subscriber subs = new Subscriber(new QueryPredicate(query), receiver);
    this.subscribe(subs);
    return subs;
}
Also used : QueryPredicate(org.nustaq.reallive.impl.QueryPredicate) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 5 with CallerSideMethod

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

the class KxPublisher method stream.

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

            @Override
            public void run() {
                try {
                    Stream<T> stream = getKxStreamsInstance().stream(KxPublisher.this, batchSize);
                    streamingCode.accept(stream);
                } catch (Throwable ce) {
                    Subscription subscription = KxSubscriber.subsToCancel.get();
                    if (subscription != null)
                        subscription.cancel();
                    throw ce;
                }
            }
        }.start();
    }
}
Also used : Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) 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