Skip to content

Commit 4e83ba9

Browse files
committed
Updated Hibernate ORM with Kafka documentation
Format numbered list Add space to list Simplify documentation
1 parent b4287d5 commit 4e83ba9

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

docs/src/main/asciidoc/kafka.adoc

+10-9
Original file line numberDiff line numberDiff line change
@@ -2956,9 +2956,7 @@ NOTE: If you use Hibernate Reactive, look at <<writing-entities-managed-by-hiber
29562956

29572957
Because we write to a database, we must run this method in a transaction.
29582958
Yet, sending the entity to Kafka happens asynchronously.
2959-
The operation returns a `CompletionStage` (or a `Uni` if you use a `MutinyEmitter`) reporting when the operation completes.
2960-
We must be sure that the transaction is still running until the object is written.
2961-
Otherwise, you may access the object outside the transaction, which is not allowed.
2959+
We can achieve this by using `.sendAndAwait()` or `.sendAndForget()` on the `MutinyEmitter`, or `.send().toCompletableFuture().join()` on the `Emitter`.
29622960

29632961
To implement this process, you need the following approach:
29642962

@@ -2973,26 +2971,29 @@ import jakarta.ws.rs.POST;
29732971
import jakarta.ws.rs.Path;
29742972
29752973
import org.eclipse.microprofile.reactive.messaging.Channel;
2976-
import org.eclipse.microprofile.reactive.messaging.Emitter;
2974+
import io.smallrye.reactive.messaging.MutinyEmitter;
29772975
29782976
@Path("/")
29792977
public class ResourceSendingToKafka {
29802978
2981-
@Channel("kafka") Emitter<Fruit> emitter;
2979+
@Channel("kafka") MutinyEmitter<Fruit> emitter;
29822980
29832981
@POST
29842982
@Path("/fruits")
2985-
@Transactional // <1>
2986-
public CompletionStage<Void> storeAndSendToKafka(Fruit fruit) { // <2>
2983+
@Transactional // <1>
2984+
public void storeAndSendToKafka(Fruit fruit) { // <2>
29872985
fruit.persist();
2988-
return emitter.send(new FruitDto(fruit)); // <3>
2986+
emitter.sendAndAwait(new FruitDto(fruit)); // <3>
29892987
}
29902988
}
29912989
----
29922990
<1> As we are writing to the database, make sure we run inside a transaction
2993-
<2> The method receives the fruit instance to persist. It returns a `CompletionStage` which is used for the transaction demarcation. The transaction is committed when the return `CompletionStage` completes. In our case, it's when the message is written to Kafka.
2991+
<2> The method receives the fruit instance to persist.
29942992
<3> Wrap the managed entity inside a Data transfer object and send it to Kafka.
29952993
This makes sure that managed entity is not impacted by the Kafka serialization.
2994+
Then await the completion of the operation before returning.
2995+
2996+
NOTE: You should not return a `CompletionStage` or `Uni` when using `@Transactional`, as all transaction commits will happen on a single thread, which impacts performance.
29962997

29972998
[[writing-entities-managed-by-hibernate-reactive-to-kafka]]
29982999
=== Writing entities managed by Hibernate Reactive to Kafka

0 commit comments

Comments
 (0)