Search in sources :

Example 6 with Actor

use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.

the class ServiceActor method awaitRequiredServices.

public IPromise awaitRequiredServices() {
    Log.Info(this, "connecting required services ..");
    if (requiredServices.size() == 0) {
        return resolve();
    }
    IPromise res = new Promise<>();
    gravity.getServiceMap().then((smap, err) -> {
        List<IPromise<Object>> servicePromis = new ArrayList();
        String[] servNames = getAllServiceNames();
        for (int i = 0; i < servNames.length; i++) {
            String servName = servNames[i];
            ServiceDescription serviceDescription = smap.get(servName);
            if (serviceDescription != null && requiredServices.get(servName) instanceof Actor == false) {
                if (serviceDescription.getConnectable() == null) {
                    Log.Error(this, "No connecteable defined for service " + serviceDescription.getName());
                }
                IPromise connect;
                try {
                    Log.Info(this, "connect " + serviceDescription.getConnectable());
                    connect = serviceDescription.getConnectable().connect();
                } catch (Throwable th) {
                    Log.Error(this, th, "failed to connect " + serviceDescription.getName());
                    continue;
                }
                Promise notify = new Promise();
                servicePromis.add(notify);
                connect.then((actor, connectionError) -> {
                    if (actor != null) {
                        requiredServices.put(servName, actor);
                        notify.complete();
                    } else {
                        requiredServices.put(servName, UNCONNECTED);
                        Log.Warn(this, "failed to connect " + servName + " " + connectionError + " " + serviceDescription.getConnectable());
                        notify.reject("failed to connect " + servName + " " + connectionError);
                    }
                });
            } else {
                res.reject("required service " + servName + " not registered.");
                return;
            }
        }
        if (!res.isSettled()) {
            all(servicePromis).timeoutIn(15000).then(res).onTimeout(() -> {
                // todo:retry
                Log.Info(this, "failed to connect required services, retry");
            });
        }
    });
    return res;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) IPromise(org.nustaq.kontraktor.IPromise) TableSpaceActor(org.nustaq.reallive.impl.tablespace.TableSpaceActor) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) Actor(org.nustaq.kontraktor.Actor)

Example 7 with Actor

use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.

the class KxStreamServer method main.

public static void main(String[] args) {
    // give fat queue
    KxStreamServer server = Actors.AsActor(KxStreamServer.class, 256000);
    server.init();
    TCPNIOPublisher publisher = new TCPNIOPublisher(server, 7890);
    publisher.publish(actor -> System.out.println("actor " + actor + " disconnected")).await();
    System.out.println("server started at " + publisher);
}
Also used : IntStream(java.util.stream.IntStream) IPromise(org.nustaq.kontraktor.IPromise) Actor(org.nustaq.kontraktor.Actor) Actors(org.nustaq.kontraktor.Actors) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) Promise(org.nustaq.kontraktor.Promise) KxPublisher(org.nustaq.kontraktor.reactivestreams.KxPublisher) HashMap(java.util.HashMap) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) Pair(org.nustaq.kontraktor.util.Pair) EventSink(org.nustaq.kontraktor.reactivestreams.EventSink) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher)

Example 8 with Actor

use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.

the class EventSink method subscribe.

@Override
public void subscribe(Subscriber<? super T> subscriber) {
    if (subs != null) {
        throw new RuntimeException("only one subscription supported");
    }
    if (subscriber == null) {
        throw null;
    }
    subs = subscriber;
    if (subs instanceof Actor) {
        actorSubs = (Actor) subs;
    }
    subscriber.onSubscribe(new Subscription() {

        @Override
        public void request(long l) {
            if (l <= 0) {
                subs.onError(new IllegalArgumentException("spec rule 3.9: request > 0 elements"));
            }
            credits.addAndGet(l);
        }

        @Override
        public void cancel() {
            subs = null;
            canceled = true;
        }
    });
}
Also used : Actor(org.nustaq.kontraktor.Actor) Subscription(org.reactivestreams.Subscription)

Example 9 with Actor

use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.

the class AsyncFile method open.

public void open(Path file, OpenOption... options) throws IOException {
    if (fileChannel != null)
        throw new RuntimeException("can only open once");
    Actor sender = Actor.current();
    Set<OpenOption> set = new HashSet<OpenOption>(options.length);
    Collections.addAll(set, options);
    fileChannel = AsynchronousFileChannel.open(file, set, new ActorExecutorService(sender), NO_ATTRIBUTES);
}
Also used : ActorExecutorService(org.nustaq.kontraktor.util.ActorExecutorService) Actor(org.nustaq.kontraktor.Actor) HashSet(java.util.HashSet)

Example 10 with Actor

use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.

the class AsyncFile method write.

public IPromise<AsyncFileIOEvent> write(long filePosition, ByteBuffer source) {
    if (fileChannel == null)
        throw new RuntimeException("file not opened");
    Actor sender = Actor.current();
    Promise p = new Promise();
    final long bufferStartPos = source.position();
    final ByteBuffer finalTarget = source;
    fileChannel.write(source, filePosition, source, new CompletionHandler<Integer, ByteBuffer>() {

        @Override
        public void completed(Integer result, ByteBuffer attachment) {
            if (source.remaining() > 0) {
                // just retry (will enqueue new message/job to actor mailbox)
                fileChannel.write(source, filePosition, source, this);
            } else {
                long newPos = filePosition + finalTarget.limit() - bufferStartPos;
                if (result < 0)
                    newPos = -1;
                attachment.flip();
                p.resolve(new AsyncFileIOEvent(newPos, result, finalTarget));
            }
        }

        @Override
        public void failed(Throwable exc, ByteBuffer attachment) {
            p.reject(exc);
        }
    });
    return p;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) Actor(org.nustaq.kontraktor.Actor) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Actor (org.nustaq.kontraktor.Actor)18 IPromise (org.nustaq.kontraktor.IPromise)8 Promise (org.nustaq.kontraktor.Promise)8 ByteBuffer (java.nio.ByteBuffer)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Actors (org.nustaq.kontraktor.Actors)2 TCPNIOPublisher (org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher)2 WebSocketConnectable (org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)2 Pair (org.nustaq.kontraktor.util.Pair)2 Handlers (io.undertow.Handlers)1 Undertow (io.undertow.Undertow)1 PathHandler (io.undertow.server.handlers.PathHandler)1 io.undertow.websockets.core (io.undertow.websockets.core)1 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)1 EOFException (java.io.EOFException)1 Serializable (java.io.Serializable)1 java.lang.ref (java.lang.ref)1