use std::sync::Arc;
use rocketmq_client_rust::ClientError;
use rocketmq_client_rust::ClientResult;
use rocketmq_client_rust::ClientRuntime;
use rocketmq_client_rust::ClientRuntimeConfig;
use rocketmq_runtime::RuntimeConfig;
use rocketmq_runtime::RuntimeOwner;
pub struct ExampleClientRuntime {
owner: RuntimeOwner,
client_runtime: Arc<ClientRuntime>,
telemetry_guard: rocketmq_observability::TelemetryRuntimeGuard,
}
impl ExampleClientRuntime {
pub fn try_new(scope: &str) -> ClientResult<Self> {
let owner = RuntimeOwner::plan(RuntimeConfig {
thread_name: format!("rocketmq-client-example-{scope}"),
..Default::default()
})
.expect("test runtime configuration is valid")
.build()
.map_err(|source| ClientError::internal("create client example runtime", source))?;
let telemetry_guard =
rocketmq_observability::install_global(&rocketmq_observability::TelemetryBootstrapConfig::default())
.map_err(|source| ClientError::internal("initialize client example telemetry", source))?;
let client_runtime = ClientRuntime::try_new(
owner.root_context().component("client"),
ClientRuntimeConfig::default(),
telemetry_guard.handle(),
)?;
Ok(Self {
owner,
client_runtime,
telemetry_guard,
})
}
pub fn client_runtime(&self) -> Arc<ClientRuntime> {
Arc::clone(&self.client_runtime)
}
pub async fn shutdown(self) {
let client_report = self.client_runtime.shutdown().await;
assert!(client_report.is_healthy(), "{}", client_report.to_json());
let owner_report = self.owner.shutdown_tasks().await;
assert!(owner_report.is_healthy(), "{}", owner_report.to_json());
let background_report = self.owner.shutdown_background();
assert!(background_report.is_healthy(), "{}", background_report.to_json());
let telemetry_report = self.telemetry_guard.shutdown();
assert!(telemetry_report.is_healthy(), "{}", telemetry_report.to_json());
}
}