BatchPublish

Struct BatchPublish 

Source
pub struct BatchPublish<C> {
    pub context: C,
    pub sequence: u64,
    pub batch_id: String,
    /* private fields */
}

Fields§

§context: C§sequence: u64§batch_id: String

Implementations§

Source§

impl<C> BatchPublish<C>

Source

pub fn new(context: C, sequence: u64, batch_id: String) -> Self

Source

pub fn size(&self) -> u64

Get the current number of messages in the batch.

This includes messages that have been added but excludes the final commit message until it is sent.

Source

pub async fn add<S: ToSubject>( &mut self, subject: S, payload: Bytes, ) -> Result<(), BatchPublishError>

Add a message to the batch with the specified subject and payload.

The message is sent immediately with batch headers. If flow control is configured (via ack_first or ack_every), this method may wait for acknowledgment from the server.

§Errors

Returns MaxMessagesExceeded if adding this message would exceed the server’s batch size limit of 1000 messages.

§Examples
let mut batch = client.batch_publish().build();
batch.add("events.user.created", r#"{"id":123}"#.into()).await?;
batch.add("events.user.updated", r#"{"id":123,"name":"Alice"}"#.into()).await?;
Source

pub async fn add_message( &mut self, message: OutboundMessage, ) -> Result<(), BatchPublishError>

Add a pre-constructed message to the batch.

This is useful when you need to include headers or have already constructed the OutboundMessage.

§Errors

Returns MaxMessagesExceeded if adding this message would exceed the server’s batch size limit of 1000 messages.

Returns BatchPublishUnsupportedHeader if the message contains unsupported headers like Nats-Msg-Id or Nats-Expected-Last-Msg-Id.

§Examples
let mut batch = client.batch_publish().build();

let message = OutboundMessage {
    subject: "events.important".into(),
    payload: "critical data".into(),
    headers: None,
};

batch.add_message(message).await?;
Source

pub async fn commit<S: ToSubject>( self, subject: S, payload: Bytes, ) -> Result<BatchPubAck, BatchPublishError>

Commit the batch with a final message.

This sends the final message with batch headers and a commit marker, completing the batch operation. The batch cannot be used after committing.

§Examples
let mut batch = client.batch_publish().build();

batch.add("events.1", "data1".into()).await?;
batch.add("events.2", "data2".into()).await?;

// Commit with final message
let ack = batch.commit("events.3", "data3".into()).await?;

println!("Batch {} committed with {} messages", ack.batch_id, ack.batch_size);
Source

pub async fn commit_message( self, message: OutboundMessage, ) -> Result<BatchPubAck, BatchPublishError>

Commit the batch with a pre-constructed final message.

Like commit, but accepts a pre-constructed OutboundMessage.

§Examples
let mut batch = client.batch_publish().build();

let final_message = OutboundMessage {
    subject: "events.complete".into(),
    payload: "batch done".into(),
    headers: None,
};

let ack = batch.commit_message(final_message).await?;
Source

pub fn discard(self)

Discard the batch without committing.

This consumes the batch without sending a commit message. The server will eventually abandon the batch after a timeout.

§Examples
let mut batch = client.batch_publish().build();

batch.add("events.1", "data".into()).await?;

// Decide to abandon the batch
batch.discard();

Auto Trait Implementations§

§

impl<C> Freeze for BatchPublish<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for BatchPublish<C>
where C: RefUnwindSafe,

§

impl<C> Send for BatchPublish<C>
where C: Send,

§

impl<C> Sync for BatchPublish<C>
where C: Sync,

§

impl<C> Unpin for BatchPublish<C>
where C: Unpin,

§

impl<C> UnwindSafe for BatchPublish<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more