|
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,65 @@ 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, |
| 383 | + max_redeliver_count: int, |
| 384 | + dead_letter_topic: str = None, |
| 385 | + initial_subscription_name: str = None): |
| 386 | + """ |
| 387 | + Wrapper DeadLetterPolicy. |
| 388 | +
|
| 389 | + Parameters |
| 390 | + ---------- |
| 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 | + dead_letter_topic: Name of the dead topic where the failing messages are sent. |
| 394 | + The default value is: sourceTopicName + "-" + subscriptionName + "-DLQ" |
| 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 max_redeliver_count is None or max_redeliver_count < 1: |
| 402 | + raise ValueError("max_redeliver_count must be greater than 0") |
| 403 | + builder.maxRedeliverCount(max_redeliver_count) |
| 404 | + if dead_letter_topic is not None: |
| 405 | + builder.deadLetterTopic(dead_letter_topic) |
| 406 | + if initial_subscription_name is not None: |
| 407 | + builder.initialSubscriptionName(initial_subscription_name) |
| 408 | + self._policy = builder.build() |
| 409 | + |
| 410 | + @property |
| 411 | + def dead_letter_topic(self) -> str: |
| 412 | + """ |
| 413 | + Return the dead letter topic for dead letter policy. |
| 414 | + """ |
| 415 | + return self._policy.getDeadLetterTopic() |
| 416 | + |
| 417 | + @property |
| 418 | + def max_redeliver_count(self) -> int: |
| 419 | + """ |
| 420 | + Return the max redeliver count for dead letter policy. |
| 421 | + """ |
| 422 | + return self._policy.getMaxRedeliverCount() |
| 423 | + |
| 424 | + @property |
| 425 | + def initial_subscription_name(self) -> str: |
| 426 | + """ |
| 427 | + Return the initial subscription name for dead letter policy. |
| 428 | + """ |
| 429 | + return self._policy.getInitialSubscriptionName() |
| 430 | + |
| 431 | + def policy(self): |
| 432 | + """ |
| 433 | + Returns the actual one DeadLetterPolicy. |
| 434 | + """ |
| 435 | + return self._policy |
| 436 | + |
377 | 437 | class Client:
|
378 | 438 | """
|
379 | 439 | The Pulsar client. A single client instance can be used to create producers
|
@@ -708,6 +768,7 @@ def subscribe(self, topic, subscription_name,
|
708 | 768 | key_shared_policy=None,
|
709 | 769 | batch_index_ack_enabled=False,
|
710 | 770 | regex_subscription_mode=RegexSubscriptionMode.PersistentOnly,
|
| 771 | + dead_letter_policy: ConsumerDeadLetterPolicy = None, |
711 | 772 | ):
|
712 | 773 | """
|
713 | 774 | Subscribe to the given topic and subscription combination.
|
@@ -805,6 +866,12 @@ def my_listener(consumer, message):
|
805 | 866 | * PersistentOnly: By default only subscribe to persistent topics.
|
806 | 867 | * NonPersistentOnly: Only subscribe to non-persistent topics.
|
807 | 868 | * AllTopics: Subscribe to both persistent and non-persistent topics.
|
| 869 | + dead_letter_policy: class ConsumerDeadLetterPolicy |
| 870 | + Set dead letter policy for consumer. |
| 871 | + By default, some messages are redelivered many times, even to the extent that they can never be |
| 872 | + stopped. By using the dead letter mechanism, messages have the max redelivery count, when they're |
| 873 | + exceeding the maximum number of redeliveries. Messages are sent to dead letter topics and acknowledged |
| 874 | + automatically. |
808 | 875 | """
|
809 | 876 | _check_type(str, subscription_name, 'subscription_name')
|
810 | 877 | _check_type(ConsumerType, consumer_type, 'consumer_type')
|
@@ -864,6 +931,8 @@ def my_listener(consumer, message):
|
864 | 931 | if key_shared_policy:
|
865 | 932 | conf.key_shared_policy(key_shared_policy.policy())
|
866 | 933 | conf.batch_index_ack_enabled(batch_index_ack_enabled)
|
| 934 | + if dead_letter_policy: |
| 935 | + conf.dead_letter_policy(dead_letter_policy.policy()) |
867 | 936 |
|
868 | 937 | c = Consumer()
|
869 | 938 | if isinstance(topic, str):
|
|
0 commit comments