|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Kafka Messaging" |
| 4 | +order: 22 |
| 5 | +--- |
| 6 | + |
| 7 | +MicroShed Testing provides integration with applications using [Apache Kafka](https://kafka.apache.org/) for messaging. Apache Kafka is |
| 8 | +a messaging engine that is commonly used with Java microservice applications, and also is commonly used with [MicroProfile Reactive Messaging(https://github.com/eclipse/microprofile-reactive-messaging). |
| 9 | + |
| 10 | +## Sending and receiving messages from tests |
| 11 | + |
| 12 | +If an application purely uses Kafka Messaging for communication, a true-to-production way of testing is to also have the test client driving requests |
| 13 | +on the application via message passing. To do this, MicroShed Testing offers two annotations: `@KafkaConsumerConfig` and `@KafkaProducerConfig` |
| 14 | + |
| 15 | +### Example setup |
| 16 | + |
| 17 | +To begin using Kafka with MicroShed Testing, define a `KafkaContainer` in the test environment: |
| 18 | + |
| 19 | +```java |
| 20 | +import org.testcontainers.containers.KafkaContainer; |
| 21 | +import org.testcontainers.containers.Network; |
| 22 | +// other imports ... |
| 23 | + |
| 24 | +public class AppContainerConfig implements SharedContainerConfiguration { |
| 25 | + |
| 26 | + private static Network network = Network.newNetwork(); |
| 27 | + |
| 28 | + @Container |
| 29 | + public static KafkaContainer kafka = new KafkaContainer() |
| 30 | + .withNetwork(network); |
| 31 | + |
| 32 | + @Container |
| 33 | + public static ApplicationContainer app = new ApplicationContainer() |
| 34 | + .withNetwork(network) |
| 35 | + .dependsOn(kafka); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Runtimes such as OpenLiberty and Quarkus will be automatically configured if a `KafkaContainer` is present |
| 40 | +in the test environment. For other runtimes, you can link the containers together by using `kafka.withNetworkAlias("kafka")` |
| 41 | +and `app.withEnv("<runtime-specific kafka bootstrap servers property>", "kafka:9092")`. |
| 42 | + |
| 43 | + |
| 44 | +### Example usage |
| 45 | + |
| 46 | +```java |
| 47 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 48 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 49 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 50 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 51 | +import org.microshed.testing.kafka.KafkaConsumerConfig; |
| 52 | +import org.microshed.testing.kafka.KafkaProducerConfig; |
| 53 | +// other imports ... |
| 54 | + |
| 55 | +@MicroShedTest |
| 56 | +@SharedContainerConfig(AppContainerConfig.class) |
| 57 | +public class KitchenEndpointIT { |
| 58 | + |
| 59 | + @KafkaProducerConfig(keySerializer = StringSerializer.class, // (1) |
| 60 | + valueSerializer = StringSerializer.class) |
| 61 | + public static KafkaProducer<String, String> producer; |
| 62 | + |
| 63 | + @KafkaConsumerConfig(keyDeserializer = StringDeserializer.class, |
| 64 | + valueDeserializer = StringDeserializer.class, |
| 65 | + groupId = "update-status", |
| 66 | + topics = "statusTopic") // (2) |
| 67 | + public static KafkaConsumer<String, String> consumer; |
| 68 | + |
| 69 | + @Test |
| 70 | + public void myTest() { |
| 71 | + // Use the producer to send messages |
| 72 | + producer.send(...); |
| 73 | + |
| 74 | + // Use the consumer to poll for records |
| 75 | + ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(30)); |
| 76 | + // ... |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +1. Each `@KafkaProducerConfig` and `@KafkaConsumerConfig` must define a set of key/value [de]serializers |
| 82 | +that correspond to the key/value types defined in the `KafkaProducer` and `KafkaConsumer`. |
| 83 | +2. For `@KafkaConsumerConfig` zero or more `topics` may be specified to automatically subscribe the |
| 84 | +injected `consumer` to the specified `topics`. |
| 85 | + |
| 86 | + |
| 87 | +## Additional resources |
| 88 | + |
| 89 | +- [Example application using Apache Kafka messaging](https://github.com/MicroShed/microshed-testing/tree/master/sample-apps/kafka-app) |
| 90 | +- [OpenLiberty blog on using MicroProfile Reactive Messaging](https://openliberty.io/blog/2019/09/13/microprofile-reactive-messaging.html) |
| 91 | +- [Quarkus guide on using Apache Kafka with Reactive Messaging](https://quarkus.io/guides/kafka) |
0 commit comments