|
14 | 14 |
|
15 | 15 | /// A Pulsar consumer used to asynchronously consume messages from a specific topic.
|
16 | 16 | ///
|
17 |
| -/// This class provides support for consuming messages from a Pulsar topic using various subscription types. |
18 |
| -/// It conforms to `AsyncSequence`, enabling iteration over received messages in an asynchronous context. |
19 |
| -/// Generic `T` represents the type of payload for the messages, conforming to `PulsarPayload`. |
| 17 | +/// This class provides functionality for consuming messages from an Apache Pulsar topic. |
| 18 | +/// It supports different subscription types and conforms to `AsyncSequence`, allowing |
| 19 | +/// messages to be iterated over in an asynchronous context using Swift's `for await` syntax. |
| 20 | +/// |
| 21 | +/// ## Features: |
| 22 | +/// - Conforms to `AsyncSequence`, enabling structured and idiomatic message consumption. |
| 23 | +/// - Handles message acknowledgment automatically (if `autoAcknowledge` is enabled). |
| 24 | +/// - Supports schema-based payload deserialization. |
| 25 | +/// - Provides explicit error handling mechanisms. |
| 26 | +/// |
| 27 | +/// ## Usage Example: |
| 28 | +/// ```swift |
| 29 | +/// let consumer = PulsarConsumer<MyPayload>( |
| 30 | +/// autoAck: true, |
| 31 | +/// handler: myHandler, |
| 32 | +/// consumerID: 67890, |
| 33 | +/// topic: "persistent://public/default/my-topic", |
| 34 | +/// subscriptionName: "my-subscription", |
| 35 | +/// subscriptionType: .shared, |
| 36 | +/// subscriptionMode: .durable, |
| 37 | +/// schema: mySchema |
| 38 | +/// ) |
| 39 | +/// |
| 40 | +/// for await message in consumer { |
| 41 | +/// print("Received message: \(message.payload)") |
| 42 | +/// } |
| 43 | +/// |
| 44 | +/// try await consumer.close() // Close the consumer when done |
| 45 | +/// ``` |
| 46 | +/// |
| 47 | +/// ## Lifecycle: |
| 48 | +/// - The consumer is initialized with a handler, topic, subscription details, and schema. |
| 49 | +/// - Messages are received and decoded using the specified schema. |
| 50 | +/// - The consumer continuously yields messages via `AsyncThrowingStream<Message<T>, Error>`. |
| 51 | +/// - The consumer can be explicitly closed using `close()`, ensuring proper resource cleanup. |
| 52 | +/// |
| 53 | +/// ## Error Handling: |
| 54 | +/// - If message deserialization fails, the consumer will call `fail(error:)`, terminating the stream. |
| 55 | +/// - If an error occurs while handling messages, the stream finishes with the provided error. |
| 56 | +/// - Closing the consumer ensures proper detachment from the Pulsar client. |
| 57 | +/// |
| 58 | +/// - Note: This class is designed to be `Sendable`, meaning it can be safely used in concurrent contexts. |
| 59 | +/// |
| 60 | +/// - Parameters: |
| 61 | +/// - T: A type conforming to ``PulsarPayload``, representing the message payload. |
| 62 | +/// |
| 63 | +/// - SeeAlso: ``PulsarProducer`` for message publishing. |
| 64 | +/// |
20 | 65 | public final class PulsarConsumer<T: PulsarPayload>: AsyncSequence, Sendable, AnyConsumer {
|
21 | 66 | public let consumerID: UInt64
|
22 | 67 | let autoAcknowledge: Bool
|
|
0 commit comments