class Producer: (source)
The Pulsar message producer, used to publish messages on a topic.
Examples
import pulsar client = pulsar.Client('pulsar://localhost:6650') producer = client.create_producer('my-topic') for i in range(10): producer.send(('Hello-%d' % i).encode('utf-8')) client.close()
Method | close |
Close the producer. |
Method | flush |
Flush all the messages buffered in the client and wait until all messages have been successfully persisted |
Method | is |
Check if the producer is connected or not. |
Method | last |
Get the last sequence id that was published by this producer. |
Method | producer |
Return the producer name which could have been assigned by the system or specified by the client |
Method | send |
Publish a message on the topic. Blocks until the message is acknowledged |
Method | send |
Send a message asynchronously. |
Method | topic |
Return the topic which producer is publishing to |
Method | _build |
Undocumented |
Flush all the messages buffered in the client and wait until all messages have been successfully persisted
Get the last sequence id that was published by this producer.
This represents either the automatically assigned or custom sequence id (set on the MessageBuilder) that was published and acknowledged by the broker.
After recreating a producer with the same producer name, this will return the last message that was published in the previous producer session, or -1 if there was no message ever published.
Publish a message on the topic. Blocks until the message is acknowledged
Returns a MessageId
object that represents where the message is persisted.
Parameters | |
content | A bytes object with the message payload. |
properties:optional | A dict of application-defined string properties. |
partition | Sets the partition key for message routing. A hash of this key is used to determine the message's topic partition. |
ordering | Sets the ordering key for message routing. |
sequence | Specify a custom sequence id for the message being published. |
replication | Override namespace replication clusters. Note that it is the caller's responsibility to provide valid cluster names and that all clusters have been previously configured as topics. Given an empty list, the message will replicate according to the namespace configuration. |
disablebool , default False | Do not replicate this message. |
event | Timestamp in millis of the timestamp of event creation |
deliver | Specify the message should not be delivered earlier than the specified timestamp. The timestamp is milliseconds and based on UTC |
deliver | Specify a delay in timedelta for the delivery of the messages. |
Send a message asynchronously.
Examples
The callback will be invoked once the message has been acknowledged by the broker. Users are responsible to handle the exception inside the callback. If any exception was thrown from the callback, the process would terminate immediately.
import pulsar client = pulsar.Client('pulsar://localhost:6650') producer = client.create_producer( 'my-topic', block_if_queue_full=True, batching_enabled=True, batching_max_publish_delay_ms=10) def callback(res, msg_id): print('Message published res=%s', res) while True: producer.send_async(('Hello-%d' % i).encode('utf-8'), callback) client.close()
When the producer queue is full, by default the message will be rejected and the callback invoked with an error code.
Parameters | |
content | A bytes object with the message payload. |
callback | A callback that is invoked once the message has been acknowledged by the broker. |
properties:optional | A dict of application0-defined string properties. |
partition | Sets the partition key for the message routing. A hash of this key is used to determine the message's topic partition. |
ordering | Sets the ordering key for the message routing. |
sequence | Specify a custom sequence id for the message being published. |
replication | Override namespace replication clusters. Note that it is the caller's responsibility to provide valid cluster names and that all clusters have been previously configured as topics. Given an empty list, the message will replicate per the namespace configuration. |
disable | Do not replicate this message. |
event | Timestamp in millis of the timestamp of event creation |
deliver | Specify the message should not be delivered earlier than the specified timestamp. |
deliver | Specify a delay in timedelta for the delivery of the messages. |