Interface TypedMessageBuilder<T>
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCONF_DELIVERY_AFTER_SECONDSstatic java.lang.StringCONF_DELIVERY_ATstatic java.lang.StringCONF_DISABLE_REPLICATIONstatic java.lang.StringCONF_EVENT_TIMEstatic java.lang.StringCONF_KEYstatic java.lang.StringCONF_PROPERTIESstatic java.lang.StringCONF_REPLICATION_CLUSTERSstatic java.lang.StringCONF_SEQUENCE_ID
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description TypedMessageBuilder<T>deliverAfter(long delay, java.util.concurrent.TimeUnit unit)Request to deliver the message only after the specified relative delay.TypedMessageBuilder<T>deliverAt(long timestamp)Deliver the message only at or after the specified absolute timestamp.TypedMessageBuilder<T>disableReplication()Disable geo-replication for this message.TypedMessageBuilder<T>eventTime(long timestamp)Set the event time for a given message.TypedMessageBuilder<T>key(java.lang.String key)Sets the key of the message for routing policy.TypedMessageBuilder<T>keyBytes(byte[] key)Sets the bytes of the key of the message for routing policy.TypedMessageBuilder<T>loadConf(java.util.Map<java.lang.String,java.lang.Object> config)Configure theTypedMessageBuilderfrom a config map, as an alternative compared to call the individual builder methods.TypedMessageBuilder<T>orderingKey(byte[] orderingKey)Sets the ordering key of the message for message dispatch inSubscriptionType.Key_Sharedmode.TypedMessageBuilder<T>properties(java.util.Map<java.lang.String,java.lang.String> properties)Add all the properties in the provided map.TypedMessageBuilder<T>property(java.lang.String name, java.lang.String value)Sets a new property on a message.TypedMessageBuilder<T>replicationClusters(java.util.List<java.lang.String> clusters)Override the geo-replication clusters for this message.MessageIdsend()Send a message synchronously.java.util.concurrent.CompletableFuture<MessageId>sendAsync()Send a message asynchronouslyTypedMessageBuilder<T>sequenceId(long sequenceId)Specify a custom sequence id for the message being published.TypedMessageBuilder<T>value(T value)Set a domain object on the message.
-
-
-
Field Detail
-
CONF_KEY
static final java.lang.String CONF_KEY
- See Also:
- Constant Field Values
-
CONF_PROPERTIES
static final java.lang.String CONF_PROPERTIES
- See Also:
- Constant Field Values
-
CONF_EVENT_TIME
static final java.lang.String CONF_EVENT_TIME
- See Also:
- Constant Field Values
-
CONF_SEQUENCE_ID
static final java.lang.String CONF_SEQUENCE_ID
- See Also:
- Constant Field Values
-
CONF_REPLICATION_CLUSTERS
static final java.lang.String CONF_REPLICATION_CLUSTERS
- See Also:
- Constant Field Values
-
CONF_DISABLE_REPLICATION
static final java.lang.String CONF_DISABLE_REPLICATION
- See Also:
- Constant Field Values
-
CONF_DELIVERY_AFTER_SECONDS
static final java.lang.String CONF_DELIVERY_AFTER_SECONDS
- See Also:
- Constant Field Values
-
CONF_DELIVERY_AT
static final java.lang.String CONF_DELIVERY_AT
- See Also:
- Constant Field Values
-
-
Method Detail
-
send
MessageId send() throws PulsarClientException
Send a message synchronously.This method will block until the message is successfully published and returns the
MessageIdassigned by the broker to the published message.Example:
MessageId msgId = producer.newMessage() .key(myKey) .value(myValue) .send(); System.out.println("Published message: " + msgId);- Returns:
- the
MessageIdassigned by the broker to the published message. - Throws:
PulsarClientException
-
sendAsync
java.util.concurrent.CompletableFuture<MessageId> sendAsync()
Send a message asynchronouslyThis method returns a future that can be used to track the completion of the send operation and yields the
MessageIdassigned by the broker to the published message.Example:
producer.newMessage() .value(myValue) .sendAsync().thenAccept(messageId -> { System.out.println("Published message: " + messageId); }).exceptionally(e -> { System.out.println("Failed to publish " + e); return null; });When the producer queue is full, by default this method will complete the future with an exception
PulsarClientException.ProducerQueueIsFullErrorSee
ProducerBuilder.maxPendingMessages(int)to configure the producer queue size andProducerBuilder.blockIfQueueFull(boolean)to change the blocking behavior.- Returns:
- a future that can be used to track when the message will have been safely persisted
-
key
TypedMessageBuilder<T> key(java.lang.String key)
Sets the key of the message for routing policy.- Parameters:
key- the partitioning key for the message- Returns:
- the message builder instance
-
keyBytes
TypedMessageBuilder<T> keyBytes(byte[] key)
Sets the bytes of the key of the message for routing policy. Internally the bytes will be base64 encoded.- Parameters:
key- routing key for message, in byte array form- Returns:
- the message builder instance
-
orderingKey
TypedMessageBuilder<T> orderingKey(byte[] orderingKey)
Sets the ordering key of the message for message dispatch inSubscriptionType.Key_Sharedmode. Partition key Will be used if ordering key not specified.- Parameters:
orderingKey- the ordering key for the message- Returns:
- the message builder instance
-
value
TypedMessageBuilder<T> value(T value)
Set a domain object on the message.- Parameters:
value- the domain object- Returns:
- the message builder instance
-
property
TypedMessageBuilder<T> property(java.lang.String name, java.lang.String value)
Sets a new property on a message.- Parameters:
name- the name of the propertyvalue- the associated value- Returns:
- the message builder instance
-
properties
TypedMessageBuilder<T> properties(java.util.Map<java.lang.String,java.lang.String> properties)
Add all the properties in the provided map.- Returns:
- the message builder instance
-
eventTime
TypedMessageBuilder<T> eventTime(long timestamp)
Set the event time for a given message.Applications can retrieve the event time by calling
Message.getEventTime().Note: currently pulsar doesn't support event-time based index. so the subscribers can't seek the messages by event time.
- Returns:
- the message builder instance
-
sequenceId
TypedMessageBuilder<T> sequenceId(long sequenceId)
Specify a custom sequence id for the message being published.The sequence id can be used for deduplication purposes and it needs to follow these rules:
sequenceId >= 0- Sequence id for a message needs to be greater than sequence id for earlier messages:
sequenceId(N+1) > sequenceId(N) - It's not necessary for sequence ids to be consecutive. There can be holes between messages. Eg. the
sequenceIdcould represent an offset or a cumulative size.
- Parameters:
sequenceId- the sequence id to assign to the current message- Returns:
- the message builder instance
-
replicationClusters
TypedMessageBuilder<T> replicationClusters(java.util.List<java.lang.String> clusters)
Override the geo-replication clusters for this message.- Parameters:
clusters- the list of clusters.- Returns:
- the message builder instance
-
disableReplication
TypedMessageBuilder<T> disableReplication()
Disable geo-replication for this message.- Returns:
- the message builder instance
-
deliverAt
TypedMessageBuilder<T> deliverAt(long timestamp)
Deliver the message only at or after the specified absolute timestamp.The timestamp is milliseconds and based on UTC (eg:
System.currentTimeMillis().Note: messages are only delivered with delay when a consumer is consuming through a
SubscriptionType.Sharedsubscription. With other subscription types, the messages will still be delivered immediately.- Parameters:
timestamp- absolute timestamp indicating when the message should be delivered to consumers- Returns:
- the message builder instance
-
deliverAfter
TypedMessageBuilder<T> deliverAfter(long delay, java.util.concurrent.TimeUnit unit)
Request to deliver the message only after the specified relative delay.Note: messages are only delivered with delay when a consumer is consuming through a
SubscriptionType.Sharedsubscription. With other subscription types, the messages will still be delivered immediately.- Parameters:
delay- the amount of delay before the message will be deliveredunit- the time unit for the delay- Returns:
- the message builder instance
-
loadConf
TypedMessageBuilder<T> loadConf(java.util.Map<java.lang.String,java.lang.Object> config)
Configure theTypedMessageBuilderfrom a config map, as an alternative compared to call the individual builder methods.The "value" of the message itself cannot be set on the config map.
Example:
Map<String, Object> conf = new HashMap<>(); conf.put("key", "my-key"); conf.put("eventTime", System.currentTimeMillis()); producer.newMessage() .value("my-message") .loadConf(conf) .send();The available options are:
Constant Name Type Doc CONF_KEYkeyStringkey(String)CONF_PROPERTIESpropertiesMap<String,String>properties(Map)CONF_EVENT_TIMEeventTimelongeventTime(long)CONF_SEQUENCE_IDsequenceIdlongsequenceId(long)CONF_REPLICATION_CLUSTERSreplicationClustersList<String>replicationClusters(List)CONF_DISABLE_REPLICATIONdisableReplicationbooleandisableReplication()CONF_DELIVERY_AFTER_SECONDSdeliverAfterSecondslongdeliverAfter(long, TimeUnit)CONF_DELIVERY_ATdeliverAtlongdeliverAt(long)- Parameters:
config- a map with the configuration options for the message- Returns:
- the message builder instance
-
-