generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc clean-up, reader example, bump pulsar dependency (#116)
- Loading branch information
Haris Secic
authored
Oct 10, 2021
1 parent
7811755
commit c35d5e8
Showing
16 changed files
with
88 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
doc-examples/example-groovy/src/test/groovy/example/ReaderExample.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package example; | ||
|
||
import io.micronaut.pulsar.annotation.PulsarReader; | ||
import jakarta.inject.Singleton; | ||
import org.apache.pulsar.client.api.Message; | ||
import org.apache.pulsar.client.api.Reader; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
|
||
@Singleton | ||
class ReaderExample { | ||
|
||
@PulsarReader(value = "persistent://public/default/messages", readerName = "simple-g-reader") // <1> | ||
private Reader<String> reader // <2> | ||
|
||
CompletableFuture<Message<String>> readNext() { // <3> | ||
return reader.readNextAsync() // <4> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
doc-examples/example-java/src/test/java/example/ReaderExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package example; | ||
|
||
import io.micronaut.pulsar.annotation.PulsarReader; | ||
import jakarta.inject.Singleton; | ||
import org.apache.pulsar.client.api.Message; | ||
import org.apache.pulsar.client.api.Reader; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
|
||
@Singleton | ||
public class ReaderExample { | ||
|
||
@PulsarReader(value = "persistent://public/default/messages", readerName = "simple-j-reader") // <1> | ||
private Reader<String> reader; // <2> | ||
|
||
public CompletableFuture<Message<String>> readNext() { // <3> | ||
return reader.readNextAsync(); // <4> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
doc-examples/example-kotlin/src/test/kotlin/example/ReaderExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package example | ||
|
||
import io.micronaut.pulsar.annotation.PulsarReader | ||
import jakarta.inject.Singleton | ||
import kotlinx.coroutines.future.await | ||
import org.apache.pulsar.client.api.Message | ||
import org.apache.pulsar.client.api.Reader | ||
|
||
@Singleton | ||
class ReaderExample { | ||
@PulsarReader(value = "persistent://public/default/messages", readerName = "simple-k-reader") // <1> | ||
private lateinit var reader: Reader<String> // <2> | ||
|
||
suspend fun readNext(): Message<String> { // <3> | ||
return reader.readNextAsync().await() // <4> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
skipDocumentation=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,16 @@ | ||
== Readers vs Consumers | ||
== Readers | ||
|
||
Readers can be used to gain more control over message flow. However, they are limited to a single topic in contrast to consumers. On the other hand, you request each message "manually" by explicitly calling next message read. A more useful feature in the case of Readers might be `seek`, where developers get the ability to position the reader where they want in the message log, thus being able to manually replay messages when required. Simply put, it's more similar to reading a file than reacting to an event, but the file gets modified by an external actor. | ||
Pulsar supports both "Consumers" and "Readers". More can be read in their | ||
https://pulsar.apache.org/docs/en/concepts-clients/#reader-interface[documentation] | ||
|
||
== Creating readers | ||
|
||
To initialize a reader, declare a field annotated with `@PulsarReader` inside any bean or as a constructor argument. | ||
|
||
[source,java] | ||
---- | ||
import io.micronaut.pulsar.annotation.PulsarProducerClient; | ||
import io.micronaut.pulsar.annotation.PulsarProducer; | ||
snippet::example.ReaderExample[project-base="doc-examples/example", indent="0"] | ||
<1> Reader annotation with the topic and the reader name | ||
<2> Reader must be of type api:org.apache.pulsar.client.api.Reader | ||
<3> Using readAsync requires `CompletableFeature` or in Kotlin awaiting is possible | ||
<4> Calling the read will move the cursor to the next message or give null in case there are no more messages | ||
|
||
@Singleton | ||
public class MyReader { | ||
@PulsarReader(...) | ||
private Reader reader; | ||
} | ||
---- | ||
|
||
Only *topic* is a required parameter. However, it's important to note that if the producer name is not set, it will default to the method name which may cause collisions in case of non-unique method names. | ||
|
||
=== Producer return values | ||
|
||
If you need the `MessageId` from Pulsar, you can | ||
specify *MessageId* as the return type instead of *void* as given in examples below. If reactive or async return types are used, | ||
the method can only return `MessageId` as their return type like `Maybe<MessageId>`. If you have blocking methods, | ||
`MessageId` or the type of the parameter passed to function can be used as a return type of the method. | ||
|
||
Examples: | ||
[source,java] | ||
---- | ||
import io.micronaut.pulsar.annotation.PulsarProducerClient; | ||
import io.micronaut.pulsar.annotation.PulsarProducer; | ||
@PulsarProducerClient | ||
public interface ProducerTester { | ||
@PulsarProducer(topic = "public/default/test", producerName = "test-producer-1") | ||
String returnParameter(String message); | ||
@PulsarProducer(topic = "public/default/test", producerName = "test-producer-2") | ||
MessageId returnMessageId(String message); | ||
@PulsarProducer(topic = "public/default/test", producerName = "test-producer-3") | ||
Single<MessageId> returnReactive(String message); | ||
} | ||
---- | ||
Reader `name` can be autogenerated but `topic` argument must be set. |