Search in sources :

Example 11 with Promise

use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.

the class BasicTest method allTest.

@Test
public void allTest() throws InterruptedException {
    ArrayList<IPromise<Object>> al = new ArrayList();
    al.add(new Promise());
    al.add(new Promise());
    new Thread(() -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        al.get(0).resolve();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        al.get(1).resolve();
    }).start();
    Actors.all(al).await();
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) IPromise(org.nustaq.kontraktor.IPromise) DispatcherThread(org.nustaq.kontraktor.impl.DispatcherThread) Test(org.junit.Test)

Example 12 with Promise

use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.

the class AsyncSocketConnection method writeFinished.

// error = null => ok
void writeFinished(Object error) {
    checkThread();
    writingBuffer = null;
    Promise wp = this.writePromise;
    writePromise = null;
    if (!wp.isSettled()) {
        if (error != null)
            wp.reject(error);
        else
            wp.resolve();
    }
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise)

Example 13 with Promise

use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.

the class TicketMachine method getTicket.

public IPromise<IPromise> getTicket(final Object channelKey) {
    List<Ticket> futures = tickets.get(channelKey);
    if (futures == null) {
        futures = new ArrayList<>(3);
        tickets.put(channelKey, futures);
    }
    Promise<Object> signalFin = new Promise<>();
    IPromise signalStart = new Promise();
    final Ticket ticket = new Ticket(signalStart, signalFin);
    futures.add(ticket);
    // System.out.println("get ticket "+ticket+" "+Thread.currentThread().getName());
    final List<Ticket> finalFutures = futures;
    signalFin.then(new Callback() {

        @Override
        public void complete(Object result, Object error) {
            // System.out.println("rec "+channelKey+" do remove+checknext");
            boolean remove = finalFutures.remove(ticket);
            if (!remove)
                System.err.println("Error failed to remove " + channelKey);
            checkNext(channelKey, finalFutures, ticket);
        }
    });
    if (futures.size() == 1) {
        // this is the one and only call, start immediately
        signalStart.complete(signalFin, null);
    }
    return signalStart;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) Callback(org.nustaq.kontraktor.Callback)

Example 14 with Promise

use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.

the class NIOServerConnector method Publish.

public static Promise<ActorServer> Publish(Actor facade, int port, Coding coding, Consumer<Actor> disconnectHandler) {
    Promise finished = new Promise();
    try {
        ActorServer publisher = new ActorServer(new NIOServerConnector(port), facade, coding);
        facade.execute(() -> {
            try {
                publisher.start(disconnectHandler);
                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) IOException(java.io.IOException)

Example 15 with Promise

use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.

the class HttpMonitor method getMonitorables.

protected IPromise<Object[]> getMonitorables(int depth, Monitorable monitorable) {
    // System.out.println("dumpmon " + monitorable);
    Promise p = new Promise();
    monitorable.$getReport().then((report, err) -> {
        Object[] result = new Object[2];
        result[0] = report;
        monitorable.$getSubMonitorables().then((monitorables, errmon) -> {
            if (monitorables.length > 0 && depth >= 1) {
                Object[] subResult = new Object[monitorables.length];
                result[1] = subResult;
                IPromise[] futs = new IPromise[monitorables.length];
                for (int i = 0; i < monitorables.length; i++) {
                    Monitorable submon = monitorables[i];
                    futs[i] = getMonitorables(depth - 1, submon);
                }
                all(futs).then((futArr, err0) -> {
                    IPromise[] futures = (IPromise[]) futArr;
                    for (int i = 0; i < futures.length; i++) {
                        IPromise future = futures[i];
                        subResult[i] = future.get();
                    }
                    p.settle(result, null);
                });
            } else
                p.settle(result, null);
        });
    });
    return p;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) Monitorable(org.nustaq.kontraktor.monitoring.Monitorable) IPromise(org.nustaq.kontraktor.IPromise)

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