use of org.nustaq.kontraktor.reactivestreams.KxPublisher 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);
}
use of org.nustaq.kontraktor.reactivestreams.KxPublisher 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);
}
use of org.nustaq.kontraktor.reactivestreams.KxPublisher in project kontraktor by RuedigerMoeller.
the class TCPNIOKStreamsTest method testClient1.
@Test
@Ignore
public void testClient1() throws InterruptedException {
AtomicLong received = new AtomicLong(0);
Callback<ActorClientConnector> discon = (acc, err) -> {
System.out.println("Client disconnected");
};
KxPublisher<String> remote = get().connect(String.class, getRemoteConnector(), discon).await();
RateMeasure ms = new RateMeasure("event rate");
remote.syncMap(string -> string.length()).syncMap(number -> number > 10 ? number : number).subscribe((str, err) -> {
if (isErrorOrComplete(err)) {
System.out.println("complete");
} else if (isError(err)) {
System.out.println("ERROR");
} else {
received.incrementAndGet();
ms.count();
}
});
while (true) {
Thread.sleep(100);
}
}
use of org.nustaq.kontraktor.reactivestreams.KxPublisher in project kontraktor by RuedigerMoeller.
the class TCPNIOKStreamsTest method testClient.
@Test
@Ignore
public void testClient() throws InterruptedException {
AtomicLong received = new AtomicLong(0);
Callback<ActorClientConnector> discon = (acc, err) -> {
System.out.println("Client disconnected");
};
KxPublisher<String> remote = get().connect(String.class, getRemoteConnector(), discon).await();
RateMeasure ms = new RateMeasure("event rate");
remote.subscribe((str, err) -> {
if (isErrorOrComplete(err)) {
System.out.println("complete e:" + err + " r:" + str);
} else if (isError(err)) {
System.out.println("ERROR " + err);
} else {
received.incrementAndGet();
ms.count();
}
});
while (true) {
Thread.sleep(100);
}
}
use of org.nustaq.kontraktor.reactivestreams.KxPublisher 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);
}
Aggregations