pub struct Message {
pub payload: Vec<u8>,
pub content_type: ContentType,
pub context: MessageContext,
pub ack_handle: Option<Arc<dyn MessageAck>>,
}Expand description
通用的 MQ 消息类型
支持多种序列化格式(JSON、Protobuf、Avro 等)
Fields§
§payload: Vec<u8>原始消息内容
content_type: ContentType序列化格式
context: MessageContext消息上下文
ack_handle: Option<Arc<dyn MessageAck>>Broker ack handle. JetStream auto-commit paths may leave this empty; JetStream uses it so ack/nack happens after handler result instead of at fetch time.
Implementations§
Source§impl Message
impl Message
Sourcepub fn new(
payload: Vec<u8>,
content_type: ContentType,
context: MessageContext,
) -> Message
pub fn new( payload: Vec<u8>, content_type: ContentType, context: MessageContext, ) -> Message
创建新的消息
pub fn with_ack_handle(self, ack_handle: Arc<dyn MessageAck>) -> Message
Sourcepub fn to_json<T>(&self) -> Result<T, ConsumerError>where
T: DeserializeOwned,
pub fn to_json<T>(&self) -> Result<T, ConsumerError>where
T: DeserializeOwned,
反序列化为 JSON(带格式校验)
会校验 content-type 是否为 JSON,用于防御性编程
Sourcepub fn decode_json<T>(&self) -> Result<T, ConsumerError>where
T: DeserializeOwned,
pub fn decode_json<T>(&self) -> Result<T, ConsumerError>where
T: DeserializeOwned,
Sourcepub fn to_protobuf<T>(&self) -> Result<T, ConsumerError>
pub fn to_protobuf<T>(&self) -> Result<T, ConsumerError>
反序列化为 Protobuf(带格式校验)
会校验 content-type 是否为 Protobuf,用于防御性编程
Sourcepub fn decode_protobuf<T>(&self) -> Result<T, ConsumerError>
pub fn decode_protobuf<T>(&self) -> Result<T, ConsumerError>
Sourcepub fn to_string(&self) -> Result<String, ConsumerError>
pub fn to_string(&self) -> Result<String, ConsumerError>
获取原始字符串
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Message
impl !UnwindSafe for Message
impl Freeze for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnsafeUnpin for Message
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request