Beispiel #1
0
    # permanently failed delivery (after retries).
    def acked(err, msg):
        """Delivery report handler called on
        successful or failed delivery of message
        """
        if err is not None:
            print("Failed to deliver message: {}".format(err))
        else:
            print("Produced record to topic {} partition [{}] @ offset {}".
                  format(msg.topic(), msg.partition(), msg.offset()))

    for n in range(10):
        name_object = ccloud_lib.Name()
        name_object.name = "alice"
        record_key = name_object.to_dict()
        count_object = ccloud_lib.Count()
        count_object.count = n
        record_value = count_object.to_dict()
        print("Producing Avro record: {}\t{}".format(name_object.name,
                                                     count_object.count))
        p.produce(topic=topic,
                  key=record_key,
                  value=record_value,
                  on_delivery=acked)
        # p.poll() serves delivery reports (on_delivery)
        # from previous produce() calls.
        p.poll(0)

    p.flush(10)

    print("10 messages were produced to topic {}!".format(topic))
         msg = c.poll(1.0)
         if msg is None:
             # No message available within timeout.
             # Initial message consumption may take up to
             # `session.timeout.ms` for the consumer group to
             # rebalance and start consuming
             print("Waiting for message or event/error in poll()")
             continue
         elif msg.error():
             print('error: {}'.format(msg.error()))
         else:
             # Check for Kafka message
             record_key = ccloud_lib.Name(msg.key())
             name_object = record_key.name
             name = name_object['name']
             record_value = ccloud_lib.Count(msg.value())
             count_object = record_value.count
             count = count_object['count']
             total_count += count
             print("Consumed record with key {} and value {}, \
                   and updated total count to {}".format(
                 name, count, total_count))
 except SerializerError as e:
     # Report malformed record, discard results, continue polling
     print("Message deserialization failed {}".format(e))
     pass
 except KeyboardInterrupt:
     pass
 finally:
     # Leave group and commit final offsets
     c.close()