Search in sources :

Example 46 with Promise

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();
}
Also used : LockSupport(java.util.concurrent.locks.LockSupport) IPromise(org.nustaq.kontraktor.IPromise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Actor(org.nustaq.kontraktor.Actor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Actors(org.nustaq.kontraktor.Actors) Test(org.junit.Test) Promise(org.nustaq.kontraktor.Promise) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 47 with Promise

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();
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise)

Example 48 with Promise

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;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) ActorServer(org.nustaq.kontraktor.remoting.base.ActorServer) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 49 with Promise

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;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) Consumer(java.util.function.Consumer)

Example 50 with Promise

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;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) ActorClient(org.nustaq.kontraktor.remoting.base.ActorClient)

Aggregations

Promise (org.nustaq.kontraktor.Promise)67 IPromise (org.nustaq.kontraktor.IPromise)62 Test (org.junit.Test)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Actor (org.nustaq.kontraktor.Actor)6 Remoted (org.nustaq.kontraktor.annotations.Remoted)6 IOException (java.io.IOException)4 Actors (org.nustaq.kontraktor.Actors)4 RateMeasure (org.nustaq.kontraktor.util.RateMeasure)4 JsonObject (com.eclipsesource.json.JsonObject)3 ByteBuffer (java.nio.ByteBuffer)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 CertificateException (java.security.cert.CertificateException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Consumer (java.util.function.Consumer)3 ActorServer (org.nustaq.kontraktor.remoting.base.ActorServer)3 TCPConnectable (org.nustaq.kontraktor.remoting.tcp.TCPConnectable)3 TableSpaceActor (org.nustaq.reallive.impl.tablespace.TableSpaceActor)3 EOFException (java.io.EOFException)2