public interface TypedMessageBuilder<T> extends Serializable
producer.newMessage().key(myKey).value(myValue).send();
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
CONF_DELIVERY_AFTER_SECONDS |
static String |
CONF_DELIVERY_AT |
static String |
CONF_DISABLE_REPLICATION |
static String |
CONF_EVENT_TIME |
static String |
CONF_KEY |
static String |
CONF_PROPERTIES |
static String |
CONF_REPLICATION_CLUSTERS |
static String |
CONF_SEQUENCE_ID |
| 限定符和类型 | 方法和说明 |
|---|---|
TypedMessageBuilder<T> |
deliverAfter(long delay,
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(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(Map<String,Object> config)
Configure the
TypedMessageBuilder from 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 in
SubscriptionType.Key_Shared mode. |
TypedMessageBuilder<T> |
properties(Map<String,String> properties)
Add all the properties in the provided map
|
TypedMessageBuilder<T> |
property(String name,
String value)
Sets a new property on a message.
|
TypedMessageBuilder<T> |
replicationClusters(List<String> clusters)
Override the geo-replication clusters for this message.
|
MessageId |
send()
Send a message synchronously.
|
CompletableFuture<MessageId> |
sendAsync()
Send a message asynchronously
This method returns a future that can be used to track the completion of the send operation and yields the
MessageId assigned by the broker to the published message. |
TypedMessageBuilder<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
|
MessageId send() throws PulsarClientException
This method will block until the message is successfully published and returns the
MessageId assigned by the broker to the published message.
Example:
MessageId msgId = producer.newMessage()
.key(myKey)
.value(myValue)
.send();
System.out.println("Published message: " + msgId);
MessageId assigned by the broker to the published message.PulsarClientExceptionCompletableFuture<MessageId> sendAsync()
This method returns a future that can be used to track the completion of the send operation and yields the
MessageId assigned 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.ProducerQueueIsFullError
See ProducerBuilder.maxPendingMessages(int) to configure the producer queue size and
ProducerBuilder.blockIfQueueFull(boolean) to change the blocking behavior.
message - a messageTypedMessageBuilder<T> key(String key)
key - the partitioning key for the messageTypedMessageBuilder<T> keyBytes(byte[] key)
key - routing key for message, in byte array formTypedMessageBuilder<T> orderingKey(byte[] orderingKey)
SubscriptionType.Key_Shared mode.
Partition key Will be used if ordering key not specifiedorderingKey - the ordering key for the messageTypedMessageBuilder<T> value(T value)
value - the domain objectTypedMessageBuilder<T> property(String name, String value)
name - the name of the propertyvalue - the associated valueTypedMessageBuilder<T> properties(Map<String,String> properties)
TypedMessageBuilder<T> eventTime(long timestamp)
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.
TypedMessageBuilder<T> sequenceId(long sequenceId)
The sequence id can be used for deduplication purposes and it needs to follow these rules:
sequenceId >= 0
sequenceId(N+1) > sequenceId(N)
sequenceId could represent an offset or a cumulative size.
sequenceId - the sequence id to assign to the current messageTypedMessageBuilder<T> replicationClusters(List<String> clusters)
clusters - TypedMessageBuilder<T> disableReplication()
TypedMessageBuilder<T> deliverAt(long 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.Shared subscription. With other subscription
types, the messages will still be delivered immediately.
timestamp - absolute timestamp indicating when the message should be delivered to consumersTypedMessageBuilder<T> deliverAfter(long delay, TimeUnit unit)
Note: messages are only delivered with delay when a consumer is consuming
through a SubscriptionType.Shared subscription. 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 delayTypedMessageBuilder<T> loadConf(Map<String,Object> config)
TypedMessageBuilder from 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_KEY | key | String | key(String) |
CONF_PROPERTIES | properties | Map<String,String> | properties(Map) |
CONF_EVENT_TIME | eventTime | long | eventTime(long) |
CONF_SEQUENCE_ID | sequenceId | long | sequenceId(long) |
CONF_REPLICATION_CLUSTERS | replicationClusters | List<String> | replicationClusters(List) |
CONF_DISABLE_REPLICATION | disableReplication | boolean | disableReplication() |
CONF_DELIVERY_AFTER_SECONDS | deliverAfterSeconds | long | deliverAfter(long, TimeUnit) |
CONF_DELIVERY_AT | deliverAt | long | deliverAt(long) |
config - a map with the configuration options for the messageCopyright © 2017–2019 Apache Software Foundation. All rights reserved.