Interact with ElasticSearch Server

I’d like to directly interact with an Elastic Search Server (ELS).

(No public access to the ELS, protected and accessed via a custom code endpoint)

Client calls backendless custom rest endpoint -> custom code (check cache) queries ELS -> beatify result, cache, responses to the client (good practice? )

Can I somehow keep a connection open to the ELS or do I have to create a simple http client for every call to the custom service, in order to query the ELS via the Elastic Rest Api?

Sounds stupid to me to always create a new http client. Singletons aren’t available/staying alive in custom code, are they?

In terms of performance and reliability it would be great to have an Elastic Transport Client within backendless. (similar to the caching API which wrapped the Redis cache) Such client is aware of an ELS cluster and therefore can directly point the query to the correct node.

Cheers,
Jens

Hi Jens,

Custom server-code has a limited run-time. Allocated threads will be terminated/destructed upon reaching the limit 5sec(free), 20sec(paid). As a result, allocating a persistent socket to any outside resource would not work. One additional reason for this is scalability - there is no guarantee that a subsequent request will end up in the same CodeRunner container, as a result, any server-side “routine” must be autonomous, without any persistent state (which rules out the concept of singletons).

Regards,
Mark

CodeRunner container may run on a different machine/node I guess?

Is Custom Server Side code time limited on Standalone, too?

public String testElasticSearch() {
    try {
        URL url = new URL("http://elasticServer");
        HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
        httpUrlConnection.setDoInput(true);
        httpUrlConnection.setRequestMethod("GET");
        httpUrlConnection.setRequestProperty("Accept-Encoding", "gzip");

        httpUrlConnection.connect();
        InputStream is = new BufferedInputStream(httpUrlConnection.getInputStream());
        BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(is));
        String line = "";
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = responseStreamReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        String result = stringBuilder.toString();
        is.close();
        responseStreamReader.close();
        httpUrlConnection.disconnect();
        return result;
    } catch (IOException e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();
    }
}

Works fine! =)

(If running on an internal network, as planned, it should finish the operation within 5 seconds)

I should have an answer on the runtime limit in CodeRunner for Standalone shortly…