Search in sources :

Example 16 with Promise

use of org.nustaq.kontraktor.Promise 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 17 with Promise

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

Example 18 with Promise

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

the class RESTActor method getBooks.

// curl -i -X GET http://localhost:8080/api/books/234234
public IPromise getBooks(int id) {
    Promise res = promise();
    // simulate blocking operation (e.g. database query)
    execInThreadPool(() -> new Book().title("Title " + id).id("" + id).author("kontraktor")).then(res);
    return res;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise)

Example 19 with Promise

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

the class AsyncHttpActor method post.

public IPromise<HttpResponse> post(String url, String postData, String... headers) {
    Promise res = new Promise();
    if (url == null) {
        int debug = 1;
    }
    try {
        HttpPost req = new HttpPost(url);
        setHeaders(req, headers);
        beChrome(req);
        req.setEntity(new StringEntity(postData));
        getClient().execute(req, new FutureCallback<HttpResponse>() {

            @Override
            public void completed(HttpResponse result) {
                // switch to actor thread
                execute(() -> res.resolve(result));
            }

            @Override
            public void failed(Exception ex) {
                // switch to actor thread
                execute(() -> res.reject(ex));
            }

            @Override
            public void cancelled() {
                // switch to actor thread
                execute(() -> res.reject("cancelled"));
            }
        });
    } catch (Throwable th) {
        Log.Warn(this, "get fail " + th + " " + url);
        res.reject(th);
    }
    return res;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 20 with Promise

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

the class JNPM method npmInstall.

public IPromise<InstallResult> npmInstall(String module, String versionSpec, File importingModuleDir) {
    if (versionSpec == null) {
        versionSpec = "latest";
    }
    File nodeModule = new File(nodeModulesDir, module);
    boolean installPrivate = false;
    if (nodeModule.exists()) {
        File targetDir = importingModuleDir.getName().equals("node_modules") ? nodeModulesDir : new File(importingModuleDir, "node_modules");
        String moduleKey = createModuleKey(module, targetDir);
        List<Promise> promises = packagesUnderway.get(moduleKey);
        if (promises != null) {
            int debug = 1;
        }
        String finalVersionSpec1 = versionSpec;
        if (// timing: not unpacked
        promises != null && promises.size() > 0) {
            Log.Warn(this, "Delaying because in transfer:" + module + " " + targetDir.getAbsolutePath());
            Promise p = new Promise();
            delayed(1000, () -> npmInstall(module, finalVersionSpec1, importingModuleDir).then(p));
            return p;
        }
        File pack = new File(nodeModule, "package.json");
        if (pack.exists() && versionSpec.indexOf(".") > 0) {
            String version = null;
            String vspec = null;
            try {
                JsonObject pjson = Json.parse(new FileReader(pack)).asObject();
                version = pjson.getString("version", null);
                vspec = versionSpec;
                if (!matches(version, vspec)) {
                    Log.Warn(this, "version mismatch for module '" + module + "'. requested:" + versionSpec + " from '" + importingModuleDir.getName() + "' installed:" + version + ". (delete module dir for update)");
                    if (config.getVersion(module) == null) {
                        installPrivate = true;
                        Log.Warn(this, "   installing private " + module);
                    } else {
                        Log.Warn(this, "   stick with mismatch because of jnpm.kson config entry for " + module);
                    }
                }
            } catch (Exception e) {
                Log.Error(this, "can't parse package.json in " + module + " " + importingModuleDir.getAbsolutePath() + ". Retry");
                Promise p = new Promise();
                delayed(1000, () -> npmInstall(module, finalVersionSpec1, importingModuleDir).then(p));
                return p;
            }
        } else
            return resolve(InstallResult.EXISTS);
    }
    Promise p = new Promise();
    // fire in parallel
    IPromise<List<String>> versionProm = getVersions(module);
    IPromise<JsonObject> distributionsProm = getDistributions(module);
    String finalVersionSpec = versionSpec == null ? "latest" : versionSpec;
    boolean finalInstallPrivate = installPrivate;
    distributionsProm.then((dist, derr) -> {
        if (dist == null) {
            dist = new JsonObject();
            Log.Error(this, "distribution is empty or error " + derr + " in module:" + module);
        }
        JsonObject finalDist = dist;
        versionProm.then((versions, verr) -> {
            if (versions == null) {
                p.reject(verr);
                return;
            }
            String resolvedVersion = getVersion(module, finalVersionSpec, versions, finalDist);
            http.getContent(config.getRepo() + "/" + module + "/" + resolvedVersion).then((cont, err) -> {
                if (cont != null) {
                    JsonObject pkg = Json.parse(cont).asObject();
                    String tarUrl = pkg.get("dist").asObject().get("tarball").asString();
                    JsonValue dependencies = pkg.get("dependencies");
                    if (dependencies == null)
                        dependencies = new JsonObject();
                    else
                        dependencies = dependencies.asObject();
                    JsonValue peerdependencies = pkg.get("peerDependencies");
                    if (peerdependencies != null) {
                        ((JsonObject) dependencies).merge(peerdependencies.asObject());
                    }
                    File targetDir = finalInstallPrivate ? new File(importingModuleDir, "node_modules") : nodeModulesDir;
                    if (finalInstallPrivate)
                        targetDir.mkdirs();
                    IPromise depP = downLoadAndInstall(tarUrl, module, resolvedVersion, targetDir);
                    List deps = new ArrayList<>();
                    deps.add(depP);
                    File importingDir = new File(nodeModulesDir, module);
                    ((JsonObject) dependencies).forEach(member -> {
                        deps.add(npmInstall(member.getName(), member.getValue().asString(), importingDir));
                    });
                    all(deps).then((r, e) -> {
                        p.resolve(InstallResult.INSTALLED);
                    });
                } else {
                    p.reject("no such module");
                }
            });
        });
    });
    return p;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) 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