Skip to content

Instantly share code, notes, and snippets.

@mustafaakin
Created June 4, 2018 20:31
Show Gist options
  • Save mustafaakin/cd74fbbba0cc7ac0a80c88022852149a to your computer and use it in GitHub Desktop.
Save mustafaakin/cd74fbbba0cc7ac0a80c88022852149a to your computer and use it in GitHub Desktop.
Graal + AWS Java Lambda + Golang
# Scripts to build
go build -buildmode=c-shared -o libHello.so src/main/go/lambda.go
$GRAALVM_HOME/bin/native-image Test -cp "target/graalvm-java-go-awslambda-1.0-SNAPSHOT.jar" \
-H:+JNI \
-H:+ReflectionEnabled \
-H:+ReportUnsupportedElementsAtRuntime \
-Djava.library.path=$(pwd) \
--no-server
package main
//#cgo CFLAGS: -I/usr/lib/jvm/java-8-openjdk-amd64/include
//#cgo CFLAGS: -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux
//#include <jni.h>
import "C"
import (
"net"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"encoding/json"
)
//export Java_Test_start
func Java_Test_start(env *C.JNIEnv, clazz C.jclass) {
fmt.Println("Hello from Go")
StartServer()
}
const (
servAddr = "localhost:6789"
)
func communicateJava(input interface{}) (interface{}, error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
if err != nil {
return nil, err
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
return nil, err
}
defer conn.Close()
inBytes, err := json.Marshal(input)
if err != nil {
return nil, err
}
// Write it to TCP socket
_, err = conn.Write(inBytes)
if err != nil {
return nil, err
}
// Signal we won't be writing anything soon
err = conn.CloseWrite()
if err != nil {
return nil, err
}
// Not ideal at all
reply := make([]byte, 3*1024*1024)
n, err := conn.Read(reply)
if err != nil {
return nil, err
}
var resp interface{}
err = json.Unmarshal(reply[0:n], &resp)
return resp, err
}
func StartServer() {
lambda.Start(communicateJava)
}
func main() {
//StartServer()
/*
out, err := communicateJava([]byte("osman"))
if err != nil {
log.Fatal(err)
}
log.Println(string(out))
*/
}
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Test {
public static void aq() {
String s = "{\"a\":123}";
JSONTokener tokener = new JSONTokener(s);
JSONObject root = new JSONObject(tokener);
System.out.println(root);
int a = root.getInt("a");
System.out.println(a);
}
public static native void start();
public static void main(String[] args) throws Exception {
System.out.println("Starting");
System.loadLibrary("Hello");
System.out.println("loaded lib");
new Thread(() -> {
System.out.println("starting");
start();
}).start();
System.out.println("Sleeping 3sec");
Thread.sleep(3000);
System.out.println("Wow, slept 3 sec");
ServerSocket serverSocket = new ServerSocket(6789);
while (true) {
System.out.println("listening");
Socket socket = serverSocket.accept();
System.out.println("accept");
BufferedReader in =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
String read = in.readLine();
System.out.println("java read: " + read);
String reply = "from java reply: " + read;
socket.getOutputStream().write(reply.getBytes());
socket.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment