Skip to content

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

Closed
@pickleburger

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]
impl MyApi for MyApiService {

    async fn get(&self, request: Request<GetRequest>) -> Result<Response<GetResponse>, Status> {
        let Ok(jvm) = Jvm::attach_thread()
            else { return Err(Status::internal("attach_thread failed")) };
        let Ok(my_object) = jvm.invoke_static("com.example.MyObject", "getInstance", &Vec::<InvocationArg>::new())
            else { return Err(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