1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use cheetah_string::CheetahString;
use crate::consumer::listener::message_listener_concurrently::MessageListenerConcurrently;
use crate::consumer::listener::message_listener_orderly::MessageListenerOrderly;
use crate::consumer::message_selector::MessageSelector;
use crate::consumer::mq_consumer::MQConsumer;
/// Defines the push-consumer interface for RocketMQ.
///
/// Implementations receive messages from brokers and dispatch them to the
/// registered concurrent or orderly listener.
#[allow(async_fn_in_trait)]
pub trait MQPushConsumer: MQConsumer {
/// Asynchronously starts the consumer instance.
///
/// This method does not block the calling thread.
///
/// # Returns
///
/// Returns `Ok(())` when startup succeeds, or an error when initialization
/// fails.
async fn start(&mut self) -> crate::ClientResult<()>;
/// Asynchronously shuts down the consumer instance.
///
/// This method does not block the calling thread.
async fn shutdown(&mut self);
/// Registers a message listener for concurrent message consumption.
///
/// # Parameters
///
/// * `message_listener` - A closure that processes a batch of messages and returns a status.
///
/// # Type Parameters
///
/// * `ML` - The listener type.
fn register_message_listener_concurrently<ML>(&mut self, message_listener: ML)
where
ML: MessageListenerConcurrently + Send + Sync + 'static;
/// Registers a message listener for orderly message consumption.
///
/// # Parameters
///
/// * `message_listener` - A closure that processes a batch of messages and returns a status.
///
/// # Type Parameters
///
/// * `ML` - The listener type.
fn register_message_listener_orderly<ML>(&mut self, message_listener: ML)
where
ML: MessageListenerOrderly + Send + Sync + 'static;
/// Asynchronously subscribes to a topic with a subscription expression.
///
/// This method does not block the calling thread.
///
/// # Parameters
///
/// * `topic` - The topic to subscribe to. Can be `&str`, `String`, or `CheetahString`.
/// * `sub_expression` - The subscription expression. Can be `&str`, `String`, or
/// `CheetahString`.
///
/// # Returns
///
/// Returns `Ok(())` when the subscription is accepted, or an error when the
/// subscription request is invalid or cannot be applied.
///
/// # Examples
///
/// ```rust,ignore
/// // Using &str
/// consumer.subscribe("TopicTest", "*").await?;
///
/// // Using String
/// let topic = String::from("TopicTest");
/// consumer.subscribe(topic, "*").await?;
///
/// // Using CheetahString
/// let topic = CheetahString::from_slice("TopicTest");
/// consumer.subscribe(topic, "*").await?;
/// ```
async fn subscribe(
&mut self,
topic: impl Into<CheetahString>,
sub_expression: impl Into<CheetahString>,
) -> crate::ClientResult<()>;
/// Asynchronously subscribes to a topic with an optional selector.
///
/// This method does not block the calling thread.
///
/// # Parameters
///
/// * `topic` - The topic to subscribe to.
/// * `selector` - An optional message selector.
///
/// # Returns
///
/// Returns `Ok(())` when the subscription is accepted, or an error when the
/// selector is invalid or cannot be applied.
async fn subscribe_with_selector(
&mut self,
topic: &str,
selector: Option<MessageSelector>,
) -> crate::ClientResult<()>;
/// Asynchronously unsubscribes from a topic.
///
/// This method does not block the calling thread.
///
/// # Parameters
///
/// * `topic` - The topic to unsubscribe from.
async fn unsubscribe(&mut self, topic: &str);
/// Asynchronously suspends message consumption.
///
/// This method does not block the calling thread.
async fn suspend(&self);
/// Asynchronously resumes message consumption.
///
/// This method does not block the calling thread.
async fn resume(&self);
/// Updates the core size used by the concurrent consume worker pool.
///
/// Orderly consumers and implementations without a resizable worker pool may
/// ignore this request.
fn update_core_pool_size(&self, _core_pool_size: usize) -> crate::ClientResult<()> {
Err(crate::mq_client_err!(
"updateCorePoolSize is not supported by this MQPushConsumer implementation"
))
}
}