-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: Multi-language documentation for Forwarding Messages with @sendto …
…example (#827)
- Loading branch information
Showing
28 changed files
with
655 additions
and
93 deletions.
There are no files selected for viewing
38 changes: 0 additions & 38 deletions
38
...rc/test/groovy/io/micronaut/configuration/kafka/docs/consumer/sendto/ProductListener.java
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
kafka/src/test/groovy/io/micronaut/configuration/kafka/docs/consumer/sendto/WordCounter.java
This file was deleted.
Oops, something went wrong.
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
15 changes: 15 additions & 0 deletions
15
...suite-groovy/src/test/groovy/io/micronaut/kafka/docs/consumer/sendto/ProductClient.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,15 @@ | ||
package io.micronaut.kafka.docs.consumer.sendto | ||
|
||
import io.micronaut.configuration.kafka.annotation.KafkaClient | ||
import io.micronaut.configuration.kafka.annotation.KafkaKey | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.kafka.docs.Product; | ||
|
||
@Requires(property = 'spec.name', value = 'SendToProductListenerTest') | ||
@KafkaClient('product-client') | ||
interface ProductClient { | ||
|
||
@Topic('sendto-products') | ||
void send(@KafkaKey String brand, Product product) | ||
} |
40 changes: 40 additions & 0 deletions
40
...ite-groovy/src/test/groovy/io/micronaut/kafka/docs/consumer/sendto/ProductListener.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,40 @@ | ||
package io.micronaut.kafka.docs.consumer.sendto | ||
|
||
import groovy.util.logging.Slf4j | ||
|
||
// tag::imports[] | ||
import io.micronaut.configuration.kafka.annotation.KafkaKey | ||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.OffsetReset | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.kafka.docs.Product | ||
import io.micronaut.messaging.annotation.SendTo | ||
import reactor.core.publisher.Mono | ||
// end::imports[] | ||
|
||
@Slf4j | ||
@Requires(property = 'spec.name', value = 'SendToProductListenerTest') | ||
@KafkaListener(offsetReset = OffsetReset.EARLIEST) | ||
class ProductListener { | ||
|
||
// tag::method[] | ||
@Topic("sendto-products") // <1> | ||
@SendTo("product-quantities") // <2> | ||
int receive(@KafkaKey String brand, Product product) { | ||
log.info("Got Product - {} by {}", product.name, brand) | ||
product.quantity // <3> | ||
} | ||
// end::method[] | ||
|
||
// tag::reactive[] | ||
@Topic("sendto-products") // <1> | ||
@SendTo("product-quantities") // <2> | ||
Mono<Integer> receiveProduct(@KafkaKey String brand, Mono<Product> productSingle) { | ||
productSingle.map(product -> { | ||
log.info("Got Product - {} by {}", product.name, brand) | ||
product.quantity // <3> | ||
}) | ||
} | ||
// end::reactive[] | ||
} |
22 changes: 22 additions & 0 deletions
22
...te-groovy/src/test/groovy/io/micronaut/kafka/docs/consumer/sendto/QuantityListener.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,22 @@ | ||
package io.micronaut.kafka.docs.consumer.sendto | ||
|
||
import groovy.util.logging.Slf4j | ||
import io.micronaut.configuration.kafka.annotation.KafkaKey | ||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.OffsetReset | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
|
||
@Slf4j | ||
@Requires(property = 'spec.name', value = 'SendToProductListenerTest') | ||
@KafkaListener(offsetReset = OffsetReset.EARLIEST) | ||
class QuantityListener { | ||
|
||
Integer quantity | ||
|
||
@Topic("product-quantities") | ||
int receive(@KafkaKey String brand, Integer quantity) { | ||
log.info("Got Quantity - {} by {}", quantity, brand) | ||
this.quantity = quantity | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../src/test/groovy/io/micronaut/kafka/docs/consumer/sendto/SendToProductListenerTest.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,26 @@ | ||
package io.micronaut.kafka.docs.consumer.sendto | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.kafka.docs.Product | ||
import spock.lang.Specification | ||
import spock.util.concurrent.PollingConditions | ||
|
||
class SendToProductListenerTest extends Specification { | ||
void "test Send Product"() { | ||
given: | ||
ApplicationContext ctx = ApplicationContext.run( | ||
'kafka.enabled': true, 'spec.name': 'SendToProductListenerTest' | ||
) | ||
|
||
when: | ||
Product product = new Product("Blue Trainers", 5) | ||
ProductClient client = ctx.getBean(ProductClient.class) | ||
client.send("Nike", product) | ||
QuantityListener listener = ctx.getBean(QuantityListener) | ||
|
||
then: | ||
new PollingConditions(timeout: 10).eventually { | ||
listener.quantity == 5 | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...e-groovy/src/test/groovy/io/micronaut/kafka/docs/consumer/sendto/WordCountListener.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,27 @@ | ||
package io.micronaut.kafka.docs.consumer.sendto | ||
|
||
import io.micronaut.configuration.kafka.annotation.KafkaKey | ||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.OffsetReset | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import org.slf4j.Logger | ||
|
||
import static org.slf4j.LoggerFactory.getLogger | ||
|
||
@Requires(property = 'spec.name', value = 'WordCounterTest') | ||
@KafkaListener(offsetReset = OffsetReset.EARLIEST) | ||
class WordCountListener { | ||
|
||
private static final Logger LOG = getLogger(WordCountListener.class) | ||
|
||
Map<String, Integer> wordCount = [:] | ||
|
||
@Topic("my-words-count") | ||
void receive(@KafkaKey byte[] key, Object value) { | ||
final String word = new String(key) | ||
final Integer count = (Integer) value | ||
LOG.info("Got word count - {}: {}", word, count) | ||
this.wordCount.put(word, count) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
test-suite-groovy/src/test/groovy/io/micronaut/kafka/docs/consumer/sendto/WordCounter.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,33 @@ | ||
package io.micronaut.kafka.docs.consumer.sendto | ||
|
||
import io.micronaut.configuration.kafka.KafkaMessage | ||
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.messaging.annotation.SendTo | ||
import org.apache.kafka.common.IsolationLevel | ||
|
||
@Requires(property = 'spec.name', value = 'WordCounterTest') | ||
// tag::transactional[] | ||
@KafkaListener( | ||
offsetReset = OffsetReset.EARLIEST, | ||
producerClientId = 'word-counter-producer', // <1> | ||
producerTransactionalId = 'tx-word-counter-id', // <2> | ||
offsetStrategy = OffsetStrategy.SEND_TO_TRANSACTION, // <3> | ||
isolation = IsolationLevel.READ_COMMITTED // <4> | ||
) | ||
class WordCounter { | ||
|
||
@Topic('tx-incoming-strings') | ||
@SendTo('my-words-count') | ||
List<KafkaMessage<byte[], Integer>> wordsCounter(String string) { | ||
string.split("\\s+") | ||
.groupBy() | ||
.collect { key, instanceList -> | ||
KafkaMessage.Builder.withBody(instanceList.size()).key(key.bytes).build() | ||
} | ||
} | ||
} | ||
// end::transactional[] |
13 changes: 13 additions & 0 deletions
13
...e-groovy/src/test/groovy/io/micronaut/kafka/docs/consumer/sendto/WordCounterClient.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,13 @@ | ||
package io.micronaut.kafka.docs.consumer.sendto | ||
|
||
import io.micronaut.configuration.kafka.annotation.KafkaClient | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
|
||
@Requires(property = 'spec.name', value = 'WordCounterTest') | ||
@KafkaClient('word-counter-producer') | ||
interface WordCounterClient { | ||
|
||
@Topic('tx-incoming-strings') | ||
void send(String words) | ||
} |
31 changes: 31 additions & 0 deletions
31
...ite-groovy/src/test/groovy/io/micronaut/kafka/docs/consumer/sendto/WordCounterTest.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,31 @@ | ||
package io.micronaut.kafka.docs.consumer.sendto | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import spock.lang.Specification | ||
import spock.util.concurrent.PollingConditions | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS | ||
|
||
class WordCounterTest extends Specification { | ||
|
||
void "test Word Counter"() { | ||
given: | ||
ApplicationContext ctx = ApplicationContext.run( | ||
'kafka.enabled': true, 'spec.name': 'WordCounterTest' | ||
) | ||
|
||
when: | ||
WordCounterClient client = ctx.getBean(WordCounterClient.class) | ||
client.send('test to test for words') | ||
|
||
then: | ||
WordCountListener listener = ctx.getBean(WordCountListener.class) | ||
new PollingConditions(timeout: 10).eventually { | ||
listener.wordCount.size() == 4 | ||
listener.wordCount.get("test") == 2 | ||
listener.wordCount.get("to") == 1 | ||
listener.wordCount.get("for") == 1 | ||
listener.wordCount.get("words") == 1 | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
test-suite-kotlin/src/test/kotlin/io/micronaut/kafka/docs/consumer/sendto/ProductClient.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,15 @@ | ||
package io.micronaut.kafka.docs.consumer.sendto | ||
|
||
import io.micronaut.configuration.kafka.annotation.KafkaClient | ||
import io.micronaut.configuration.kafka.annotation.KafkaKey | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.kafka.docs.Product | ||
|
||
@Requires(property = "spec.name", value = "SendToProductListenerTest") | ||
@KafkaClient("product-client") | ||
interface ProductClient { | ||
|
||
@Topic("sendto-products") | ||
fun send(@KafkaKey brand: String, product: Product) | ||
} |
Oops, something went wrong.