mirror of
https://github.com/TeamPiped/reqwest4j.git
synced 2024-08-14 23:54:39 +00:00
Refactor to send request and response without channels.
This commit is contained in:
parent
bfd4ac68e6
commit
5408d34a22
1 changed files with 86 additions and 115 deletions
|
@ -7,7 +7,6 @@ use jni::sys::jobject;
|
||||||
use jni::JNIEnv;
|
use jni::JNIEnv;
|
||||||
use reqwest::{Client, Method, Url};
|
use reqwest::{Client, Method, Url};
|
||||||
use tokio::runtime::Runtime;
|
use tokio::runtime::Runtime;
|
||||||
use tokio::sync::mpsc;
|
|
||||||
|
|
||||||
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
|
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
|
||||||
static CLIENT: OnceLock<Client> = OnceLock::new();
|
static CLIENT: OnceLock<Client> = OnceLock::new();
|
||||||
|
@ -131,9 +130,10 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
|
||||||
|
|
||||||
let runtime = RUNTIME.get().unwrap();
|
let runtime = RUNTIME.get().unwrap();
|
||||||
|
|
||||||
// Create a channel for communication between tasks
|
// send request in a async task
|
||||||
let (tx, mut rx) = mpsc::channel(1);
|
{
|
||||||
let (err_tx, mut err_rx) = mpsc::channel(1);
|
let jvm = Arc::clone(&jvm);
|
||||||
|
let future = Arc::clone(&future);
|
||||||
|
|
||||||
runtime.spawn(async move {
|
runtime.spawn(async move {
|
||||||
// send request
|
// send request
|
||||||
|
@ -141,29 +141,6 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
// Send response to the processing task
|
|
||||||
tx.send(response).await.unwrap();
|
|
||||||
}
|
|
||||||
Err(error) => {
|
|
||||||
// Send error to the error handling task
|
|
||||||
err_tx.send(error).await.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
{
|
|
||||||
let jvm = Arc::clone(&jvm);
|
|
||||||
let future = Arc::clone(&future);
|
|
||||||
runtime.spawn(async move {
|
|
||||||
// Receive the response from the first task
|
|
||||||
let response = rx.recv().await;
|
|
||||||
|
|
||||||
if response.is_none() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let response = response.unwrap();
|
|
||||||
|
|
||||||
// get response
|
// get response
|
||||||
let status = response.status().as_u16() as i32;
|
let status = response.status().as_u16() as i32;
|
||||||
|
|
||||||
|
@ -173,6 +150,8 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
|
||||||
|
|
||||||
let body = response.bytes().await.unwrap_or_default().to_vec();
|
let body = response.bytes().await.unwrap_or_default().to_vec();
|
||||||
|
|
||||||
|
// send response in a blocking task
|
||||||
|
runtime.spawn_blocking(move || {
|
||||||
let mut env = jvm.attach_current_thread().unwrap();
|
let mut env = jvm.attach_current_thread().unwrap();
|
||||||
|
|
||||||
let final_url = env.new_string(final_url).unwrap();
|
let final_url = env.new_string(final_url).unwrap();
|
||||||
|
@ -214,20 +193,9 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
|
||||||
.unwrap();
|
.unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Err(error) => {
|
||||||
{
|
// send error in a blocking task
|
||||||
let jvm = Arc::clone(&jvm);
|
runtime.spawn_blocking(move || {
|
||||||
let future = Arc::clone(&future);
|
|
||||||
runtime.spawn(async move {
|
|
||||||
// Receive the error from the first task
|
|
||||||
let error = err_rx.recv().await;
|
|
||||||
|
|
||||||
if error.is_none() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let error = error.unwrap();
|
|
||||||
|
|
||||||
let mut env = jvm.attach_current_thread().unwrap();
|
let mut env = jvm.attach_current_thread().unwrap();
|
||||||
|
|
||||||
let error = error.to_string();
|
let error = error.to_string();
|
||||||
|
@ -253,6 +221,9 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
|
||||||
.unwrap();
|
.unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
_future.into_raw()
|
_future.into_raw()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue