Search in sources :

Example 11 with IPromise

use of org.nustaq.kontraktor.IPromise 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 IPromise

use of org.nustaq.kontraktor.IPromise 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 13 with IPromise

use of org.nustaq.kontraktor.IPromise 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)

Example 14 with IPromise

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

the class PromiseSampleService method getDataSimple1.

public IPromise<String> getDataSimple1() {
    Promise result = new Promise();
    result.resolve("Data");
    return result;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise)

Example 15 with IPromise

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

the class PromiseSampleService method getDataAsync.

// //////////////////////////////////////////////////
public IPromise<String> getDataAsync() {
    Promise p = new Promise();
    // simulate async long running op
    delayed(3000, () -> p.resolve("Data"));
    // returns before result is present
    return p;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise)

Aggregations

IPromise (org.nustaq.kontraktor.IPromise)58 Promise (org.nustaq.kontraktor.Promise)56 Remoted (org.nustaq.kontraktor.annotations.Remoted)6 Actor (org.nustaq.kontraktor.Actor)5 JsonObject (com.eclipsesource.json.JsonObject)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 ByteBuffer (java.nio.ByteBuffer)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 CertificateException (java.security.cert.CertificateException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Consumer (java.util.function.Consumer)3 TableSpaceActor (org.nustaq.reallive.impl.tablespace.TableSpaceActor)3 JsonValue (com.eclipsesource.json.JsonValue)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringEntity (org.apache.http.entity.StringEntity)2