-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for batch Acknowledgement
Based-on: #692 Co-authored-by: Jeremy Grelle <[email protected]>
- Loading branch information
1 parent
2ee65d1
commit 330d00e
Showing
13 changed files
with
310 additions
and
91 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
68 changes: 68 additions & 0 deletions
68
kafka/src/test/groovy/io/micronaut/configuration/kafka/offsets/BatchManualAckSpec.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,68 @@ | ||
package io.micronaut.configuration.kafka.offsets | ||
|
||
import io.micronaut.configuration.kafka.AbstractKafkaContainerSpec | ||
import io.micronaut.configuration.kafka.annotation.KafkaClient | ||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.messaging.Acknowledgement | ||
import io.micronaut.serde.annotation.Serdeable | ||
import jakarta.inject.Singleton | ||
|
||
import static io.micronaut.configuration.kafka.annotation.OffsetReset.EARLIEST | ||
import static io.micronaut.configuration.kafka.annotation.OffsetStrategy.DISABLED | ||
import static io.micronaut.configuration.kafka.config.AbstractKafkaConfiguration.EMBEDDED_TOPICS | ||
|
||
class BatchManualAckSpec extends AbstractKafkaContainerSpec { | ||
|
||
public static final String TOPIC_SYNC = "BatchManualAckSpec-products-sync" | ||
|
||
protected Map<String, Object> getConfiguration() { | ||
super.configuration + | ||
[(EMBEDDED_TOPICS): [TOPIC_SYNC]] | ||
} | ||
|
||
void "test manual ack"() { | ||
given: | ||
ProductClient client = context.getBean(ProductClient) | ||
ProductListener listener = context.getBean(ProductListener) | ||
|
||
when: | ||
client.send(new Product(name: "Apple")) | ||
client.send(new Product(name: "Orange")) | ||
|
||
then: | ||
conditions.eventually { | ||
listener.products.size() == 2 | ||
listener.products.find() { it.name == "Apple"} | ||
} | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'BatchManualAckSpec') | ||
@KafkaClient | ||
static interface ProductClient { | ||
@Topic(BatchManualAckSpec.TOPIC_SYNC) | ||
void send(Product product) | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'BatchManualAckSpec') | ||
@Singleton | ||
static class ProductListener { | ||
|
||
List<Product> products = [] | ||
|
||
@KafkaListener(offsetReset = EARLIEST, offsetStrategy = DISABLED, batch = true) | ||
@Topic(BatchManualAckSpec.TOPIC_SYNC) | ||
void receive(List<Product> products, Acknowledgement acknowledgement) { | ||
for (p in products) { | ||
this.products << p | ||
} | ||
acknowledgement.ack() | ||
} | ||
} | ||
|
||
@Serdeable | ||
static class Product { | ||
String name | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
...ite-groovy/src/test/groovy/io/micronaut/kafka/docs/consumer/batch/ack/BookListener.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,25 @@ | ||
package io.micronaut.kafka.docs.consumer.batch.ack | ||
|
||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.kafka.docs.consumer.batch.Book | ||
import io.micronaut.messaging.Acknowledgement | ||
|
||
import static io.micronaut.configuration.kafka.annotation.OffsetReset.EARLIEST | ||
import static io.micronaut.configuration.kafka.annotation.OffsetStrategy.DISABLED | ||
|
||
@Requires(property = 'spec.name', value = 'BatchManualAckSpec') | ||
class BookListener { | ||
|
||
// tag::method[] | ||
@KafkaListener(offsetReset = EARLIEST, offsetStrategy = DISABLED, batch = true) // <1> | ||
@Topic("all-the-books") | ||
void receive(List<Book> books, Acknowledgement acknowledgement) { // <2> | ||
|
||
//process the books | ||
|
||
acknowledgement.ack() // <3> | ||
} | ||
// end::method[] | ||
} |
41 changes: 41 additions & 0 deletions
41
...-groovy/src/test/groovy/io/micronaut/kafka/docs/consumer/batch/manual/BookListener.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,41 @@ | ||
package io.micronaut.kafka.docs.consumer.batch.manual | ||
|
||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.kafka.docs.consumer.batch.Book | ||
import org.apache.kafka.clients.consumer.Consumer | ||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
import org.apache.kafka.clients.consumer.OffsetAndMetadata | ||
import org.apache.kafka.common.TopicPartition | ||
|
||
import static io.micronaut.configuration.kafka.annotation.OffsetReset.EARLIEST | ||
import static io.micronaut.configuration.kafka.annotation.OffsetStrategy.DISABLED | ||
|
||
@Requires(property = 'spec.name', value = 'BatchManualAckSpec') | ||
class BookListener { | ||
|
||
// tag::method[] | ||
@KafkaListener(offsetReset = EARLIEST, offsetStrategy = DISABLED, batch = true) // <1> | ||
@Topic("all-the-books") | ||
void receive(List<ConsumerRecord<String, Book>> records, Consumer kafkaConsumer) { // <2> | ||
|
||
for (int i = 0; i < records.size(); i++) { | ||
ConsumerRecord<String, Book> record = records.get(i) // <3> | ||
|
||
// process the book | ||
Book book = record.value() | ||
|
||
// commit offsets | ||
String topic = record.topic() | ||
int partition = record.partition() | ||
long offset = record.offset() // <4> | ||
|
||
kafkaConsumer.commitSync(Collections.singletonMap( // <5> | ||
new TopicPartition(topic, partition), | ||
new OffsetAndMetadata(offset + 1, "my metadata") | ||
)) | ||
} | ||
} | ||
// end::method[] | ||
} |
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
24 changes: 24 additions & 0 deletions
24
test-suite-kotlin/src/test/kotlin/io/micronaut/kafka/docs/consumer/batch/ack/BookListener.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,24 @@ | ||
package io.micronaut.kafka.docs.consumer.batch.ack | ||
|
||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.OffsetReset | ||
import io.micronaut.configuration.kafka.annotation.OffsetStrategy | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.kafka.docs.consumer.batch.Book | ||
import io.micronaut.messaging.Acknowledgement | ||
|
||
@Requires(property = "spec.name", value = "BatchManualAckSpec") | ||
internal class BookListener { | ||
|
||
// tag::method[] | ||
@KafkaListener(offsetReset = OffsetReset.EARLIEST, offsetStrategy = OffsetStrategy.DISABLED, batch = true) // <1> | ||
@Topic("all-the-books") | ||
fun receive(books: List<Book?>?, acknowledgement: Acknowledgement) { // <2> | ||
|
||
//process the books | ||
|
||
acknowledgement.ack() // <3> | ||
} | ||
// end::method[] | ||
} |
45 changes: 45 additions & 0 deletions
45
...uite-kotlin/src/test/kotlin/io/micronaut/kafka/docs/consumer/batch/manual/BookListener.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,45 @@ | ||
package io.micronaut.kafka.docs.consumer.batch.manual | ||
|
||
// tag::imports[] | ||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.OffsetReset | ||
import io.micronaut.configuration.kafka.annotation.OffsetStrategy | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.kafka.docs.consumer.batch.Book | ||
import org.apache.kafka.clients.consumer.Consumer | ||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
import org.apache.kafka.clients.consumer.OffsetAndMetadata | ||
import org.apache.kafka.common.TopicPartition | ||
|
||
import java.util.* | ||
// end::imports[] | ||
|
||
@Requires(property = "spec.name", value = "BatchManualAckSpec") | ||
internal class BookListener { | ||
|
||
// tag::method[] | ||
@KafkaListener(offsetReset = OffsetReset.EARLIEST, offsetStrategy = OffsetStrategy.DISABLED, batch = true) // <1> | ||
@Topic("all-the-books") | ||
fun receive(records: List<ConsumerRecord<String?, Book?>>, kafkaConsumer: Consumer<*, *>) { // <2> | ||
for (i in records.indices) { | ||
val record = records[i] // <3> | ||
|
||
// process the book | ||
val book = record.value() | ||
|
||
// commit offsets | ||
val topic = record.topic() | ||
val partition = record.partition() | ||
val offset = record.offset() // <4> | ||
kafkaConsumer.commitSync( | ||
Collections.singletonMap( // <5> | ||
TopicPartition(topic, partition), | ||
OffsetAndMetadata(offset + 1, "my metadata") | ||
) | ||
) | ||
} | ||
} | ||
// end::method[] | ||
} | ||
|
Oops, something went wrong.