接口 TypedMessageBuilder<T>
- 所有超级接口:
Serializable
Usage example:
producer.newMessage().key(myKey).value(myValue).send();
-
字段概要
字段 -
方法概要
修饰符和类型方法说明deliverAfter(long delay, TimeUnit unit) Request to deliver the message only after the specified relative delay.deliverAt(long timestamp) Deliver the message only at or after the specified absolute timestamp.Disable geo-replication for this message.eventTime(long timestamp) Set the event time for a given message.Sets the key of the message for routing policy.keyBytes(byte[] key) Sets the bytes of the key of the message for routing policy.Configure theTypedMessageBuilderfrom a config map, as an alternative compared to call the individual builder methods.orderingKey(byte[] orderingKey) Sets the ordering key of the message for message dispatch inSubscriptionType.Key_Sharedmode.properties(Map<String, String> properties) Add all the properties in the provided map.Sets a new property on a message.replicationClusters(List<String> clusters) Override the geo-replication clusters for this message.send()Send a message synchronously.Send a message asynchronouslysequenceId(long sequenceId) Specify a custom sequence id for the message being published.Set a domain object on the message.
-
字段详细资料
-
方法详细资料
-
send
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);- 返回:
- the
MessageIdassigned by the broker to the published message. - 抛出:
PulsarClientException
-
sendAsync
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.- 返回:
- a future that can be used to track when the message will have been safely persisted
-
key
Sets the key of the message for routing policy.- 参数:
key- the partitioning key for the message- 返回:
- the message builder instance
-
keyBytes
Sets the bytes of the key of the message for routing policy. Internally the bytes will be base64 encoded.- 参数:
key- routing key for message, in byte array form- 返回:
- the message builder instance
-
orderingKey
Sets the ordering key of the message for message dispatch inSubscriptionType.Key_Sharedmode. Partition key Will be used if ordering key not specified.- 参数:
orderingKey- the ordering key for the message- 返回:
- the message builder instance
-
value
Set a domain object on the message.- 参数:
value- the domain object- 返回:
- the message builder instance
-
property
Sets a new property on a message.- 参数:
name- the name of the propertyvalue- the associated value- 返回:
- the message builder instance
-
properties
Add all the properties in the provided map.- 返回:
- the message builder instance
-
eventTime
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.
- 返回:
- the message builder instance
-
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.
- 参数:
sequenceId- the sequence id to assign to the current message- 返回:
- the message builder instance
-
replicationClusters
Override the geo-replication clusters for this message.- 参数:
clusters- the list of clusters.- 返回:
- the message builder instance
-
disableReplication
TypedMessageBuilder<T> disableReplication()Disable geo-replication for this message.- 返回:
- the message builder instance
-
deliverAt
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.- 参数:
timestamp- absolute timestamp indicating when the message should be delivered to consumers- 返回:
- the message builder instance
-
deliverAfter
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.- 参数:
delay- the amount of delay before the message will be deliveredunit- the time unit for the delay- 返回:
- the message builder instance
-
loadConf
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)- 参数:
config- a map with the configuration options for the message- 返回:
- the message builder instance
-