Search in sources :

Example 1 with KxPublisherActor

use of org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor in project kontraktor by RuedigerMoeller.

the class KxReactiveStreams method newAsyncProcessor.

/**
 * create async processor. Usually not called directly (see EventSink+RxPublisher)
 * The resulting processor is Publisher allowing for multiple subscribers, having a
 * slowest-subscriber-dominates policy.
 *
 * @param processingFunction
 * @param sched
 * @param batchSize
 * @param <IN>
 * @param <OUT>
 * @return
 */
public <IN, OUT> Processor<IN, OUT> newAsyncProcessor(Function<IN, OUT> processingFunction, Scheduler sched, int batchSize) {
    if (batchSize > KxReactiveStreams.MAX_BATCH_SIZE) {
        throw new RuntimeException("batch size exceeds max of " + KxReactiveStreams.MAX_BATCH_SIZE);
    }
    KxPublisherActor pub = Actors.AsActor(KxPublisherActor.class, sched);
    // actually init should provide a promise as return value to ensure streams instance is initialized.
    // do sync shortcut instead here
    pub._streams = this;
    ((KxPublisherActor) pub.getActor())._streams = this;
    pub.setBatchSize(batchSize);
    pub.init(processingFunction);
    return pub;
}
Also used : KxPublisherActor(org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor)

Example 2 with KxPublisherActor

use of org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor in project kontraktor by RuedigerMoeller.

the class KxReactiveStreams method connect.

/**
 * @param eventType
 * @param connectable
 * @param disconHandler - can be null
 * @param <T>
 * @return
 */
public <T> IPromise<KxPublisher<T>> connect(Class<T> eventType, ConnectableActor connectable, Callback<ActorClientConnector> disconHandler) {
    Callback<ActorClientConnector> discon = (acc, err) -> {
        Log.Info(this, "Client disconnected");
        acc.closeClient();
        if (disconHandler != null) {
            disconHandler.complete(acc, err);
        }
    };
    IPromise connect = connectable.actorClass(KxPublisherActor.class).inboundQueueSize(scheduler.getDefaultQSize()).connect(discon, r -> {
        Object remoteref = r;
        if (((KxPublisherActor) remoteref)._callerSideSubscribers != null) {
            ((KxPublisherActor) remoteref)._callerSideSubscribers.forEach(subs -> {
                ((Subscriber) subs).onError(new IOException("connection lost"));
            });
            ((KxPublisherActor) remoteref)._callerSideSubscribers = null;
        }
    });
    Promise<KxPublisher<T>> res = new Promise<>();
    connect.then((publisher, err) -> {
        if (publisher != null) {
            ((KxPublisherActor) publisher)._streams = this;
            res.resolve((KxPublisher<T>) publisher);
        } else {
            res.reject(err);
        }
    });
    return res;
}
Also used : ActorPublisher(org.nustaq.kontraktor.remoting.base.ActorPublisher) Iterator(java.util.Iterator) ActorClientConnector(org.nustaq.kontraktor.remoting.base.ActorClientConnector) Spliterators(java.util.Spliterators) java.util.stream(java.util.stream) Processor(org.reactivestreams.Processor) Publisher(org.reactivestreams.Publisher) Collection(java.util.Collection) SimpleScheduler(org.nustaq.kontraktor.impl.SimpleScheduler) IOException(java.io.IOException) Function(java.util.function.Function) Consumer(java.util.function.Consumer) SyncProcessor(org.nustaq.kontraktor.reactivestreams.impl.SyncProcessor) KxSubscriber(org.nustaq.kontraktor.reactivestreams.impl.KxSubscriber) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) KxPublisherActor(org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod) Log(org.nustaq.kontraktor.util.Log) org.nustaq.kontraktor(org.nustaq.kontraktor) Subscriber(org.reactivestreams.Subscriber) Spliterator(java.util.Spliterator) KxSubscriber(org.nustaq.kontraktor.reactivestreams.impl.KxSubscriber) Subscriber(org.reactivestreams.Subscriber) ActorClientConnector(org.nustaq.kontraktor.remoting.base.ActorClientConnector) IOException(java.io.IOException)

Example 3 with KxPublisherActor

use of org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor in project kontraktor by RuedigerMoeller.

the class KxReactiveStreams method produce.

public <T> KxPublisher<T> produce(int batchSize, Iterator<T> iter) {
    if (batchSize > KxReactiveStreams.MAX_BATCH_SIZE) {
        throw new RuntimeException("batch size exceeds max of " + KxReactiveStreams.MAX_BATCH_SIZE);
    }
    KxPublisherActor pub = Actors.AsActor(KxPublisherActor.class, scheduler);
    // actually init should provide a promise as return value to ensure streams instance is initialized.
    // do sync shortcut instead here
    pub._streams = this;
    ((KxPublisherActor) pub.getActor())._streams = this;
    pub.setBatchSize(batchSize);
    pub.initFromIterator(iter);
    return pub;
}
Also used : KxPublisherActor(org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor)

Example 4 with KxPublisherActor

use of org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor in project kontraktor by RuedigerMoeller.

the class KxReactiveStreams method newLossyProcessor.

public <T, OUT> Processor<T, OUT> newLossyProcessor(Function<T, OUT> processingFunction, int batchSize) {
    if (batchSize > KxReactiveStreams.MAX_BATCH_SIZE) {
        throw new RuntimeException("batch size exceeds max of " + KxReactiveStreams.MAX_BATCH_SIZE);
    }
    KxPublisherActor pub = Actors.AsActor(KxPublisherActor.class, scheduler);
    // actually init should provide a promise as return value to ensure streams instance is initialized.
    // do sync shortcut instead here
    pub._streams = this;
    ((KxPublisherActor) pub.getActor())._streams = this;
    pub.setBatchSize(batchSize);
    pub.setLossy(true);
    pub.init(processingFunction);
    return pub;
}
Also used : KxPublisherActor(org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor)

Aggregations

KxPublisherActor (org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor)4 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Spliterator (java.util.Spliterator)1 Spliterators (java.util.Spliterators)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 java.util.stream (java.util.stream)1 org.nustaq.kontraktor (org.nustaq.kontraktor)1 CallerSideMethod (org.nustaq.kontraktor.annotations.CallerSideMethod)1 SimpleScheduler (org.nustaq.kontraktor.impl.SimpleScheduler)1 KxSubscriber (org.nustaq.kontraktor.reactivestreams.impl.KxSubscriber)1 SyncProcessor (org.nustaq.kontraktor.reactivestreams.impl.SyncProcessor)1 ActorClientConnector (org.nustaq.kontraktor.remoting.base.ActorClientConnector)1 ActorPublisher (org.nustaq.kontraktor.remoting.base.ActorPublisher)1 ConnectableActor (org.nustaq.kontraktor.remoting.base.ConnectableActor)1 Log (org.nustaq.kontraktor.util.Log)1 Processor (org.reactivestreams.Processor)1 Publisher (org.reactivestreams.Publisher)1