-
Notifications
You must be signed in to change notification settings - Fork 64
Producer
Daniel Blankensteiner edited this page Feb 21, 2021
·
3 revisions
Need to send a message to Pulsar? Well, that's why we have a producer.
Start with creating a client.
When creating a producer we have these options:
- ProducerName - Set the producer name. This is optional.
- InitialSequenceId - Set the initial sequence id. The default is 0.
- Topic - Set the topic for this producer. This is required.
Only the topic is required.
var producer = client.NewProducer()
.Topic("persistent://public/default/mytopic")
.Create();
var options = new ProducerOptions("persistent://public/default/mytopic");
var producer = client.CreateProducer(options);
var data = Encoding.UTF8.GetBytes("Hello World");
await producer.Send(data);
We have quite a few options in regards to metadata:
- DeliverAt - Absolute timestamp indicating when the message should be delivered to consumers.
- EventTime - Set the event time of the message.
- Key - Set the key of the message for routing policy.
- KeyBytes - Set the key of the message for routing policy.
- OrderingKey - Set the ordering key of the message for message dispatch in SubscriptionType.KeyShared mode.
- SequenceId - Set the sequence id of the message.
Besides these, we can also specify custom metadata. Let's see how.
var data = Encoding.UTF8.GetBytes("Hello World");
var messageId = await producer.NewMessage()
.Property("SomeKey", "SomeValue")
.Send(data);
var data = Encoding.UTF8.GetBytes("Hello World");
var metadata = new MessageMetadata();
metadata["SomeKey"] = "SomeValue";
var messageId = await producer.Send(metadata, data));
Monitoring the state is recommended and described here.
The Producer implements IAsyncDisposable and should be disposed when it's no longer needed. Disposing the client will dispose all producers.