Skip to content

Commit d77ad02

Browse files
author
Ómar Kjartan Yasin
committed
[improve][client] Add newMessage with schema and transactions
Pulsar Client allows callers to create messages with a schema or a transaction, but not both. This commit adds a new method in the producer that allows callers to create a message with both a schema and transaction.
1 parent cdab2d6 commit d77ad02

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

pulsar-client-api/src/main/java/org/apache/pulsar/client/api/Producer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,21 @@ public interface Producer<T> extends Closeable {
132132
* @since 2.7.0
133133
*/
134134
TypedMessageBuilder<T> newMessage(Transaction txn);
135+
136+
/**
137+
* Create a new message builder with transaction and schema, not required same parameterized type with the
138+
* producer.
139+
*
140+
* <p>After the transaction commit, it will be made visible to consumer.
141+
*
142+
* <p>After the transaction abort, it will never be visible to consumer.
143+
*
144+
* @return a typed message builder that can be used to construct the message to be sent through this producer
145+
* @see #newMessage()
146+
*/
147+
<V> TypedMessageBuilder<V> newMessage(Schema<V> schema,
148+
Transaction txn);
149+
135150
/**
136151
* Get the last sequence id that was published by this producer.
137152
*

pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerBase.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public TypedMessageBuilder<T> newMessage() {
7979
return new TypedMessageBuilderImpl<>(this, schema);
8080
}
8181

82+
@Override
8283
public <V> TypedMessageBuilder<V> newMessage(Schema<V> schema) {
8384
checkArgument(schema != null);
8485
return new TypedMessageBuilderImpl<>(this, schema);
@@ -92,6 +93,14 @@ public TypedMessageBuilder<T> newMessage(Transaction txn) {
9293
return new TypedMessageBuilderImpl<>(this, schema, (TransactionImpl) txn);
9394
}
9495

96+
@Override
97+
public <V> TypedMessageBuilder<V> newMessage(Schema<V> schema,
98+
Transaction txn) {
99+
checkArgument(txn instanceof TransactionImpl);
100+
checkArgument(schema != null);
101+
return new TypedMessageBuilderImpl<>(this, schema, (TransactionImpl) txn);
102+
}
103+
95104
abstract CompletableFuture<MessageId> internalSendAsync(Message<?> message);
96105

97106
abstract CompletableFuture<MessageId> internalSendWithTxnAsync(Message<?> message, Transaction txn);

0 commit comments

Comments
 (0)