pub struct BatchPublish<C> {
pub context: C,
pub sequence: u64,
pub batch_id: String,
/* private fields */
}Fields§
§context: C§sequence: u64§batch_id: StringImplementations§
Source§impl<C> BatchPublish<C>
impl<C> BatchPublish<C>
pub fn new(context: C, sequence: u64, batch_id: String) -> Self
Sourcepub fn size(&self) -> u64
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.
Sourcepub async fn add<S: ToSubject>(
&mut self,
subject: S,
payload: Bytes,
) -> Result<(), BatchPublishError>
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?;Sourcepub async fn add_message(
&mut self,
message: OutboundMessage,
) -> Result<(), BatchPublishError>
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?;Sourcepub async fn commit<S: ToSubject>(
self,
subject: S,
payload: Bytes,
) -> Result<BatchPubAck, BatchPublishError>
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);Sourcepub async fn commit_message(
self,
message: OutboundMessage,
) -> Result<BatchPubAck, BatchPublishError>
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?;Sourcepub fn discard(self)
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();