Search in sources :

Example 6 with RateMeasure

use of org.nustaq.kontraktor.util.RateMeasure in project kontraktor by RuedigerMoeller.

the class RxJava method remotingToJ8Streams.

@Test
public void remotingToJ8Streams() {
    Observable<Integer> range = Observable.range(0, NUM_MSG / 4);
    Publisher<Integer> pub = RxReactiveStreams.toPublisher(range);
    KxReactiveStreams.get().asKxPublisher(pub).serve(new TCPNIOPublisher().port(3457));
    RateMeasure rm = new RateMeasure("events");
    Promise<Integer> finished = new Promise<>();
    KxReactiveStreams.get().connect(Integer.class, new TCPConnectable().host("localhost").port(3457)).stream(stream -> {
        long count = stream.map(i -> {
            rm.count();
            return i;
        }).count();
        finished.resolve((int) count);
    });
    Assert.assertTrue(finished.await(50000) == NUM_MSG / 4);
}
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) Promise(org.nustaq.kontraktor.Promise) 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 7 with RateMeasure

use of org.nustaq.kontraktor.util.RateMeasure in project kontraktor by RuedigerMoeller.

the class RxJava method rxToIteratorTest.

@Test
public void rxToIteratorTest() {
    Observable<Integer> range = Observable.range(0, NUM_MSG / 4);
    Publisher<Integer> pub = RxReactiveStreams.toPublisher(range);
    AtomicInteger count = new AtomicInteger(0);
    RateMeasure rm = new RateMeasure("events");
    Promise<Integer> finished = new Promise<>();
    KxReactiveStreams.get().asKxPublisher(pub).async().iterator(it -> {
        while (it.hasNext()) {
            rm.count();
            count.incrementAndGet();
        }
        finished.resolve(count.get());
    });
    Assert.assertTrue(finished.await(50000).intValue() == NUM_MSG / 4);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Promise(org.nustaq.kontraktor.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) Test(org.junit.Test)

Example 8 with RateMeasure

use of org.nustaq.kontraktor.util.RateMeasure in project kontraktor by RuedigerMoeller.

the class RxJava method rxToKontraktorTest.

@Test
public void rxToKontraktorTest() {
    Observable<Integer> range = Observable.range(0, NUM_MSG);
    Publisher<Integer> pub = RxReactiveStreams.toPublisher(range);
    AtomicInteger count = new AtomicInteger(0);
    RateMeasure rm = new RateMeasure("events");
    Promise<Integer> finished = new Promise<>();
    KxReactiveStreams.get().asKxPublisher(pub).subscribe((r, e) -> {
        rm.count();
        if (Actors.isResult(e)) {
            count.incrementAndGet();
        } else {
            finished.resolve(count.get());
        }
    });
    Assert.assertTrue(finished.await(50000).intValue() == NUM_MSG);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Promise(org.nustaq.kontraktor.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) Test(org.junit.Test)

Example 9 with RateMeasure

use of org.nustaq.kontraktor.util.RateMeasure 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 10 with RateMeasure

use of org.nustaq.kontraktor.util.RateMeasure 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)

Aggregations

RateMeasure (org.nustaq.kontraktor.util.RateMeasure)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 Test (org.junit.Test)20 KxReactiveStreams (org.nustaq.kontraktor.reactivestreams.KxReactiveStreams)13 WebSocketPublisher (org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher)13 WebSocketConnectable (org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 Actors (org.nustaq.kontraktor.Actors)12 IntStream (java.util.stream.IntStream)10 Log (org.nustaq.kontraktor.util.Log)10 Assert (org.junit.Assert)9 KxPublisher (org.nustaq.kontraktor.reactivestreams.KxPublisher)9 Publisher (org.reactivestreams.Publisher)9 RxReactiveStreams (rx.RxReactiveStreams)9 TCPConnectable (org.nustaq.kontraktor.remoting.tcp.TCPConnectable)8 ActorSystem (akka.actor.ActorSystem)7 ActorMaterializer (akka.stream.ActorMaterializer)7 ActorMaterializerSettings (akka.stream.ActorMaterializerSettings)7 Materializer (akka.stream.Materializer)7 PublisherSink (akka.stream.impl.PublisherSink)7