Search in sources :

Example 11 with TCPConnectable

use of org.nustaq.kontraktor.remoting.tcp.TCPConnectable in project kontraktor by RuedigerMoeller.

the class Java8Streams method remoteJava8Streams.

/**
 * stream a java 8 stream via network
 *
 * @throws InterruptedException
 */
public <T> int remoteJava8Streams(Function<MyEvent, T> doStuff, int port) throws InterruptedException {
    AtomicInteger validCount = new AtomicInteger(-1);
    KxReactiveStreams kxReactiveStreams = KxReactiveStreams.get();
    kxReactiveStreams.produce(IntStream.range(0, 5_000_000).mapToObj(i -> new MyEvent(i, Math.random(), "Hello" + i))).serve(new TCPPublisher().port(port));
    CountDownLatch latch = new CountDownLatch(1);
    RateMeasure measure = new RateMeasure("rate");
    KxReactiveStreams.get().connect(MyEvent.class, new TCPConnectable().host("localhost").port(port)).stream(stream -> {
        long count = 0;
        try {
            count = stream.map(event -> {
                measure.count();
                return doStuff.apply(event);
            }).count();
        } finally {
            System.out.println("Count:" + count);
            validCount.set((int) count);
            latch.countDown();
        }
    });
    latch.await();
    return validCount.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with TCPConnectable

use of org.nustaq.kontraktor.remoting.tcp.TCPConnectable in project kontraktor by RuedigerMoeller.

the class RxJava method remotingTest.

@Test
public void remotingTest() {
    Observable<Integer> range = Observable.range(0, NUM_MSG / 4);
    Publisher<Integer> pub = RxReactiveStreams.toPublisher(range);
    KxReactiveStreams.get().asKxPublisher(pub).serve(new TCPNIOPublisher().port(3456));
    RateMeasure rm = new RateMeasure("events");
    AtomicInteger cnt = new AtomicInteger(0);
    Promise<Integer> finished = new Promise<>();
    KxReactiveStreams.get().connect(Integer.class, new TCPConnectable().host("localhost").port(3456)).subscribe((r, e) -> {
        rm.count();
        if (Actors.isResult(e))
            cnt.incrementAndGet();
        else
            finished.resolve(cnt.get());
    });
    Assert.assertTrue(finished.await(50000) == NUM_MSG / 4);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Promise(org.nustaq.kontraktor.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) Test(org.junit.Test)

Example 13 with TCPConnectable

use of org.nustaq.kontraktor.remoting.tcp.TCPConnectable in project kontraktor by RuedigerMoeller.

the class RxJava method remotingRxToRx.

@Test
public void remotingRxToRx() throws InterruptedException {
    Observable<Integer> range = Observable.range(0, NUM_MSG / 4);
    Publisher<Integer> pub = RxReactiveStreams.toPublisher(range);
    KxReactiveStreams.get().asKxPublisher(pub).serve(new TCPNIOPublisher().port(3458));
    RateMeasure rm = new RateMeasure("events");
    KxPublisher<Integer> remoteStream = KxReactiveStreams.get().connect(Integer.class, new TCPConnectable().host("localhost").port(3458));
    CountDownLatch cnt = new CountDownLatch(NUM_MSG / 4);
    RxReactiveStreams.toObservable(remoteStream).forEach(i -> {
        rm.count();
        cnt.countDown();
    });
    cnt.await(50, TimeUnit.SECONDS);
    Assert.assertTrue(cnt.getCount() == 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 14 with TCPConnectable

use of org.nustaq.kontraktor.remoting.tcp.TCPConnectable in project kontraktor by RuedigerMoeller.

the class KxClient method main.

public static void main(String[] args) {
    // FIXME: cancel test. No error on connection close delivered.
    // FIXME: test from within actor
    KxStreamServer remoteRef = (KxStreamServer) new TCPConnectable(KxStreamServer.class, "localhost", 7890).connect().await();
    // check for stream provided by RxStreamProviding client
    remoteRef.listen("SomeNumbers").then(stream -> {
        stream.subscribe((res, err) -> {
            if (Actors.isError(err)) {
                System.out.println("error:" + err);
            } else if (Actors.isComplete(err)) {
                System.out.println("complete");
            } else {
                System.out.println("received from provided:" + res);
            }
        });
    // then( (stream,err) -> .. ) is same as then( result -> .. ).catchError( error -> .. )
    }).catchError(e -> {
        System.out.println("no stream provided");
    });
    remoteRef.createStream("NUMBERS", 0, 100).then((rxPub, errMsg) -> {
        if (rxPub == null) {
            System.out.println("no stream available:" + errMsg);
            return;
        }
        // could also subscribe with arbitrary RxStreams subscriber.
        // as RxPublisher extends reactivestreams.Publisher
        rxPub.subscribe((res, err) -> {
            if (Actors.isError(err)) {
                System.out.println("error:" + err);
            } else if (Actors.isComplete(err)) {
                System.out.println("complete");
            } else {
                System.out.println("received:" + res);
            }
        });
    });
    remoteRef.listen("TIME").then((rxPub, errMsg) -> {
        if (rxPub == null) {
            System.out.println("no stream available:" + errMsg);
            return;
        }
        // could also subscribe with arbitrary reactive streams subscriber.
        // as RxPublisher extends reactivestreams.Publisher
        rxPub.subscribe((time, err) -> {
            if (Actors.isError(err)) {
                System.out.println("error:" + err);
            } else if (Actors.isComplete(err)) {
                // should not happen as infinite stream
                System.out.println("complete. connection closed ?");
            } else {
                System.out.println("stream received:" + new Date((Long) time));
            }
        });
    });
    remoteRef.<Long>listen("TIME").then(rxPub -> {
        // the indirection of stream( Consumer ) is required as stream() is blocking
        rxPub.stream(stream -> stream.forEach(event -> System.out.println(event)));
    });
}
Also used : TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) Date(java.util.Date) Actors(org.nustaq.kontraktor.Actors) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) Date(java.util.Date)

Example 15 with TCPConnectable

use of org.nustaq.kontraktor.remoting.tcp.TCPConnectable in project kontraktor by RuedigerMoeller.

the class KxStreamProvidingClient method main.

public static void main(String[] args) {
    // FIXME: cancel test. No error on connection close delivered.
    KxStreamServer remoteRef = (KxStreamServer) new TCPConnectable(KxStreamServer.class, "localhost", 7890).connect().await();
    remoteRef.putStream("SomeNumbers", KxReactiveStreams.get().produce(LongStream.range(13, 139)), false);
    // await based variant of subscribing timer (compare with other client):
    KxPublisher timer = remoteRef.listen("TIME").await();
    // could also subscribe with arbitrary RxStreams subscriber.
    // as RxPublisher extends reactivestreams.Publisher
    timer.subscribe((time, err) -> {
        if (Actors.isError(err)) {
            System.out.println("error:" + err);
        } else if (Actors.isComplete(err)) {
            // should not happen as infinite stream
            System.out.println("complete. connection closed ?");
        } else {
            System.out.println("received:" + new Date((Long) time));
        }
    });
}
Also used : TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) KxPublisher(org.nustaq.kontraktor.reactivestreams.KxPublisher) Date(java.util.Date)

Aggregations

TCPConnectable (org.nustaq.kontraktor.remoting.tcp.TCPConnectable)25 Test (org.junit.Test)9 TCPNIOPublisher (org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Actors (org.nustaq.kontraktor.Actors)5 Promise (org.nustaq.kontraktor.Promise)4 TCPPublisher (org.nustaq.kontraktor.remoting.tcp.TCPPublisher)4 RateMeasure (org.nustaq.kontraktor.util.RateMeasure)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 WebSocketConnectable (org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)3 JCommander (com.beust.jcommander.JCommander)2 UnknownHostException (java.net.UnknownHostException)2 Date (java.util.Date)2 TimeUnit (java.util.concurrent.TimeUnit)2 Actor (org.nustaq.kontraktor.Actor)2 IPromise (org.nustaq.kontraktor.IPromise)2 KxPublisher (org.nustaq.kontraktor.reactivestreams.KxPublisher)2 KxReactiveStreams (org.nustaq.kontraktor.reactivestreams.KxReactiveStreams)2 HttpConnectable (org.nustaq.kontraktor.remoting.http.HttpConnectable)2 WebSocketPublisher (org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher)2