|
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
|
@@ -692,7 +750,8 @@ def subscribe(self, topic, subscription_name,
|
692 | 750 | auto_ack_oldest_chunked_message_on_queue_full=False,
|
693 | 751 | start_message_id_inclusive=False,
|
694 | 752 | batch_receive_policy=None,
|
695 |
| - key_shared_policy=None |
| 753 | + key_shared_policy=None, |
| 754 | + dead_letter_policy: ConsumerDeadLetterPolicy = None, |
696 | 755 | ):
|
697 | 756 | """
|
698 | 757 | Subscribe to the given topic and subscription combination.
|
@@ -778,7 +837,13 @@ def my_listener(consumer, message):
|
778 | 837 | batch_receive_policy: class ConsumerBatchReceivePolicy
|
779 | 838 | Set the batch collection policy for batch receiving.
|
780 | 839 | key_shared_policy: class ConsumerKeySharedPolicy
|
781 |
| - Set the key shared policy for use when the ConsumerType is KeyShared. |
| 840 | + Set the key shared policy for use when the ConsumerType is KeyShared. |
| 841 | + dead_letter_policy: class ConsumerDeadLetterPolicy |
| 842 | + Set dead letter policy for consumer. |
| 843 | + By default, some messages are redelivered many times, even to the extent that they can never be |
| 844 | + stopped. By using the dead letter mechanism, messages have the max redelivery count, when they're |
| 845 | + exceeding the maximum number of redeliveries. Messages are sent to dead letter topics and acknowledged |
| 846 | + automatically. |
782 | 847 | """
|
783 | 848 | _check_type(str, subscription_name, 'subscription_name')
|
784 | 849 | _check_type(ConsumerType, consumer_type, 'consumer_type')
|
@@ -834,6 +899,8 @@ def my_listener(consumer, message):
|
834 | 899 |
|
835 | 900 | if key_shared_policy:
|
836 | 901 | conf.key_shared_policy(key_shared_policy.policy())
|
| 902 | + if dead_letter_policy: |
| 903 | + conf.dead_letter_policy(dead_letter_policy.policy()) |
837 | 904 |
|
838 | 905 | c = Consumer()
|
839 | 906 | if isinstance(topic, str):
|
@@ -1457,6 +1524,8 @@ def policy(self):
|
1457 | 1524 | """
|
1458 | 1525 | return self._policy
|
1459 | 1526 |
|
| 1527 | + |
| 1528 | + |
1460 | 1529 | class ConsumerKeySharedPolicy:
|
1461 | 1530 | """
|
1462 | 1531 | Consumer key shared policy is used to configure the consumer behaviour when the ConsumerType is KeyShared.
|
|
0 commit comments