timob / jnigi

Golang Java JNI library
BSD 2-Clause "Simplified" License
163 stars 44 forks source link

concurrent JVMs #13

Closed bernardoaraujor closed 6 years ago

bernardoaraujor commented 6 years ago

I'm trying to have many JVMs running on concurrent functions (in main()):

for i := 0; i < nProcessPass; i++{
        slice = csvSlices[i*sliceSize:(i+1)*sliceSize]
        go processPass(slice, resultChan)
    }

where processPass instantiates its own JVM (with a .jar classpath):


func processPass(slice [][]string, resultChan chan string){
    //start JVM
    _, env, err := jnigi.CreateJVM(jnigi.NewJVMInitArgs(false, true, jnigi.DEFAULT_VERSION, []string{"-Xcheck:jni", "-Djava.class.path=/home/bernardo/go/src/github.com/bernardoaraujor/corinda/passfault_corinda/out/artifacts/passfault_corinda_jar/passfault_corinda.jar"}))
    if err != nil {
        log.Fatal(err)
    }

    //create TextAnalysis JVM object
    obj, err := env.NewObject("org/owasp/passfault/TextAnalysis")
    if err != nil {
        log.Fatal(err)
    }

    for _, value := range slice{
        fmt.Println(value)

        //create JVM string with password
        str, err := env.NewObject("java/lang/String", []byte(value[2]))

        //call passwordAnalysis on password
        v, err := obj.CallMethod(env, "passwordAnalysis", "java/lang/String", str)
        if err != nil {
            log.Fatal(err)
        }

        //format result from JVM into Go string
        resultJVM, err := v.(*jnigi.ObjectRef).CallMethod(env, "getBytes", jnigi.Byte|jnigi.Array)
        resultGo := string(resultJVM.([]byte))

        fmt.Println(resultGo)
    }
}

am I doing something wrong? or jnigi just can't have multiple JVMs running concurrently?

timob commented 6 years ago

Hi,

Please see JNI docs. JNI_CreateJavaVM: "Creation of multiple VMs in a single process is not supported."

JNIGI cannot do things that JNI can't do.

If you are trying to run your method concurrently, maybe you want to use AttachCurrentThread.

timob commented 6 years ago

Have a look at https://github.com/timob/jnigi/blob/master/jnigi_test.go#L135 TestAttach on how to use AttachCurrentThread. In your code processPass call AttachCurrentThread create the object and call the method.

bernardoaraujor commented 6 years ago

Cool! I'll dig into it! Seems kinda obvious, now that you pointed it!

Thanks so much Tim!

Regards, Bernardo.

On Nov 12, 2017 19:28, "timob" notifications@github.com wrote:

Have a look at https://github.com/timob/jnigi/blob/master/jnigi_test.go#L135 TestAttach on how to use AttachCurrentThread. In your code processPass call AttachCurrentThread create the object and call the method.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/timob/jnigi/issues/13#issuecomment-343769391, or mute the thread https://github.com/notifications/unsubscribe-auth/AJP7tKd2oHyIokmoTU4u9O4G96Ubqoemks5s12MKgaJpZM4QanuQ .

bernardoaraujor commented 6 years ago

works like a charm! thanks @timob