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();
}
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);
}
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);
}
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)));
});
}
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));
}
});
}
Aggregations