Skip to content

Jvm cannot be sent between threads. How can I work with invoke_async? #103

Description

@pickleburger

Hello,

Based on #72, Jvm cannot be sent between threads. In that case, how can I work with invoke_async? As soon as I call await on the future returned by invoke_async, I get the compilation error complaining that Jvm cannot be sent between threads. How can I work around it? Thanks!

Sample error message:

the trait `Send` is not implemented for `*mut *const JNINativeInterface_`, which is required by `{async block@src/main.rs:53:134: 76:6}: Send`

Sample code snippet

#[tonic::async_trait]implMyApiforMyApiService{asyncfnget(&self,request:Request<GetRequest>) -> Result<Response<GetResponse>,Status>{letOk(jvm) = Jvm::attach_thread()else{returnErr(Status::internal("attach_thread failed"))};letOk(my_object) = jvm.invoke_static("com.example.MyObject","getInstance",&Vec::<InvocationArg>::new())else{returnErr(Status::internal("invoke_static failed"))};// let args = ...let _ = jvm.invoke_async(&my_object,"get",&vec![args]).await;// ...Ok(Response::new(GetResponse::default()))}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions