use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class FutureCatch method testFut.
@Test
public void testFut() throws InterruptedException {
AtomicBoolean res = new AtomicBoolean(true);
final FutCatch futCatch = AsActor(FutCatch.class);
AtomicInteger count = new AtomicInteger(0);
futCatch.error(1).then(r -> System.out.println("NEVER SHOULD BE CALLED")).then(() -> System.out.println("oops")).catchError(err -> {
System.out.println("the expected error");
count.incrementAndGet();
});
futCatch.result(0).then(() -> {
// Runnable
count.addAndGet(1);
System.out.println("EMPTYTHEN");
}).thenAnd(() -> {
// supplier [=callable]
Promise p = new Promise();
count.addAndGet(1);
new Thread(() -> {
LockSupport.parkNanos(500l * 1000 * 1000);
p.resolve("supplier");
}).start();
return p;
}).thenAnd(r -> {
System.out.println("" + r);
count.addAndGet(1);
return futCatch.result(1);
}).thenAnd(r -> {
System.out.println("" + r);
count.addAndGet(2);
return futCatch.result(2);
}).thenAnd(r -> {
System.out.println("" + r);
count.addAndGet(3);
return futCatch.error(1);
}).thenAnd(r -> {
System.out.println("" + r);
count.addAndGet(4);
return futCatch.result(3);
}).thenAnd(r -> {
System.out.println("" + r);
count.addAndGet(5);
return futCatch.result(4);
}).catchError(error -> {
count.addAndGet(5);
System.out.println("catched " + error);
}).then((r, e) -> System.out.println("done " + count.get())).await();
assertTrue(count.get() == 14);
futCatch.stop();
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class TCPServerConnector method connect.
@Override
public void connect(Actor facade, Function<ObjectSocket, ObjectSink> factory) throws Exception {
Promise p = new Promise();
new Thread(() -> acceptLoop(facade, port, factory, p), "acceptor thread " + port).start();
p.await();
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class TCPServerConnector method Publish.
public static Promise<ActorServer> Publish(Actor facade, int port, Coding coding, Consumer<Actor> disconnectCB) {
Promise finished = new Promise();
try {
ActorServer publisher = new ActorServer(new TCPServerConnector(port), facade, coding);
facade.execute(() -> {
try {
publisher.start(disconnectCB);
finished.resolve(publisher);
} catch (Exception e) {
finished.reject(e);
}
});
} catch (Exception e) {
e.printStackTrace();
return new Promise(null, e);
}
return finished;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class Routing method registerService.
/**
* publish and register a service at a remote Krouter
*
* @param connectable - the krouter to connect
* @param service - the service to publish
* @param disconnectCallback
* @return
*/
public static IPromise<Object> registerService(ConnectableActor connectable, Actor service, Consumer<Actor> disconnectCallback, boolean stateful) {
Promise p = promise();
service.getActor().zzRoutingGCEnabled = true;
service.getActorRef().zzRoutingGCEnabled = true;
if (connectable.getActorClass() == null) {
connectable.actorClass(Krouter.class);
}
service.execute(() -> {
connectable.connect(null, (Consumer<Actor>) disconnectCallback).then((r, e) -> {
if (r != null) {
try {
((AbstractKrouter) r).router$RegisterService(service.getUntypedRef(), stateful).await();
} catch (Exception ex) {
p.complete(null, ex);
return;
}
}
p.complete(r, e);
});
});
return p;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class TCPConnectable method connect.
@Override
public <T extends Actor> IPromise<T> connect(Callback<ActorClientConnector> disconnectCallback, Consumer<Actor> actorDisconnecCB) {
Promise result = new Promise();
Runnable connect = () -> {
TCPClientConnector client = new TCPClientConnector(port, host, disconnectCallback);
ActorClient connector = new ActorClient(client, actorClz, coding);
connector.connect(inboundQueueSize, actorDisconnecCB).then(result);
};
if (!Actor.inside()) {
TCPClientConnector.get().execute(() -> Thread.currentThread().setName("tcp singleton remote client actor polling"));
TCPClientConnector.get().execute(connect);
} else
connect.run();
return result;
}
Aggregations