Giter VIP home page Giter VIP logo

appprocess's Introduction

AppProcess

Prerequisites:

implementation 'com.google.code.gson:gson:2.8.5'

Example

import com.google.gson.JsonObject;

// This block of code will be run in your main process
public class Example {
    public static void Run() throws Exception {
        print();
        echo();
    }

    // this example show you how to send message from parent process to app_process
    // without caring about response data
    public static void print() throws Exception {
        String publicSourceDir = ""; // (applicationContext instance).getApplicationInfo().publicSourceDir;
        AppProcessHost apHost = AppProcessHost.create(PrintProcess.class, publicSourceDir);
        
        apHost.send("Hello there");

        // above line of code is a short hand for
        JsonObject jo = new JsonObject();
        jo.addProperty("message", "Hello there");
        apHost.send(jo);
    }

    // this example show you how to send json object from parent process to app_process
    // and received response data.
    public static void echo() throws Exception {
        String publicSourceDir = ""; // (applicationContext instance).getApplicationInfo().publicSourceDir;
        AppProcessHost apHost = AppProcessHost.create(EchoProcess.class, publicSourceDir);
        JsonObject jo = new JsonObject();
        jo.addProperty("name", "Joey");
        apHost.registerLog(/*String*/ s -> { /* do something with log contenr */ });
        apHost.send(jo, (res) -> {
            if (res.has("echo")) {
                String echoMessage = res.get("echo").getAsString();
                // do stuff with echo Message
            }
        });
    }
}

// This block of code will be run on EchoProcess process
class PrintProcess {
    public static void main(String[] args) {
        AppProcess.start((payload, res) -> {
            if (payload.has("message")) {
                // plain string in app_process_host will be store in "message" prop of payload
                String message = payload.get("message").getAsString();
                // do something with message
             
                // in Example.print apHost.send get called without callback
                // calling res.json(data) in this method doesn't have any meaning   
            }
        });
    }
}

// This block of code will be run on ValidateProcess process
class EchoProcess {
    public static void main(String[] args) {
        AppProcess.start((payload, res) -> {
            if (payload.has("name")) {
                String name = payload.get("name").getAsString();
                JsonObject jo = new JsonObject();
                jo.addProperty("echo", "Echo: " + name);
                
                // calling res.json return json object to parent process
                res.json(jo);   
            }
        });
    }
}

appprocess's People

Contributors

thinhvu avatar

Watchers

 avatar

Forkers

thinhvu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.