|
48 | 48 | import _pulsar |
49 | 49 |
|
50 | 50 | from _pulsar import Result, CompressionType, ConsumerType, InitialPosition, PartitionsRoutingMode, BatchingType, \ |
51 | | - LoggerLevel, BatchReceivePolicy, KeySharedPolicy, KeySharedMode, ProducerAccessMode, RegexSubscriptionMode # noqa: F401 |
| 51 | + LoggerLevel, BatchReceivePolicy, KeySharedPolicy, KeySharedMode, ProducerAccessMode, RegexSubscriptionMode, \ |
| 52 | + DeadLetterPolicyBuilder # noqa: F401 |
52 | 53 |
|
53 | 54 | from pulsar.__about__ import __version__ |
54 | 55 |
|
@@ -374,6 +375,64 @@ def __init__(self, username=None, password=None, method='basic', auth_params_str |
374 | 375 | _check_type(str, method, 'method') |
375 | 376 | self.auth = _pulsar.AuthenticationBasic.create(username, password, method) |
376 | 377 |
|
| 378 | +class ConsumerDeadLetterPolicy: |
| 379 | + """ |
| 380 | + Configuration for the "dead letter queue" feature in consumer. |
| 381 | + """ |
| 382 | + def __init__(self, dead_letter_topic: str = None, |
| 383 | + max_redeliver_count: int = None, |
| 384 | + initial_subscription_name: str = None): |
| 385 | + """ |
| 386 | + Wrapper DeadLetterPolicy. |
| 387 | +
|
| 388 | + Parameters |
| 389 | + ---------- |
| 390 | + dead_letter_topic: Name of the dead topic where the failing messages are sent. |
| 391 | + The default value is: sourceTopicName + "-" + subscriptionName + "-DLQ" |
| 392 | + max_redeliver_count: Maximum number of times that a message is redelivered before being sent to the dead letter queue. |
| 393 | + - The maxRedeliverCount must be greater than 0. |
| 394 | + - The default value is None (DLQ is not enabled) |
| 395 | + initial_subscription_name: Name of the initial subscription name of the dead letter topic. |
| 396 | + If this field is not set, the initial subscription for the dead letter topic is not created. |
| 397 | + If this field is set but the broker's `allowAutoSubscriptionCreation` is disabled, the DLQ producer |
| 398 | + fails to be created. |
| 399 | + """ |
| 400 | + builder = DeadLetterPolicyBuilder() |
| 401 | + if dead_letter_topic is not None: |
| 402 | + builder.deadLetterTopic(dead_letter_topic) |
| 403 | + if max_redeliver_count is not None: |
| 404 | + builder.maxRedeliverCount(max_redeliver_count) |
| 405 | + if initial_subscription_name is not None: |
| 406 | + builder.initialSubscriptionName(initial_subscription_name) |
| 407 | + self._policy = builder.build() |
| 408 | + |
| 409 | + @property |
| 410 | + def dead_letter_topic(self) -> str: |
| 411 | + """ |
| 412 | + Return the dead letter topic for dead letter policy. |
| 413 | + """ |
| 414 | + return self._policy.getDeadLetterTopic() |
| 415 | + |
| 416 | + @property |
| 417 | + def max_redeliver_count(self) -> int: |
| 418 | + """ |
| 419 | + Return the max redeliver count for dead letter policy. |
| 420 | + """ |
| 421 | + return self._policy.getMaxRedeliverCount() |
| 422 | + |
| 423 | + @property |
| 424 | + def initial_subscription_name(self) -> str: |
| 425 | + """ |
| 426 | + Return the initial subscription name for dead letter policy. |
| 427 | + """ |
| 428 | + return self._policy.getInitialSubscriptionName() |
| 429 | + |
| 430 | + def policy(self): |
| 431 | + """ |
| 432 | + Returns the actual one DeadLetterPolicy. |
| 433 | + """ |
| 434 | + return self._policy |
| 435 | + |
377 | 436 | class Client: |
378 | 437 | """ |
379 | 438 | The Pulsar client. A single client instance can be used to create producers |
@@ -708,6 +767,7 @@ def subscribe(self, topic, subscription_name, |
708 | 767 | key_shared_policy=None, |
709 | 768 | batch_index_ack_enabled=False, |
710 | 769 | regex_subscription_mode=RegexSubscriptionMode.PersistentOnly, |
| 770 | + dead_letter_policy: ConsumerDeadLetterPolicy = None, |
711 | 771 | ): |
712 | 772 | """ |
713 | 773 | Subscribe to the given topic and subscription combination. |
@@ -805,6 +865,12 @@ def my_listener(consumer, message): |
805 | 865 | * PersistentOnly: By default only subscribe to persistent topics. |
806 | 866 | * NonPersistentOnly: Only subscribe to non-persistent topics. |
807 | 867 | * AllTopics: Subscribe to both persistent and non-persistent topics. |
| 868 | + dead_letter_policy: class ConsumerDeadLetterPolicy |
| 869 | + Set dead letter policy for consumer. |
| 870 | + By default, some messages are redelivered many times, even to the extent that they can never be |
| 871 | + stopped. By using the dead letter mechanism, messages have the max redelivery count, when they're |
| 872 | + exceeding the maximum number of redeliveries. Messages are sent to dead letter topics and acknowledged |
| 873 | + automatically. |
808 | 874 | """ |
809 | 875 | _check_type(str, subscription_name, 'subscription_name') |
810 | 876 | _check_type(ConsumerType, consumer_type, 'consumer_type') |
@@ -864,6 +930,8 @@ def my_listener(consumer, message): |
864 | 930 | if key_shared_policy: |
865 | 931 | conf.key_shared_policy(key_shared_policy.policy()) |
866 | 932 | conf.batch_index_ack_enabled(batch_index_ack_enabled) |
| 933 | + if dead_letter_policy: |
| 934 | + conf.dead_letter_policy(dead_letter_policy.policy()) |
867 | 935 |
|
868 | 936 | c = Consumer() |
869 | 937 | if isinstance(topic, str): |
|
0 commit comments