|
48 | 48 | import _pulsar |
49 | 49 |
|
50 | 50 | from _pulsar import Result, CompressionType, ConsumerType, InitialPosition, PartitionsRoutingMode, BatchingType, \ |
51 | | - LoggerLevel, BatchReceivePolicy, KeySharedPolicy, KeySharedMode # noqa: F401 |
| 51 | + LoggerLevel, BatchReceivePolicy, KeySharedPolicy, KeySharedMode, DeadLetterPolicy, DeadLetterPolicyBuilder # noqa: F401 |
52 | 52 |
|
53 | 53 | from pulsar.__about__ import __version__ |
54 | 54 |
|
@@ -374,6 +374,64 @@ def __init__(self, username=None, password=None, method='basic', auth_params_str |
374 | 374 | _check_type(str, method, 'method') |
375 | 375 | self.auth = _pulsar.AuthenticationBasic.create(username, password, method) |
376 | 376 |
|
| 377 | +class ConsumerDeadLetterPolicy: |
| 378 | + """ |
| 379 | + Configuration for the "dead letter queue" feature in consumer. |
| 380 | + """ |
| 381 | + def __init__(self, dead_letter_topic: str = None, |
| 382 | + max_redeliver_count: int = None, |
| 383 | + initial_subscription_name: str = None): |
| 384 | + """ |
| 385 | + Wrapper DeadLetterPolicy. |
| 386 | +
|
| 387 | + Parameters |
| 388 | + ---------- |
| 389 | + dead_letter_topic: Name of the dead topic where the failing messages are sent. |
| 390 | + The default value is: sourceTopicName + "-" + subscriptionName + "-DLQ" |
| 391 | + max_redeliver_count: Maximum number of times that a message is redelivered before being sent to the dead letter queue. |
| 392 | + - The maxRedeliverCount must be greater than 0. |
| 393 | + - The default value is None (DLQ is not enabled) |
| 394 | + initial_subscription_name: Name of the initial subscription name of the dead letter topic. |
| 395 | + If this field is not set, the initial subscription for the dead letter topic is not created. |
| 396 | + If this field is set but the broker's `allowAutoSubscriptionCreation` is disabled, the DLQ producer |
| 397 | + fails to be created. |
| 398 | + """ |
| 399 | + builder = DeadLetterPolicyBuilder() |
| 400 | + if dead_letter_topic is not None: |
| 401 | + builder.deadLetterTopic(dead_letter_topic) |
| 402 | + if max_redeliver_count is not None: |
| 403 | + builder.maxRedeliverCount(max_redeliver_count) |
| 404 | + if initial_subscription_name is not None: |
| 405 | + builder.initialSubscriptionName(initial_subscription_name) |
| 406 | + self._policy = builder.build() |
| 407 | + |
| 408 | + @property |
| 409 | + def dead_letter_topic(self) -> str: |
| 410 | + """ |
| 411 | + Return the dead letter topic for dead letter policy. |
| 412 | + """ |
| 413 | + return self._policy.getDeadLetterTopic() |
| 414 | + |
| 415 | + @property |
| 416 | + def max_redeliver_count(self) -> int: |
| 417 | + """ |
| 418 | + Return the max redeliver count for dead letter policy. |
| 419 | + """ |
| 420 | + return self._policy.getMaxRedeliverCount() |
| 421 | + |
| 422 | + @property |
| 423 | + def initial_subscription_name(self) -> str: |
| 424 | + """ |
| 425 | + Return the initial subscription name for dead letter policy. |
| 426 | + """ |
| 427 | + return self._policy.getInitialSubscriptionName() |
| 428 | + |
| 429 | + def policy(self): |
| 430 | + """ |
| 431 | + Returns the actual one DeadLetterPolicy. |
| 432 | + """ |
| 433 | + return self._policy |
| 434 | + |
377 | 435 | class Client: |
378 | 436 | """ |
379 | 437 | The Pulsar client. A single client instance can be used to create producers |
@@ -694,6 +752,7 @@ def subscribe(self, topic, subscription_name, |
694 | 752 | batch_receive_policy=None, |
695 | 753 | key_shared_policy=None, |
696 | 754 | batch_index_ack_enabled=False, |
| 755 | + dead_letter_policy: ConsumerDeadLetterPolicy = None, |
697 | 756 | ): |
698 | 757 | """ |
699 | 758 | Subscribe to the given topic and subscription combination. |
@@ -783,6 +842,12 @@ def my_listener(consumer, message): |
783 | 842 | batch_index_ack_enabled: Enable the batch index acknowledgement. |
784 | 843 | It should be noted that this option can only work when the broker side also enables the batch index |
785 | 844 | acknowledgement. See the `acknowledgmentAtBatchIndexLevelEnabled` config in `broker.conf`. |
| 845 | + dead_letter_policy: class ConsumerDeadLetterPolicy |
| 846 | + Set dead letter policy for consumer. |
| 847 | + By default, some messages are redelivered many times, even to the extent that they can never be |
| 848 | + stopped. By using the dead letter mechanism, messages have the max redelivery count, when they're |
| 849 | + exceeding the maximum number of redeliveries. Messages are sent to dead letter topics and acknowledged |
| 850 | + automatically. |
786 | 851 | """ |
787 | 852 | _check_type(str, subscription_name, 'subscription_name') |
788 | 853 | _check_type(ConsumerType, consumer_type, 'consumer_type') |
@@ -840,6 +905,8 @@ def my_listener(consumer, message): |
840 | 905 | if key_shared_policy: |
841 | 906 | conf.key_shared_policy(key_shared_policy.policy()) |
842 | 907 | conf.batch_index_ack_enabled(batch_index_ack_enabled) |
| 908 | + if dead_letter_policy: |
| 909 | + conf.dead_letter_policy(dead_letter_policy.policy()) |
843 | 910 |
|
844 | 911 | c = Consumer() |
845 | 912 | if isinstance(topic, str): |
|
0 commit comments