|
23 | 23 |
|
24 | 24 | import asyncio |
25 | 25 | import functools |
26 | | -from typing import Any |
| 26 | +from typing import Any, List, Union |
27 | 27 |
|
28 | 28 | import _pulsar |
| 29 | +from _pulsar import InitialPosition |
29 | 30 | import pulsar |
30 | 31 |
|
31 | 32 | class PulsarException(BaseException): |
@@ -116,6 +117,134 @@ async def close(self) -> None: |
116 | 117 | self._producer.close_async(functools.partial(_set_future, future, value=None)) |
117 | 118 | await future |
118 | 119 |
|
| 120 | +class Consumer: |
| 121 | + """ |
| 122 | + The Pulsar message consumer, used to subscribe to messages from a topic. |
| 123 | + """ |
| 124 | + |
| 125 | + def __init__(self, consumer: _pulsar.Consumer) -> None: |
| 126 | + """ |
| 127 | + Create the consumer. |
| 128 | + Users should not call this constructor directly. Instead, create the |
| 129 | + consumer via `Client.subscribe`. |
| 130 | +
|
| 131 | + Parameters |
| 132 | + ---------- |
| 133 | + consumer: _pulsar.Consumer |
| 134 | + The underlying Consumer object from the C extension. |
| 135 | + """ |
| 136 | + self._consumer: _pulsar.Consumer = consumer |
| 137 | + |
| 138 | + async def receive(self) -> pulsar.Message: |
| 139 | + """ |
| 140 | + Receive a single message asynchronously. |
| 141 | +
|
| 142 | + Returns |
| 143 | + ------- |
| 144 | + pulsar.Message |
| 145 | + The message received. |
| 146 | +
|
| 147 | + Raises |
| 148 | + ------ |
| 149 | + PulsarException |
| 150 | + """ |
| 151 | + future = asyncio.get_running_loop().create_future() |
| 152 | + self._consumer.receive_async(functools.partial(_set_future, future)) |
| 153 | + msg = await future |
| 154 | + m = pulsar.Message() |
| 155 | + m._message = msg |
| 156 | + m._schema = pulsar.schema.BytesSchema() |
| 157 | + return m |
| 158 | + |
| 159 | + async def acknowledge(self, message: Union[pulsar.Message, pulsar.MessageId, _pulsar.Message, _pulsar.MessageId]) -> None: |
| 160 | + """ |
| 161 | + Acknowledge the reception of a single message asynchronously. |
| 162 | +
|
| 163 | + Parameters |
| 164 | + ---------- |
| 165 | + message : Message, MessageId, _pulsar.Message, _pulsar.MessageId |
| 166 | + The received message or message id. |
| 167 | +
|
| 168 | + Raises |
| 169 | + ------ |
| 170 | + PulsarException |
| 171 | + """ |
| 172 | + future = asyncio.get_running_loop().create_future() |
| 173 | + if isinstance(message, pulsar.Message): |
| 174 | + msg = message._message |
| 175 | + elif isinstance(message, pulsar.MessageId): |
| 176 | + msg = message._msg_id |
| 177 | + else: |
| 178 | + msg = message |
| 179 | + self._consumer.acknowledge_async(msg, functools.partial(_set_future, future, value=None)) |
| 180 | + await future |
| 181 | + |
| 182 | + async def acknowledge_cumulative(self, message: Union[pulsar.Message, pulsar.MessageId, _pulsar.Message, _pulsar.MessageId]) -> None: |
| 183 | + """ |
| 184 | + Acknowledge the reception of all the messages in the stream up to (and |
| 185 | + including) the provided message asynchronously. |
| 186 | +
|
| 187 | + Parameters |
| 188 | + ---------- |
| 189 | + message : Message, MessageId, _pulsar.Message, _pulsar.MessageId |
| 190 | + The received message or message id. |
| 191 | +
|
| 192 | + Raises |
| 193 | + ------ |
| 194 | + PulsarException |
| 195 | + """ |
| 196 | + future = asyncio.get_running_loop().create_future() |
| 197 | + if isinstance(message, pulsar.Message): |
| 198 | + msg = message._message |
| 199 | + elif isinstance(message, pulsar.MessageId): |
| 200 | + msg = message._msg_id |
| 201 | + else: |
| 202 | + msg = message |
| 203 | + self._consumer.acknowledge_cumulative_async(msg, functools.partial(_set_future, future, value=None)) |
| 204 | + await future |
| 205 | + |
| 206 | + async def unsubscribe(self) -> None: |
| 207 | + """ |
| 208 | + Unsubscribe the current consumer from the topic asynchronously. |
| 209 | +
|
| 210 | + Raises |
| 211 | + ------ |
| 212 | + PulsarException |
| 213 | + """ |
| 214 | + future = asyncio.get_running_loop().create_future() |
| 215 | + self._consumer.unsubscribe_async(functools.partial(_set_future, future, value=None)) |
| 216 | + await future |
| 217 | + |
| 218 | + async def close(self) -> None: |
| 219 | + """ |
| 220 | + Close the consumer asynchronously. |
| 221 | +
|
| 222 | + Raises |
| 223 | + ------ |
| 224 | + PulsarException |
| 225 | + """ |
| 226 | + future = asyncio.get_running_loop().create_future() |
| 227 | + self._consumer.close_async(functools.partial(_set_future, future, value=None)) |
| 228 | + await future |
| 229 | + |
| 230 | + def topic(self) -> str: |
| 231 | + """ |
| 232 | + Return the topic this consumer is subscribed to. |
| 233 | + """ |
| 234 | + return self._consumer.topic() |
| 235 | + |
| 236 | + def subscription_name(self) -> str: |
| 237 | + """ |
| 238 | + Return the subscription name. |
| 239 | + """ |
| 240 | + return self._consumer.subscription_name() |
| 241 | + |
| 242 | + def consumer_name(self) -> str: |
| 243 | + """ |
| 244 | + Return the consumer name. |
| 245 | + """ |
| 246 | + return self._consumer.consumer_name() |
| 247 | + |
119 | 248 | class Client: |
120 | 249 | """ |
121 | 250 | The asynchronous version of `pulsar.Client`. |
@@ -151,6 +280,53 @@ async def create_producer(self, topic: str) -> Producer: |
151 | 280 | self._client.create_producer_async(topic, conf, functools.partial(_set_future, future)) |
152 | 281 | return Producer(await future) |
153 | 282 |
|
| 283 | + async def subscribe(self, topic: Union[str, List[str]], subscription_name: str, |
| 284 | + is_pattern_topic: bool = False, |
| 285 | + consumer_type: pulsar.ConsumerType = pulsar.ConsumerType.Exclusive, |
| 286 | + initial_position: InitialPosition = InitialPosition.Latest) -> Consumer: |
| 287 | + """ |
| 288 | + Subscribe to the given topic and subscription combination. |
| 289 | +
|
| 290 | + Parameters |
| 291 | + ---------- |
| 292 | + topic: str, List[str], or regex pattern |
| 293 | + The name of the topic, list of topics or regex pattern. |
| 294 | + subscription_name: str |
| 295 | + The name of the subscription. |
| 296 | + is_pattern_topic: bool, default=False |
| 297 | + Whether `topic` is a regex pattern. This option takes no effect when `topic` is a list of topics. |
| 298 | + consumer_type: pulsar.ConsumerType, default=pulsar.ConsumerType.Exclusive |
| 299 | + Select the subscription type to be used when subscribing to the topic. |
| 300 | + initial_position: InitialPosition, default=InitialPosition.Latest |
| 301 | + Set the initial position of a consumer when subscribing to the topic. |
| 302 | + It could be either: ``InitialPosition.Earliest`` or ``InitialPosition.Latest``. |
| 303 | +
|
| 304 | + Returns |
| 305 | + ------- |
| 306 | + Consumer |
| 307 | + The consumer created |
| 308 | +
|
| 309 | + Raises |
| 310 | + ------ |
| 311 | + PulsarException |
| 312 | + """ |
| 313 | + future = asyncio.get_running_loop().create_future() |
| 314 | + conf = _pulsar.ConsumerConfiguration() |
| 315 | + conf.consumer_type(consumer_type) |
| 316 | + conf.subscription_initial_position(initial_position) |
| 317 | + |
| 318 | + if isinstance(topic, str): |
| 319 | + if is_pattern_topic: |
| 320 | + self._client.subscribe_async_pattern(topic, subscription_name, conf, functools.partial(_set_future, future)) |
| 321 | + else: |
| 322 | + self._client.subscribe_async(topic, subscription_name, conf, functools.partial(_set_future, future)) |
| 323 | + elif isinstance(topic, list): |
| 324 | + self._client.subscribe_async_topics(topic, subscription_name, conf, functools.partial(_set_future, future)) |
| 325 | + else: |
| 326 | + raise ValueError("Argument 'topic' is expected to be of a type between (str, list)") |
| 327 | + |
| 328 | + return Consumer(await future) |
| 329 | + |
154 | 330 | async def close(self) -> None: |
155 | 331 | """ |
156 | 332 | Close the client and all the associated producers and consumers |
|
0 commit comments