producer-raw-recipies_1.py 437 B

123456789101112
  1. def publish_message(producer_instance, topic_name, key, value):
  2. try:
  3. key_bytes = bytes(key, encoding='utf-8')
  4. value_bytes = bytes(value, encoding='utf-8')
  5. producer_instance.send(topic_name, key=key_bytes, value=value_bytes)
  6. producer_instance.flush()
  7. print('Message published successfully.')
  8. except Exception as ex:
  9. print('Exception in publishing message')
  10. print(str(ex))