Search in sources :

Example 11 with WebSocketConnectable

use of org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable in project kontraktor by RuedigerMoeller.

the class AkkaInterop method same_as_serveAkkaKontraktorAsBridge_use_RxJavaAtReceiverSide.

@Test
public void same_as_serveAkkaKontraktorAsBridge_use_RxJavaAtReceiverSide() throws InterruptedException {
    Log.setLevel(Log.DEBUG);
    final int NUM_MSG = 20_000_000;
    final ActorSystem system = ActorSystem.create("reactive-interop");
    // Attention: buffer + batchsizes of Akka need increase in order to get performance
    final ActorMaterializer mat = ActorMaterializer.create(ActorMaterializerSettings.create(system).withInputBuffer(8192, 8192), system);
    KxReactiveStreams kxStreams = KxReactiveStreams.get();
    RateMeasure rm = new RateMeasure("rate");
    CountDownLatch count = new CountDownLatch(NUM_MSG);
    Iterable it = () -> IntStream.range(0, NUM_MSG).mapToObj(x -> x).iterator();
    Publisher<Integer> pub = (Publisher<Integer>) Source.from(it).runWith(Sink.publisher(), mat);
    kxStreams.asKxPublisher(pub).serve(new WebSocketPublisher().hostName("localhost").port(6789).urlPath("akka"));
    AtomicInteger kontraktorCount = new AtomicInteger(0);
    // subscribe with akka client
    KxPublisher<Integer> remotedPublisher = new KxReactiveStreams(true).connect(Integer.class, new WebSocketConnectable().url("ws://localhost:6789/akka")).map(x -> {
        // (*)
        kontraktorCount.incrementAndGet();
        return x;
    });
    RxReactiveStreams.toObservable(remotedPublisher).forEach(i -> {
        rm.count();
        count.countDown();
    });
    int secondsWait = 50;
    while (count.getCount() > 0 && secondsWait-- > 0) {
        System.out.println("count:" + (NUM_MSG - count.getCount()) + " kontraktor count:" + kontraktorCount.get());
        Thread.sleep(1000);
    }
    system.shutdown();
    Assert.assertTrue(count.getCount() == 0);
    // give time closing stuff
    Thread.sleep(1000);
}
Also used : ActorSystem(akka.actor.ActorSystem) IntStream(java.util.stream.IntStream) ActorMaterializerSettings(akka.stream.ActorMaterializerSettings) PublisherSink(akka.stream.impl.PublisherSink) Publisher(org.reactivestreams.Publisher) Source(akka.stream.javadsl.Source) Sink(akka.stream.javadsl.Sink) Materializer(akka.stream.Materializer) Test(org.junit.Test) KxPublisher(org.nustaq.kontraktor.reactivestreams.KxPublisher) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) SubscriberSink(akka.stream.impl.SubscriberSink) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) CountDownLatch(java.util.concurrent.CountDownLatch) ActorMaterializer(akka.stream.ActorMaterializer) RxReactiveStreams(rx.RxReactiveStreams) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorSystem(akka.actor.ActorSystem) Log(org.nustaq.kontraktor.util.Log) Actors(org.nustaq.kontraktor.Actors) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) Assert(org.junit.Assert) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) Publisher(org.reactivestreams.Publisher) KxPublisher(org.nustaq.kontraktor.reactivestreams.KxPublisher) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) CountDownLatch(java.util.concurrent.CountDownLatch) ActorMaterializer(akka.stream.ActorMaterializer) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) Test(org.junit.Test)

Example 12 with WebSocketConnectable

use of org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable in project kontraktor by RuedigerMoeller.

the class RxJava method remotingRxToRxWebSocketSampleEvent.

@Test
public void remotingRxToRxWebSocketSampleEvent() throws InterruptedException {
    Observable<Integer> range = Observable.range(0, NUM_MSG / 4);
    Publisher<MyEvent> pub = RxReactiveStreams.toPublisher(range.map(i -> new MyEvent(i, Math.random(), "Hello" + i)));
    KxReactiveStreams.get().asKxPublisher(pub).serve(new WebSocketPublisher().hostName("localhost").port(7778).urlPath("/ws/rx"));
    RateMeasure rm = new RateMeasure("events");
    KxPublisher<MyEvent> remoteStream = KxReactiveStreams.get().connect(MyEvent.class, new WebSocketConnectable().url("ws://localhost:7778/ws/rx"));
    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) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) Promise(org.nustaq.kontraktor.Promise) KxPublisher(org.nustaq.kontraktor.reactivestreams.KxPublisher) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) MyEvent(examples.MyEvent) CountDownLatch(java.util.concurrent.CountDownLatch) RxReactiveStreams(rx.RxReactiveStreams) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Actors(org.nustaq.kontraktor.Actors) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) Assert(org.junit.Assert) MyEvent(examples.MyEvent) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) Test(org.junit.Test)

Example 13 with WebSocketConnectable

use of org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable in project kontraktor by RuedigerMoeller.

the class RxJava method remotingRxToRxWebSocket.

@Test
public void remotingRxToRxWebSocket() throws InterruptedException {
    Observable<Integer> range = Observable.range(0, NUM_MSG / 4);
    Publisher<Integer> pub = RxReactiveStreams.toPublisher(range);
    KxReactiveStreams.get().asKxPublisher(pub).serve(new WebSocketPublisher().hostName("localhost").port(7777).urlPath("/ws/rx"));
    RateMeasure rm = new RateMeasure("events");
    KxPublisher<Integer> remoteStream = KxReactiveStreams.get().connect(Integer.class, new WebSocketConnectable().url("ws://localhost:7777/ws/rx"));
    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) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) Test(org.junit.Test)

Example 14 with WebSocketConnectable

use of org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable in project kontraktor by RuedigerMoeller.

the class AkkaInterop method serveAkkaKontraktorAsBridge.

@Test
@Ignore
public void serveAkkaKontraktorAsBridge() throws InterruptedException {
    Log.setLevel(Log.DEBUG);
    final int NUM_MSG = 20_000_000;
    final ActorSystem system = ActorSystem.create("reactive-interop");
    // Attention: buffer + batchsizes of Akka need increase in order to get performance
    final ActorMaterializer mat = ActorMaterializer.create(ActorMaterializerSettings.create(system).withInputBuffer(8192, 8192), system);
    KxReactiveStreams kxStreams = KxReactiveStreams.get();
    RateMeasure rm = new RateMeasure("rate");
    CountDownLatch count = new CountDownLatch(NUM_MSG);
    Iterable it = () -> IntStream.range(0, NUM_MSG).mapToObj(x -> x).iterator();
    Publisher<Integer> pub = (Publisher<Integer>) Source.from(it).runWith(Sink.publisher(), mat);
    kxStreams.asKxPublisher(pub).serve(new WebSocketPublisher().hostName("localhost").port(6789).urlPath("akka"));
    AtomicInteger kontraktorCount = new AtomicInteger(0);
    // subscribe with akka client
    KxPublisher<Integer> remotedPublisher = new KxReactiveStreams(true).connect(Integer.class, new WebSocketConnectable().url("ws://localhost:6789/akka")).map(x -> {
        // (*)
        kontraktorCount.incrementAndGet();
        return x;
    });
    // for unknown reasons, this loses messages .. kontraktor receiver (*) above gets them all.
    // when trying to reproduce this locally the message loss seems not to occur.
    // as one can see from the counters the message loss is linear (not related to stream completion)
    // I think its an Akka issue as same test works if consuming from RxJava (see below)
    Source.from(remotedPublisher).runWith(Sink.foreach(elem -> {
        rm.count();
        count.countDown();
    }), mat);
    int secondsWait = 50;
    while (count.getCount() > 0 && secondsWait-- > 0) {
        System.out.println("count:" + (NUM_MSG - count.getCount()) + " kontraktor count:" + kontraktorCount.get());
        Thread.sleep(1000);
    }
    system.shutdown();
    Assert.assertTrue(count.getCount() == 0);
    // give time closing stuff
    Thread.sleep(1000);
}
Also used : ActorSystem(akka.actor.ActorSystem) IntStream(java.util.stream.IntStream) ActorMaterializerSettings(akka.stream.ActorMaterializerSettings) PublisherSink(akka.stream.impl.PublisherSink) Publisher(org.reactivestreams.Publisher) Source(akka.stream.javadsl.Source) Sink(akka.stream.javadsl.Sink) Materializer(akka.stream.Materializer) Test(org.junit.Test) KxPublisher(org.nustaq.kontraktor.reactivestreams.KxPublisher) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) SubscriberSink(akka.stream.impl.SubscriberSink) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) CountDownLatch(java.util.concurrent.CountDownLatch) ActorMaterializer(akka.stream.ActorMaterializer) RxReactiveStreams(rx.RxReactiveStreams) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorSystem(akka.actor.ActorSystem) Log(org.nustaq.kontraktor.util.Log) Actors(org.nustaq.kontraktor.Actors) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) Assert(org.junit.Assert) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) Publisher(org.reactivestreams.Publisher) KxPublisher(org.nustaq.kontraktor.reactivestreams.KxPublisher) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) CountDownLatch(java.util.concurrent.CountDownLatch) ActorMaterializer(akka.stream.ActorMaterializer) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with WebSocketConnectable

use of org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable in project kontraktor by RuedigerMoeller.

the class AkkaInterop method kontraktorRemoteKontraktor.

@Test
public void kontraktorRemoteKontraktor() throws InterruptedException {
    Log.setLevel(Log.DEBUG);
    final int NUM_MSG = 20_000_000;
    KxReactiveStreams kxStreams = KxReactiveStreams.get();
    RateMeasure rm = new RateMeasure("rate");
    CountDownLatch count = new CountDownLatch(NUM_MSG);
    Iterable it = () -> IntStream.range(0, NUM_MSG).mapToObj(x -> x).iterator();
    Publisher<Integer> pub = kxStreams.produce(it.iterator());
    kxStreams.asKxPublisher(pub).serve(new WebSocketPublisher().hostName("localhost").port(6789).urlPath("not_akka"));
    // subscribe with akka client
    KxPublisher<Integer> remotedPublisher = new KxReactiveStreams(true).connect(Integer.class, new WebSocketConnectable().url("ws://localhost:6789/not_akka"));
    remotedPublisher.subscribe((res, err) -> {
        if (Actors.isResult(err)) {
            rm.count();
            count.countDown();
        }
    });
    int secondsWait = 50;
    while (count.getCount() > 0 && secondsWait-- > 0) {
        System.out.println("count:" + count.getCount());
        Thread.sleep(1000);
    }
    Assert.assertTrue(count.getCount() == 0);
    // give time closing stuff
    Thread.sleep(1000);
}
Also used : IntStream(java.util.stream.IntStream) ActorMaterializerSettings(akka.stream.ActorMaterializerSettings) PublisherSink(akka.stream.impl.PublisherSink) Publisher(org.reactivestreams.Publisher) Source(akka.stream.javadsl.Source) Sink(akka.stream.javadsl.Sink) Materializer(akka.stream.Materializer) Test(org.junit.Test) KxPublisher(org.nustaq.kontraktor.reactivestreams.KxPublisher) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) SubscriberSink(akka.stream.impl.SubscriberSink) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) CountDownLatch(java.util.concurrent.CountDownLatch) ActorMaterializer(akka.stream.ActorMaterializer) RxReactiveStreams(rx.RxReactiveStreams) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorSystem(akka.actor.ActorSystem) Log(org.nustaq.kontraktor.util.Log) Actors(org.nustaq.kontraktor.Actors) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) Assert(org.junit.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) KxReactiveStreams(org.nustaq.kontraktor.reactivestreams.KxReactiveStreams) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) Test(org.junit.Test)

Aggregations

WebSocketConnectable (org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)25 Test (org.junit.Test)10 WebSocketPublisher (org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 RateMeasure (org.nustaq.kontraktor.util.RateMeasure)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 SerializerType (org.nustaq.kontraktor.remoting.encoding.SerializerType)6 Log (org.nustaq.kontraktor.util.Log)6 Assert (org.junit.Assert)5 Ignore (org.junit.Ignore)5 Actor (org.nustaq.kontraktor.Actor)5 Actors (org.nustaq.kontraktor.Actors)5 KxPublisher (org.nustaq.kontraktor.reactivestreams.KxPublisher)5 KxReactiveStreams (org.nustaq.kontraktor.reactivestreams.KxReactiveStreams)5 HttpConnectable (org.nustaq.kontraktor.remoting.http.HttpConnectable)5 Publisher (org.reactivestreams.Publisher)5 RxReactiveStreams (rx.RxReactiveStreams)5 ActorSystem (akka.actor.ActorSystem)4 ActorMaterializer (akka.stream.ActorMaterializer)4 ActorMaterializerSettings (akka.stream.ActorMaterializerSettings)4