Created
January 1, 2017 04:48
-
-
Save ScalaWilliam/e0b7257175a1bdf6c5da6025b4329f43 to your computer and use it in GitHub Desktop.
J2V8 sample https://github.com/eclipsesource/J2V8 http://eclipsesource.com/blogs/2016/07/20/running-node-js-on-the-jvm/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.eclipsesource.v8.JavaCallback; | |
import com.eclipsesource.v8.NodeJS; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
public class Mapp { | |
private static String NODE_SCRIPT = "var http = require('http');\n" | |
+ "" | |
+ "var server = http.createServer(function (request, response) {\n" | |
+ " response.writeHead(200, {'Content-Type': 'text/plain'});\n" | |
+ " response.end(someJavaMethod());\n" | |
+ "});\n" | |
+ "" | |
+ "server.listen(8000);\n" | |
+ "console.log('Server running at http://127.0.0.1:8000/');"; | |
public static void main(String[] args) throws IOException { | |
final NodeJS nodeJS = NodeJS.createNodeJS(); | |
JavaCallback callback = (receiver, parameters) -> "Hello, JavaWorld!"; | |
nodeJS.getRuntime().registerJavaMethod(callback, "someJavaMethod"); | |
Path f = Paths.get("meh.js").toAbsolutePath(); | |
Files.write(f.toAbsolutePath(), NODE_SCRIPT.getBytes()); | |
nodeJS.exec(f.toFile()); | |
while (nodeJS.isRunning()) { | |
nodeJS.handleMessage(); | |
} | |
nodeJS.release(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment