Skip to content

Commit c257758

Browse files
committed
Add error_cb to confluent_cloud.py example
1 parent 59be749 commit c257758

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

examples/confluent_cloud.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,42 +48,76 @@
4848

4949
import uuid
5050

51-
from confluent_kafka import Producer, Consumer
51+
from confluent_kafka import Producer, Consumer, KafkaError, KafkaException
5252

5353

54+
def error_cb(err):
55+
""" The error callback is used for generic client errors. These
56+
errors are generally to be considered informational as the client will
57+
automatically try to recover from all errors, and no extra action
58+
is typically required by the application.
59+
For this example however, we terminate the application if the client
60+
is unable to connect to any broker (_ALL_BROKERS_DOWN) and on
61+
authentication errors (_AUTHENTICATION). """
62+
63+
print("Client error: {}".format(err))
64+
if err.code() == KafkaError._ALL_BROKERS_DOWN or \
65+
err.code() == KafkaError._AUTHENTICATION:
66+
# Any exception raised from this callback will be re-raised from the
67+
# triggering flush() or poll() call.
68+
raise KafkaException(err)
69+
70+
71+
# Create producer
5472
p = Producer({
5573
'bootstrap.servers': '<ccloud bootstrap servers>',
5674
'sasl.mechanism': 'PLAIN',
5775
'security.protocol': 'SASL_SSL',
5876
'sasl.username': '<ccloud key>',
59-
'sasl.password': '<ccloud secret>'
77+
'sasl.password': '<ccloud secret>',
78+
'error_cb': error_cb,
6079
})
6180

6281

6382
def acked(err, msg):
6483
"""Delivery report callback called (from flush()) on successful or failed delivery of the message."""
6584
if err is not None:
66-
print("failed to deliver message: {}".format(err.str()))
85+
print('Failed to deliver message: {}'.format(err.str()))
6786
else:
68-
print("produced to: {} [{}] @ {}".format(msg.topic(), msg.partition(), msg.offset()))
87+
print('Produced to: {} [{}] @ {}'.format(msg.topic(), msg.partition(), msg.offset()))
88+
6989

90+
for n in range(0, 10):
91+
# Produce message: this is an asynchronous operation.
92+
# Upon successful or permanently failed delivery to the broker the
93+
# callback will be called to propagate the produce result.
94+
# The delivery callback is triggered from poll() or flush().
95+
# For long running
96+
# produce loops it is recommended to call poll() to serve these
97+
# delivery report callbacks.
98+
p.produce('python-test-topic', value='python test value nr {}'.format(n),
99+
callback=acked)
70100

71-
p.produce('python-test-topic', value='python test value', callback=acked)
101+
# Trigger delivery report callbacks from previous produce calls.
102+
p.poll(0)
72103

73104
# flush() is typically called when the producer is done sending messages to wait
74105
# for outstanding messages to be transmitted to the broker and delivery report
75106
# callbacks to get called. For continous producing you should call p.poll(0)
76107
# after each produce() call to trigger delivery report callbacks.
77108
p.flush(10)
78109

110+
111+
# Create consumer
79112
c = Consumer({
80113
'bootstrap.servers': '<ccloud bootstrap servers>',
81114
'sasl.mechanism': 'PLAIN',
82115
'security.protocol': 'SASL_SSL',
83116
'sasl.username': '<ccloud key>',
84117
'sasl.password': '<ccloud secret>',
85118
'group.id': str(uuid.uuid1()), # this will create a new consumer group on each invocation.
86-
'auto.offset.reset': 'earliest'
119+
'auto.offset.reset': 'earliest',
120+
'error_cb': error_cb,
87121
})
88122

89123
c.subscribe(['python-test-topic'])
@@ -98,10 +132,10 @@ def acked(err, msg):
98132
continue
99133
if msg.error():
100134
# Errors are typically temporary, print error and continue.
101-
print("Consumer error: {}".format(msg.error()))
135+
print('Consumer error: {}'.format(msg.error()))
102136
continue
103137

104-
print('consumed: {}'.format(msg.value()))
138+
print('Consumed: {}'.format(msg.value()))
105139

106140
except KeyboardInterrupt:
107141
pass

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy