rocketmq_client_rust/consumer/lite_pull_consumer.rs
1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::HashMap;
16use std::collections::HashSet;
17use std::sync::Arc;
18
19use cheetah_string::CheetahString;
20use rocketmq_common::common::consumer::consume_from_where::ConsumeFromWhere;
21use rocketmq_common::common::message::message_ext::MessageExt;
22use rocketmq_common::common::message::message_queue::MessageQueue;
23use rocketmq_remoting::protocol::heartbeat::message_model::MessageModel;
24use rocketmq_remoting::protocol::heartbeat::subscription_data::SubscriptionData;
25use rocketmq_rust::ArcMut;
26
27use crate::consumer::allocate_message_queue_strategy::AllocateMessageQueueStrategy;
28use crate::consumer::message_queue_listener::ArcMessageQueueListener;
29use crate::consumer::message_queue_listener::MessageQueueListener;
30use crate::consumer::message_selector::MessageSelector;
31use crate::consumer::store::offset_store::OffsetStore;
32use crate::consumer::topic_message_queue_change_listener::TopicMessageQueueChangeListener;
33
34/// A consumer that pulls messages from brokers on demand, providing explicit control over
35/// fetch timing, offset management, and queue assignment.
36///
37/// Unlike the push consumer, the caller drives message retrieval through [`poll`] or
38/// [`poll_with_timeout`], and may manage offsets manually when auto-commit is disabled.
39/// Queue assignment can be controlled either by subscribing to topics (broker-side rebalance)
40/// or by calling [`assign`] directly (client-controlled assignment).
41///
42/// All methods are asynchronous and do not block the calling thread.
43///
44/// [`poll`]: LitePullConsumerLocal::poll
45/// [`poll_with_timeout`]: LitePullConsumerLocal::poll_with_timeout
46/// [`assign`]: LitePullConsumerLocal::assign
47#[trait_variant::make(LitePullConsumer: Send)]
48pub trait LitePullConsumerLocal: Sync {
49 /// Starts the consumer and establishes connections to the broker and name server.
50 ///
51 /// This function does not block the calling thread.
52 ///
53 /// # Errors
54 ///
55 /// Returns an error if the consumer is already running, if required configuration is
56 /// invalid, or if the connection to the name server cannot be established.
57 async fn start(&self) -> rocketmq_error::RocketMQResult<()>;
58
59 /// Shuts down the consumer and releases all associated resources.
60 ///
61 /// This function does not block the calling thread. After shutdown, the consumer
62 /// cannot be restarted.
63 async fn shutdown(&self);
64
65 /// Returns whether the consumer is currently in the running state.
66 ///
67 /// This function does not block the calling thread.
68 async fn is_running(&self) -> bool;
69
70 /// Subscribes to the specified topic using the default subscription expression.
71 ///
72 /// This function does not block the calling thread.
73 ///
74 /// # Arguments
75 ///
76 /// * `topic` - The name of the topic to subscribe to.
77 ///
78 /// # Errors
79 ///
80 /// Returns an error if the topic name is invalid or if the subscription cannot be
81 /// registered with the broker.
82 async fn subscribe(&self, topic: &str) -> rocketmq_error::RocketMQResult<()>;
83
84 /// Subscribes to the specified topic with a tag-based or SQL-based filter expression.
85 ///
86 /// This function does not block the calling thread.
87 ///
88 /// # Arguments
89 ///
90 /// * `topic` - The name of the topic to subscribe to.
91 /// * `sub_expression` - A tag expression (e.g. `"TagA || TagB"`) or SQL-92 predicate. Pass
92 /// `"*"` to receive all messages.
93 ///
94 /// # Errors
95 ///
96 /// Returns an error if the topic name is invalid, the expression cannot be parsed,
97 /// or the subscription cannot be registered with the broker.
98 async fn subscribe_with_expression(&self, topic: &str, sub_expression: &str) -> rocketmq_error::RocketMQResult<()>;
99
100 /// Subscribes to the specified topic with a filter expression and a queue-change listener.
101 ///
102 /// The listener is invoked whenever the set of assigned [`MessageQueue`]s changes for
103 /// this topic due to rebalance.
104 ///
105 /// This function does not block the calling thread.
106 ///
107 /// # Arguments
108 ///
109 /// * `topic` - The name of the topic to subscribe to.
110 /// * `sub_expression` - A tag expression or SQL-92 predicate. Pass `"*"` for all messages.
111 /// * `listener` - A [`MessageQueueListener`] notified on queue assignment changes.
112 ///
113 /// # Errors
114 ///
115 /// Returns an error if the topic name is invalid, the expression cannot be parsed,
116 /// or the subscription cannot be registered with the broker.
117 async fn subscribe_with_listener<MQL>(
118 &self,
119 topic: &str,
120 sub_expression: &str,
121 listener: MQL,
122 ) -> rocketmq_error::RocketMQResult<()>
123 where
124 MQL: MessageQueueListener + 'static;
125
126 /// Subscribes to the specified topic using a [`MessageSelector`] for server-side filtering.
127 ///
128 /// This function does not block the calling thread.
129 ///
130 /// # Arguments
131 ///
132 /// * `topic` - The name of the topic to subscribe to.
133 /// * `selector` - The filter selector. Pass `None` to receive all messages.
134 ///
135 /// # Errors
136 ///
137 /// Returns an error if the topic name is invalid, the selector expression is rejected
138 /// by the broker, or the subscription cannot be registered.
139 async fn subscribe_with_selector(
140 &self,
141 topic: &str,
142 selector: Option<MessageSelector>,
143 ) -> rocketmq_error::RocketMQResult<()>;
144
145 /// Removes the subscription for the specified topic.
146 ///
147 /// Messages for this topic will no longer be fetched after the next rebalance cycle.
148 /// This function does not block the calling thread.
149 ///
150 /// # Arguments
151 ///
152 /// * `topic` - The name of the topic to unsubscribe from.
153 async fn unsubscribe(&self, topic: &str);
154
155 /// Returns the set of [`MessageQueue`]s currently assigned to this consumer.
156 ///
157 /// This function does not block the calling thread.
158 ///
159 /// # Errors
160 ///
161 /// Returns an error if the consumer is not in the running state.
162 async fn assignment(&self) -> rocketmq_error::RocketMQResult<HashSet<MessageQueue>>;
163
164 /// Manually assigns the given [`MessageQueue`]s to this consumer, bypassing broker rebalance.
165 ///
166 /// Any previously assigned queues not present in `message_queues` are removed.
167 /// This function does not block the calling thread.
168 ///
169 /// # Arguments
170 ///
171 /// * `message_queues` - The complete set of queues to assign to this consumer.
172 ///
173 /// # Errors
174 ///
175 /// Returns an error if `message_queues` is empty or the assignment cannot be applied.
176 async fn assign(&self, message_queues: Vec<MessageQueue>) -> rocketmq_error::RocketMQResult<()>;
177
178 /// Sets the subscription filter expression applied when fetching from manually assigned queues.
179 ///
180 /// This function does not block the calling thread.
181 ///
182 /// # Arguments
183 ///
184 /// * `topic` - The topic for which the filter expression applies.
185 /// * `sub_expression` - A tag expression or SQL-92 predicate used to filter messages.
186 ///
187 /// # Errors
188 ///
189 /// Returns an error if the expression is blank, the consumer has already started, or
190 /// the subscription cannot be switched to manual assignment mode.
191 async fn set_sub_expression_for_assign(
192 &self,
193 topic: &str,
194 sub_expression: &str,
195 ) -> rocketmq_error::RocketMQResult<()>;
196
197 /// Populates `sub_expression_map` with the filter selector for each subscribed topic,
198 /// providing the subscription metadata required for heartbeat payloads.
199 ///
200 /// This function does not block the calling thread.
201 ///
202 /// # Arguments
203 ///
204 /// * `sub_expression_map` - Output map from topic name to its [`MessageSelector`]. Entries are
205 /// inserted for every topic that has an active subscription with a selector.
206 ///
207 /// # Errors
208 ///
209 /// Returns an error if the subscription metadata cannot be retrieved.
210 async fn build_subscriptions_for_heartbeat(
211 &self,
212 sub_expression_map: &mut HashMap<String, MessageSelector>,
213 ) -> rocketmq_error::RocketMQResult<()>;
214
215 /// Returns the subscriptions currently advertised in heartbeat payloads.
216 async fn subscriptions_for_heartbeat(&self) -> HashSet<SubscriptionData>;
217
218 /// Fetches the next batch of messages without allocating owned copies.
219 ///
220 /// Returns `ArcMut<MessageExt>` references to messages, providing shared mutable access
221 /// without heap allocation or deep cloning. The returned references remain valid until
222 /// they are dropped. Messages that need to outlive the poll scope must be cloned explicitly.
223 ///
224 /// This method uses the default poll timeout configured for the consumer.
225 ///
226 /// # Performance
227 ///
228 /// Message contents are not copied. For workloads processing messages without long-term
229 /// storage, this eliminates allocation overhead compared to [`poll()`].
230 ///
231 /// # Examples
232 ///
233 /// ```rust,ignore
234 /// let messages = consumer.poll_zero_copy().await;
235 /// for msg in &messages {
236 /// process_message(msg);
237 /// }
238 ///
239 /// // Clone only filtered messages
240 /// let messages = consumer.poll_zero_copy().await;
241 /// let important: Vec<MessageExt> = messages.into_iter()
242 /// .filter(|msg| is_important(msg))
243 /// .map(|msg| (*msg).clone())
244 /// .collect();
245 /// ```
246 ///
247 /// Returns an empty vector if no messages are available within the default timeout period.
248 /// This function does not block the calling thread.
249 async fn poll_zero_copy(&self) -> Vec<rocketmq_rust::ArcMut<MessageExt>>;
250
251 /// Fetches the next batch of messages without allocating owned copies, with a specified
252 /// timeout.
253 ///
254 /// Behaves identically to [`poll_zero_copy()`], but waits up to `timeout` milliseconds
255 /// for messages to become available.
256 ///
257 /// # Arguments
258 ///
259 /// * `timeout` - Maximum time to wait for messages, in milliseconds.
260 ///
261 /// # Examples
262 ///
263 /// ```rust,ignore
264 /// let messages = consumer.poll_with_timeout_zero_copy(1000).await;
265 /// ```
266 ///
267 /// Returns an empty vector if no messages are available before the timeout expires.
268 /// This function does not block the calling thread.
269 async fn poll_with_timeout_zero_copy(&self, timeout: u64) -> Vec<rocketmq_rust::ArcMut<MessageExt>>;
270
271 /// Fetches the next batch of messages, returning owned copies.
272 ///
273 /// Each returned message is cloned from the internal message store. The caller
274 /// owns the returned messages and may store them beyond the poll scope.
275 ///
276 /// This method uses the default poll timeout configured for the consumer.
277 ///
278 /// # Performance
279 ///
280 /// All messages are deep-cloned, including message body and properties. For a 2KB message,
281 /// each poll returning 32 messages allocates approximately 90KB. At 100 polls per second,
282 /// this results in approximately 9MB/s of allocations.
283 ///
284 /// For workloads that do not require owned messages, [`poll_zero_copy()`] avoids
285 /// this allocation overhead.
286 ///
287 /// # Examples
288 ///
289 /// ```rust,ignore
290 /// let messages = consumer.poll().await;
291 /// my_store.save(messages);
292 /// ```
293 ///
294 /// Returns an empty vector if no messages are available within the timeout period.
295 /// This function does not block the calling thread.
296 async fn poll(&self) -> Vec<MessageExt>;
297
298 /// Fetches the next batch of messages with a specified timeout, returning owned copies.
299 ///
300 /// Behaves identically to [`poll()`], but waits up to `timeout` milliseconds for
301 /// messages to become available. All messages are deep-cloned.
302 ///
303 /// # Arguments
304 ///
305 /// * `timeout` - Maximum time to wait for messages, in milliseconds.
306 ///
307 /// Returns an empty vector if no messages are available before the timeout expires.
308 /// This function does not block the calling thread.
309 async fn poll_with_timeout(&self, timeout: u64) -> Vec<MessageExt>;
310
311 /// Returns the configured message model.
312 async fn message_model(&self) -> MessageModel;
313
314 /// Sets the message model.
315 ///
316 /// This mirrors Java `DefaultLitePullConsumer.setMessageModel`.
317 async fn set_message_model(&self, message_model: MessageModel);
318
319 /// Returns where consumption starts when no offset exists.
320 async fn consume_from_where(&self) -> ConsumeFromWhere;
321
322 /// Sets where consumption starts when no offset exists.
323 ///
324 /// # Errors
325 ///
326 /// Returns an error for legacy `ConsumeFromWhere` values that Java LitePull rejects.
327 async fn set_consume_from_where(&self, consume_from_where: ConsumeFromWhere) -> rocketmq_error::RocketMQResult<()>;
328
329 /// Returns the timestamp used by `ConsumeFromWhere::ConsumeFromTimestamp`.
330 async fn consume_timestamp(&self) -> Option<CheetahString>;
331
332 /// Sets the timestamp used by `ConsumeFromWhere::ConsumeFromTimestamp`.
333 async fn set_consume_timestamp(&self, consume_timestamp: Option<CheetahString>);
334
335 /// Returns the message queue allocation strategy used during rebalance.
336 async fn allocate_message_queue_strategy(&self) -> Arc<dyn AllocateMessageQueueStrategy + Send + Sync>;
337
338 /// Sets the message queue allocation strategy used during rebalance.
339 ///
340 /// This mirrors Java `DefaultLitePullConsumer.setAllocateMessageQueueStrategy`.
341 async fn set_allocate_message_queue_strategy(
342 &self,
343 allocate_message_queue_strategy: Arc<dyn AllocateMessageQueueStrategy + Send + Sync>,
344 );
345
346 /// Returns the listener notified when rebalance changes assigned message queues.
347 async fn message_queue_listener(&self) -> Option<ArcMessageQueueListener>;
348
349 /// Sets the listener notified when rebalance changes assigned message queues.
350 ///
351 /// Passing `None` clears the listener, matching Java's nullable
352 /// `DefaultLitePullConsumer.setMessageQueueListener`.
353 async fn set_message_queue_listener(&self, message_queue_listener: Option<ArcMessageQueueListener>);
354
355 /// Returns the configured offset store, if one has been set or initialized.
356 async fn offset_store(&self) -> Option<ArcMut<OffsetStore>>;
357
358 /// Sets the offset store used by the consumer.
359 ///
360 /// This mirrors Java `DefaultLitePullConsumer.setOffsetStore`. The store
361 /// should be set before startup so load, rebalance, and commit paths share
362 /// the same backend.
363 async fn set_offset_store(&self, offset_store: Option<ArcMut<OffsetStore>>) -> rocketmq_error::RocketMQResult<()>;
364
365 /// Returns the topic metadata check interval in milliseconds.
366 async fn topic_metadata_check_interval_millis(&self) -> u64;
367
368 /// Sets the topic metadata check interval in milliseconds.
369 ///
370 /// Like Java, this applies directly to the stored configuration. If the
371 /// metadata task is already scheduled, the existing task keeps its start-time interval.
372 async fn set_topic_metadata_check_interval_millis(&self, interval_millis: u64);
373
374 /// Returns the default timeout used by [`poll`] in milliseconds.
375 ///
376 /// This function does not block the calling thread.
377 ///
378 /// [`poll`]: LitePullConsumerLocal::poll
379 async fn poll_timeout_millis(&self) -> u64;
380
381 /// Sets the default timeout used by [`poll`] in milliseconds.
382 ///
383 /// This follows the Java client setter and applies the value directly. Use
384 /// [`poll_with_timeout`] for a per-call timeout override.
385 ///
386 /// This function does not block the calling thread.
387 ///
388 /// [`poll`]: LitePullConsumerLocal::poll
389 /// [`poll_with_timeout`]: LitePullConsumerLocal::poll_with_timeout
390 async fn set_poll_timeout_millis(&self, timeout_millis: u64);
391
392 /// Returns the maximum time that the broker may suspend a long-poll pull request.
393 async fn broker_suspend_max_time_millis(&self) -> u64;
394
395 /// Sets the maximum time that the broker may suspend a long-poll pull request.
396 ///
397 /// This follows Java `DefaultLitePullConsumer.setBrokerSuspendMaxTimeMillis`
398 /// and applies the value directly.
399 async fn set_broker_suspend_max_time_millis(&self, timeout_millis: u64);
400
401 /// Returns the consumer-side timeout for suspended long-poll pull requests.
402 async fn consumer_timeout_millis_when_suspend(&self) -> u64;
403
404 /// Sets the consumer-side timeout for suspended long-poll pull requests.
405 ///
406 /// This follows Java `DefaultLitePullConsumer.setConsumerTimeoutMillisWhenSuspend`
407 /// and applies the value directly.
408 async fn set_consumer_timeout_millis_when_suspend(&self, timeout_millis: u64);
409
410 /// Returns the RPC timeout used by lite pull requests in milliseconds.
411 async fn consumer_pull_timeout_millis(&self) -> u64;
412
413 /// Sets the RPC timeout used by lite pull requests in milliseconds.
414 ///
415 /// This follows Java `DefaultLitePullConsumer.setConsumerPullTimeoutMillis`
416 /// and applies the value directly.
417 async fn set_consumer_pull_timeout_millis(&self, timeout_millis: u64);
418
419 /// Returns the maximum number of messages requested in a single pull.
420 async fn pull_batch_size(&self) -> i32;
421
422 /// Sets the maximum number of messages requested in a single pull.
423 ///
424 /// This follows the Java client setter and applies the value directly.
425 async fn set_pull_batch_size(&self, pull_batch_size: i32);
426
427 /// Returns the configured number of pull worker threads.
428 async fn pull_thread_nums(&self) -> usize;
429
430 /// Sets the configured number of pull worker threads.
431 ///
432 /// This mirrors Java `DefaultLitePullConsumer.setPullThreadNums`.
433 async fn set_pull_thread_nums(&self, pull_thread_nums: usize);
434
435 /// Returns the maximum total number of cached messages across all queues.
436 async fn pull_threshold_for_all(&self) -> i64;
437
438 /// Sets the maximum total number of cached messages across all queues.
439 ///
440 /// This follows the Java client setter and applies the value directly.
441 async fn set_pull_threshold_for_all(&self, pull_threshold_for_all: i64);
442
443 /// Returns the maximum cached message count for each queue.
444 async fn pull_threshold_for_queue(&self) -> i64;
445
446 /// Sets the maximum cached message count for each queue.
447 ///
448 /// This follows the Java client setter and applies the value directly.
449 async fn set_pull_threshold_for_queue(&self, pull_threshold_for_queue: i64);
450
451 /// Returns the maximum cached message size in MiB for each queue.
452 async fn pull_threshold_size_for_queue(&self) -> i32;
453
454 /// Sets the maximum cached message size in MiB for each queue.
455 ///
456 /// This follows the Java client setter and applies the value directly.
457 async fn set_pull_threshold_size_for_queue(&self, pull_threshold_size_for_queue: i32);
458
459 /// Returns the maximum allowed offset span in a cached process queue.
460 async fn consume_max_span(&self) -> i64;
461
462 /// Sets the maximum allowed offset span in a cached process queue.
463 ///
464 /// This follows the Java client setter and applies the value directly.
465 async fn set_consume_max_span(&self, consume_max_span: i64);
466
467 /// Returns whether pulls always use the configured default broker ID.
468 async fn is_connect_broker_by_user(&self) -> bool;
469
470 /// Sets whether pulls always use the configured default broker ID.
471 ///
472 /// This mirrors Java `DefaultLitePullConsumer.setConnectBrokerByUser`.
473 async fn set_connect_broker_by_user(&self, connect_broker_by_user: bool);
474
475 /// Returns the broker ID used when user-controlled broker selection is enabled.
476 async fn default_broker_id(&self) -> u64;
477
478 /// Sets the broker ID used when user-controlled broker selection is enabled.
479 ///
480 /// This mirrors Java `DefaultLitePullConsumer.setDefaultBrokerId`.
481 async fn set_default_broker_id(&self, broker_id: u64);
482
483 /// Seeks the fetch position of the specified [`MessageQueue`] to the given offset.
484 ///
485 /// The next [`poll`] invocation will return messages starting from `offset`.
486 /// This function does not block the calling thread.
487 ///
488 /// # Arguments
489 ///
490 /// * `message_queue` - The queue whose fetch position is to be updated.
491 /// * `offset` - The target offset. Must be within the queue's valid range.
492 ///
493 /// # Errors
494 ///
495 /// Returns an error if the queue is not currently assigned to this consumer,
496 /// or if the specified offset is out of the valid range.
497 ///
498 /// [`poll`]: LitePullConsumerLocal::poll
499 async fn seek(&self, message_queue: &MessageQueue, offset: i64) -> rocketmq_error::RocketMQResult<()>;
500
501 /// Suspends message fetching for the specified [`MessageQueue`]s.
502 ///
503 /// Paused queues are excluded from subsequent [`poll`] results until [`resume`] is called.
504 /// This function does not block the calling thread.
505 ///
506 /// # Arguments
507 ///
508 /// * `message_queues` - The queues to pause.
509 ///
510 /// [`poll`]: LitePullConsumerLocal::poll
511 /// [`resume`]: LitePullConsumerLocal::resume
512 async fn pause(&self, message_queues: Vec<MessageQueue>);
513
514 /// Resumes message fetching for the specified [`MessageQueue`]s.
515 ///
516 /// This function does not block the calling thread.
517 ///
518 /// # Arguments
519 ///
520 /// * `message_queues` - The queues to resume.
521 async fn resume(&self, message_queues: Vec<MessageQueue>);
522
523 /// Returns whether automatic offset commit is enabled.
524 ///
525 /// This function does not block the calling thread.
526 async fn is_auto_commit(&self) -> bool;
527
528 /// Enables or disables automatic offset commit.
529 ///
530 /// When auto-commit is enabled, offsets are committed periodically without explicit calls
531 /// to [`commit`] or [`commit_sync`]. When disabled, the caller is responsible for committing
532 /// offsets.
533 ///
534 /// This function does not block the calling thread.
535 ///
536 /// # Arguments
537 ///
538 /// * `auto_commit` - `true` to enable automatic offset commit; `false` to disable it.
539 ///
540 /// [`commit`]: LitePullConsumerLocal::commit
541 /// [`commit_sync`]: LitePullConsumerLocal::commit_sync
542 async fn set_auto_commit(&self, auto_commit: bool);
543
544 /// Returns whether the subscription group runs in unit mode.
545 async fn is_unit_mode(&self) -> bool;
546
547 /// Sets whether the subscription group runs in unit mode.
548 ///
549 /// This mirrors Java `DefaultLitePullConsumer.setUnitMode`.
550 async fn set_unit_mode(&self, unit_mode: bool);
551
552 /// Returns the interval between automatic offset commits in milliseconds.
553 ///
554 /// This function does not block the calling thread.
555 async fn auto_commit_interval_millis(&self) -> u64;
556
557 /// Sets the interval between automatic offset commits in milliseconds.
558 ///
559 /// Values below the Java client minimum of 1000ms are ignored, matching
560 /// `DefaultLitePullConsumer.setAutoCommitIntervalMillis`.
561 ///
562 /// This function does not block the calling thread.
563 ///
564 /// # Arguments
565 ///
566 /// * `interval_millis` - The new auto-commit interval in milliseconds.
567 async fn set_auto_commit_interval_millis(&self, interval_millis: u64);
568
569 /// Queries the broker for all [`MessageQueue`]s belonging to the specified topic.
570 ///
571 /// This function does not block the calling thread.
572 ///
573 /// # Arguments
574 ///
575 /// * `topic` - The name of the topic to query.
576 ///
577 /// # Errors
578 ///
579 /// Returns an error if the topic does not exist, if the name server is unreachable,
580 /// or if the consumer is not in the running state.
581 async fn fetch_message_queues(&self, topic: &str) -> rocketmq_error::RocketMQResult<Vec<MessageQueue>>;
582
583 /// Queries the broker for the offset corresponding to the given timestamp in a queue.
584 ///
585 /// This function does not block the calling thread.
586 ///
587 /// # Arguments
588 ///
589 /// * `message_queue` - The queue to query.
590 /// * `timestamp` - The Unix timestamp in milliseconds. The broker returns the offset of the
591 /// first message stored at or after this timestamp.
592 ///
593 /// # Errors
594 ///
595 /// Returns an error if the queue is not found on the broker or the query fails.
596 async fn offset_for_timestamp(
597 &self,
598 message_queue: &MessageQueue,
599 timestamp: u64,
600 ) -> rocketmq_error::RocketMQResult<i64>;
601
602 /// Queries the broker for the earliest message store time of the specified queue.
603 ///
604 /// This function does not block the calling thread.
605 ///
606 /// # Arguments
607 ///
608 /// * `message_queue` - The queue to query.
609 ///
610 /// # Errors
611 ///
612 /// Returns an error if the queue is not found on the broker, the consumer is not running,
613 /// or this LitePull implementation does not support broker offset metadata queries.
614 async fn earliest_msg_store_time(&self, message_queue: &MessageQueue) -> rocketmq_error::RocketMQResult<i64>;
615
616 /// Queries the broker for the current maximum offset of the specified queue.
617 ///
618 /// This function does not block the calling thread.
619 ///
620 /// # Arguments
621 ///
622 /// * `message_queue` - The queue to query.
623 ///
624 /// # Errors
625 ///
626 /// Returns an error if the queue is not found on the broker, the consumer is not running,
627 /// or this LitePull implementation does not support broker offset metadata queries.
628 async fn max_offset(&self, message_queue: &MessageQueue) -> rocketmq_error::RocketMQResult<i64>;
629
630 /// Queries the broker for the current minimum offset of the specified queue.
631 ///
632 /// This function does not block the calling thread.
633 ///
634 /// # Arguments
635 ///
636 /// * `message_queue` - The queue to query.
637 ///
638 /// # Errors
639 ///
640 /// Returns an error if the queue is not found on the broker, the consumer is not running,
641 /// or this LitePull implementation does not support broker offset metadata queries.
642 async fn min_offset(&self, message_queue: &MessageQueue) -> rocketmq_error::RocketMQResult<i64>;
643
644 /// Commits all consumed offsets to the consumer offset store.
645 ///
646 /// This function does not block the calling thread.
647 ///
648 /// # Deprecation
649 ///
650 /// This method is deprecated. The name implies broker-synchronous persistence, but Java
651 /// `DefaultLitePullConsumer.commitSync()` delegates to `commitAll()` and only updates the
652 /// consumer offset store. Use [`commit`] for the modern equivalent.
653 ///
654 /// [`commit`]: LitePullConsumerLocal::commit
655 async fn commit_sync(&self);
656
657 /// Commits the provided offsets and optionally persists them to the broker.
658 ///
659 /// This function does not block the calling thread.
660 ///
661 /// # Deprecation
662 ///
663 /// This method is deprecated. The name implies synchronous behavior, but the underlying
664 /// implementation relies on a background thread to commit offsets rather than committing
665 /// synchronously. Use [`commit_with_map`] instead.
666 ///
667 /// # Arguments
668 ///
669 /// * `offset_map` - A map from [`MessageQueue`] to the offset to commit.
670 /// * `persist` - When `true`, the committed offsets are persisted to the broker immediately.
671 ///
672 /// [`commit_with_map`]: LitePullConsumerLocal::commit_with_map
673 async fn commit_sync_with_map(&self, offset_map: HashMap<MessageQueue, i64>, persist: bool);
674
675 /// Commits all consumed offsets to the consumer offset store.
676 ///
677 /// This function does not block the calling thread. Broker or local-file persistence is
678 /// performed by the normal periodic persistence task, shutdown, or explicit
679 /// `commit_with_map` / `commit_with_set` calls with `persist = true`.
680 ///
681 /// [`commit_sync`]: LitePullConsumerLocal::commit_sync
682 async fn commit(&self);
683
684 /// Commits the provided offsets asynchronously, optionally persisting them to the broker.
685 ///
686 /// This function does not block the calling thread.
687 ///
688 /// # Arguments
689 ///
690 /// * `offset_map` - A map from [`MessageQueue`] to the offset to commit.
691 /// * `persist` - When `true`, the committed offsets are persisted to the broker.
692 async fn commit_with_map(&self, offset_map: HashMap<MessageQueue, i64>, persist: bool);
693
694 /// Commits the offsets for the specified subset of assigned queues.
695 ///
696 /// This function does not block the calling thread.
697 ///
698 /// # Arguments
699 ///
700 /// * `message_queues` - The queues whose current offsets are to be committed.
701 /// * `persist` - When `true`, the committed offsets are persisted to the broker.
702 async fn commit_with_set(&self, message_queues: HashSet<MessageQueue>, persist: bool);
703
704 /// Returns the last committed offset for the specified [`MessageQueue`].
705 ///
706 /// This function does not block the calling thread.
707 ///
708 /// # Arguments
709 ///
710 /// * `message_queue` - The queue to query.
711 ///
712 /// # Errors
713 ///
714 /// Returns an error if the queue is not assigned to this consumer or if the offset
715 /// cannot be retrieved from the offset store.
716 async fn committed(&self, message_queue: &MessageQueue) -> rocketmq_error::RocketMQResult<i64>;
717
718 /// Registers a listener that is notified when the set of [`MessageQueue`]s for a topic changes.
719 ///
720 /// This function does not block the calling thread.
721 ///
722 /// # Arguments
723 ///
724 /// * `topic` - The topic to monitor for queue changes.
725 /// * `listener` - A [`TopicMessageQueueChangeListener`] invoked when the queue set changes.
726 ///
727 /// # Errors
728 ///
729 /// Returns an error if a listener is already registered for the given topic, or if the
730 /// registration fails due to an internal error.
731 async fn register_topic_message_queue_change_listener<TL>(
732 &self,
733 topic: &str,
734 listener: TL,
735 ) -> rocketmq_error::RocketMQResult<()>
736 where
737 TL: TopicMessageQueueChangeListener + 'static;
738
739 /// Updates the name server address used for topic route discovery.
740 ///
741 /// This function does not block the calling thread.
742 ///
743 /// # Arguments
744 ///
745 /// * `name_server_address` - The new semicolon-separated name server address list.
746 async fn update_name_server_address(&self, name_server_address: &str);
747
748 /// Seeks the fetch position of the specified [`MessageQueue`] to its earliest available offset.
749 ///
750 /// This function does not block the calling thread.
751 ///
752 /// # Arguments
753 ///
754 /// * `message_queue` - The queue to seek to the beginning.
755 ///
756 /// # Errors
757 ///
758 /// Returns an error if the queue is not assigned to this consumer or if the earliest
759 /// offset cannot be retrieved from the broker.
760 async fn seek_to_begin(&self, message_queue: &MessageQueue) -> rocketmq_error::RocketMQResult<()>;
761
762 /// Seeks the fetch position of the specified [`MessageQueue`] to its latest available offset.
763 ///
764 /// The next [`poll`] call will return only messages published after this point.
765 /// This function does not block the calling thread.
766 ///
767 /// # Arguments
768 ///
769 /// * `message_queue` - The queue to seek to the end.
770 ///
771 /// # Errors
772 ///
773 /// Returns an error if the queue is not assigned to this consumer or if the latest
774 /// offset cannot be retrieved from the broker.
775 ///
776 /// [`poll`]: LitePullConsumerLocal::poll
777 async fn seek_to_end(&self, message_queue: &MessageQueue) -> rocketmq_error::RocketMQResult<()>;
778
779 /// Commits all consumed offsets for all assigned queues to the local offset store.
780 ///
781 /// This method commits the current consumption offset for every assigned [`MessageQueue`].
782 /// It follows the Java client `commitAll()` behavior: offsets are updated in the consumer's
783 /// offset store, while broker or local-file persistence is handled by the normal periodic
784 /// persistence task, shutdown, or explicit `commit_with_map` / `commit_with_set` calls with
785 /// `persist = true`.
786 ///
787 /// This function does not block the calling thread.
788 ///
789 /// # Errors
790 ///
791 /// Returns an error if the consumer is not in the running state.
792 ///
793 /// [`commit`]: LitePullConsumerLocal::commit
794 async fn commit_all(&self) -> rocketmq_error::RocketMQResult<()>;
795
796 /// Checks whether a specific [`MessageQueue`] is currently paused.
797 ///
798 /// A paused queue will not be fetched from during [`poll`] operations until it is resumed.
799 ///
800 /// This function does not block the calling thread.
801 ///
802 /// # Arguments
803 ///
804 /// * `message_queue` - The queue to check.
805 ///
806 /// # Returns
807 ///
808 /// `true` if the queue is paused, `false` otherwise.
809 ///
810 /// [`poll`]: LitePullConsumerLocal::poll
811 async fn is_paused(&self, message_queue: &MessageQueue) -> bool;
812}