rocketmq_client_rust/consumer/mq_push_consumer.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use rocketmq_common::common::message::message_ext::MessageExt;
18
19use crate::consumer::listener::consume_concurrently_context::ConsumeConcurrentlyContext;
20use crate::consumer::listener::consume_concurrently_status::ConsumeConcurrentlyStatus;
21use crate::consumer::listener::consume_orderly_context::ConsumeOrderlyContext;
22use crate::consumer::listener::consume_orderly_status::ConsumeOrderlyStatus;
23use crate::consumer::listener::message_listener_concurrently::MessageListenerConcurrently;
24use crate::consumer::listener::message_listener_orderly::MessageListenerOrderly;
25use crate::consumer::message_selector::MessageSelector;
26use crate::consumer::mq_consumer::MQConsumer;
27
28/// The `MQPushConsumer` trait defines the interface for a push consumer in RocketMQ.
29/// A push consumer receives messages from the broker and processes them using registered listeners.
30#[allow(async_fn_in_trait)]
31pub trait MQPushConsumer: MQConsumer {
32 /// Starts the push consumer.
33 ///
34 /// # Returns
35 ///
36 /// * `rocketmq_error::RocketMQResult<()>` - An empty result indicating success or an error.
37 async fn start(&mut self) -> rocketmq_error::RocketMQResult<()>;
38
39 /// Shuts down the push consumer.
40 async fn shutdown(&mut self);
41
42 /// Registers a message listener for concurrent message consumption.
43 ///
44 /// # Parameters
45 ///
46 /// * `message_listener` - A closure that processes a batch of messages and returns a status.
47 ///
48 /// # Type Parameters
49 ///
50 /// * `MLC` - The type of the message listener closure.
51 fn register_message_listener_concurrently_fn<MLCFN>(&mut self, message_listener: MLCFN)
52 where
53 MLCFN: Fn(
54 Vec<MessageExt>,
55 ConsumeConcurrentlyContext,
56 ) -> rocketmq_error::RocketMQResult<ConsumeConcurrentlyStatus>
57 + Send
58 + Sync;
59
60 fn register_message_listener_concurrently<ML>(&mut self, message_listener: ML)
61 where
62 ML: MessageListenerConcurrently + Send + Sync + 'static;
63
64 /// Registers a message listener for orderly message consumption.
65 ///
66 /// # Parameters
67 ///
68 /// * `message_listener` - A closure that processes a batch of messages and returns a status.
69 ///
70 /// # Type Parameters
71 ///
72 /// * `MLO` - The type of the message listener closure.
73 async fn register_message_listener_orderly_fn<MLOFN>(&mut self, message_listener: MLOFN)
74 where
75 MLOFN: Fn(
76 Vec<MessageExt>,
77 ConsumeOrderlyContext,
78 ) -> rocketmq_error::RocketMQResult<ConsumeOrderlyStatus>
79 + Send
80 + Sync;
81
82 fn register_message_listener_orderly<ML>(&mut self, message_listener: ML)
83 where
84 ML: MessageListenerOrderly + Send + Sync + 'static;
85
86 /// Subscribes to a topic with a subscription expression.
87 ///
88 /// # Parameters
89 ///
90 /// * `topic` - The topic to subscribe to.
91 /// * `sub_expression` - The subscription expression.
92 ///
93 /// # Returns
94 ///
95 /// * `rocketmq_error::RocketMQResult<()>` - An empty result indicating success or an error.
96 fn subscribe(
97 &mut self,
98 topic: &str,
99 sub_expression: &str,
100 ) -> rocketmq_error::RocketMQResult<()>;
101
102 /// Subscribes to a topic with an optional message selector.
103 ///
104 /// # Parameters
105 ///
106 /// * `topic` - The topic to subscribe to.
107 /// * `selector` - An optional message selector.
108 ///
109 /// # Returns
110 ///
111 /// * `rocketmq_error::RocketMQResult<()>` - An empty result indicating success or an error.
112 async fn subscribe_with_selector(
113 &mut self,
114 topic: &str,
115 selector: Option<MessageSelector>,
116 ) -> rocketmq_error::RocketMQResult<()>;
117
118 /// Unsubscribes from a topic.
119 ///
120 /// # Parameters
121 ///
122 /// * `topic` - The topic to unsubscribe from.
123 async fn unsubscribe(&mut self, topic: &str);
124
125 /// Suspends the push consumer.
126 async fn suspend(&mut self);
127
128 /// Resumes the push consumer.
129 async fn resume(&mut self);
130}