-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96a1a9c
commit 3c2382c
Showing
14 changed files
with
278 additions
and
4 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
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,12 @@ | ||
plugins { | ||
id("cnt-micronaut.micronaut-conventions") | ||
id("io.micronaut.test-resources") | ||
} | ||
|
||
dependencies { | ||
implementation("io.micronaut.kafka:micronaut-kafka") | ||
} | ||
|
||
application { | ||
mainClass.set("example.micronaut.ApplicationKt") | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/kafka/src/main/kotlin/example/micronaut/kafka/Application.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,9 @@ | ||
package example.micronaut.kafka | ||
|
||
import io.micronaut.runtime.Micronaut | ||
|
||
|
||
@Suppress("SpreadOperator") | ||
fun main(args: Array<String>) { | ||
Micronaut.run(*args) | ||
} |
37 changes: 37 additions & 0 deletions
37
examples/kafka/src/main/kotlin/example/micronaut/kafka/business/events.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,37 @@ | ||
package example.micronaut.kafka.business | ||
|
||
import java.util.UUID | ||
|
||
sealed class BookEvent { | ||
abstract val type: String | ||
abstract val eventId: UUID | ||
abstract val bookId: UUID | ||
} | ||
|
||
data class BookCreatedEvent( | ||
override val eventId: UUID, | ||
override val bookId: UUID, | ||
val book: Book | ||
) : BookEvent() { | ||
override val type: String = "book-created" | ||
} | ||
|
||
data class BookDeletedEvent( | ||
override val eventId: UUID, | ||
override val bookId: UUID | ||
) : BookEvent() { | ||
override val type: String = "book-deleted" | ||
} | ||
|
||
fun BookRecord.createdEvent() = | ||
BookCreatedEvent( | ||
eventId = UUID.randomUUID(), | ||
bookId = this.id, | ||
book = this.book | ||
) | ||
|
||
fun BookRecord.deletedEvent() = | ||
BookDeletedEvent( | ||
eventId = UUID.randomUUID(), | ||
bookId = this.id | ||
) |
13 changes: 13 additions & 0 deletions
13
examples/kafka/src/main/kotlin/example/micronaut/kafka/business/model.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,13 @@ | ||
package example.micronaut.kafka.business | ||
|
||
import java.util.UUID | ||
|
||
data class Book( | ||
val isbn: String, | ||
val title: String | ||
) | ||
|
||
data class BookRecord( | ||
val id: UUID, | ||
val book: Book | ||
) |
57 changes: 57 additions & 0 deletions
57
examples/kafka/src/main/kotlin/example/micronaut/kafka/events/BookEventHandler.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,57 @@ | ||
package example.micronaut.kafka.events | ||
|
||
import example.micronaut.kafka.business.BookCreatedEvent | ||
import example.micronaut.kafka.business.BookDeletedEvent | ||
import example.micronaut.kafka.business.BookEvent | ||
import io.micronaut.configuration.kafka.annotation.ErrorStrategy | ||
import io.micronaut.configuration.kafka.annotation.ErrorStrategyValue | ||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.OffsetReset.EARLIEST | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.configuration.kafka.exceptions.DefaultKafkaListenerExceptionHandler | ||
import io.micronaut.configuration.kafka.exceptions.KafkaListenerException | ||
import io.micronaut.configuration.kafka.exceptions.KafkaListenerExceptionHandler | ||
import io.micronaut.context.annotation.Replaces | ||
import jakarta.inject.Singleton | ||
import org.slf4j.LoggerFactory.getLogger | ||
|
||
@KafkaListener( | ||
offsetReset = EARLIEST, | ||
errorStrategy = ErrorStrategy( | ||
value = ErrorStrategyValue.RESUME_AT_NEXT_RECORD, | ||
) | ||
) | ||
open class BookEventHandler { | ||
|
||
private val log = getLogger(javaClass) | ||
|
||
@Topic("book-created") | ||
open fun handleCreatedEvent(event: BookCreatedEvent) { | ||
log.info("”Book was created: $event") | ||
} | ||
|
||
@Topic("book-deleted") | ||
open fun handleDeletedEvent(event: BookDeletedEvent) { | ||
log.info("Book was deleted: $event") | ||
} | ||
} | ||
|
||
@Singleton | ||
@Replaces(DefaultKafkaListenerExceptionHandler::class) | ||
class KafkaListenerExceptionHandlerFactory( | ||
private val publishEvent: PublishEventFunction | ||
) : KafkaListenerExceptionHandler { | ||
|
||
private val log = getLogger(javaClass) | ||
|
||
override fun handle(exception: KafkaListenerException?) { | ||
log.error("Failed to publish record due to: {}", exception?.message, exception) | ||
|
||
exception?.consumerRecord?.ifPresent { record -> | ||
when (val event = record.value()) { | ||
is BookEvent -> publishEvent.bookDeadLetter(event) | ||
else -> log.info("Cannot process events that are not of type ${BookEvent::class.simpleName}.") | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/kafka/src/main/kotlin/example/micronaut/kafka/events/DeadLetterHandler.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.micronaut.kafka.events | ||
|
||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
import org.slf4j.LoggerFactory.getLogger | ||
|
||
@KafkaListener | ||
open class DeadLetterHandler { | ||
|
||
private val log = getLogger(javaClass) | ||
|
||
@Topic("book-dead-letter") | ||
open fun handleDeadLetterEvent(record: ConsumerRecord<String, *>) { | ||
log.error("Dead letter message arrived: ${record.value()}") | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/kafka/src/main/kotlin/example/micronaut/kafka/events/PublishEventFunction.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,20 @@ | ||
package example.micronaut.kafka.events | ||
|
||
import example.micronaut.kafka.business.BookCreatedEvent | ||
import example.micronaut.kafka.business.BookDeletedEvent | ||
import example.micronaut.kafka.business.BookEvent | ||
import io.micronaut.configuration.kafka.annotation.KafkaClient | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
|
||
@KafkaClient | ||
interface PublishEventFunction { | ||
|
||
@Topic("book-created") | ||
operator fun invoke(event: BookCreatedEvent) | ||
|
||
@Topic("book-deleted") | ||
operator fun invoke(event: BookDeletedEvent) | ||
|
||
@Topic("book-dead-letter") | ||
fun bookDeadLetter(event: BookEvent) | ||
} |
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 @@ | ||
|
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,10 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
<root level="info"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
57 changes: 57 additions & 0 deletions
57
examples/kafka/src/test/kotlin/example/micronaut/kafka/MessagingIntegrationTests.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,57 @@ | ||
package example.micronaut.kafka | ||
|
||
import example.micronaut.kafka.business.createdEvent | ||
import example.micronaut.kafka.business.deletedEvent | ||
import example.micronaut.kafka.events.DeadLetterHandler | ||
import example.micronaut.kafka.events.BookEventHandler | ||
import example.micronaut.kafka.events.PublishEventFunction | ||
import example.spring.boot.kafka.business.Examples.cleanCode | ||
import io.micronaut.test.annotation.MockBean | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.spyk | ||
import io.mockk.verify | ||
import org.junit.jupiter.api.Test | ||
|
||
@MicronautTest | ||
class MessagingIntegrationTests( | ||
private val publishEvent: PublishEventFunction, | ||
private val bookEventHandler: BookEventHandler, | ||
private val deadLetterHandler: DeadLetterHandler | ||
) { | ||
|
||
@MockBean(BookEventHandler::class) | ||
fun mockEventHandler() = mockk<BookEventHandler>(relaxed = true) | ||
|
||
@MockBean(DeadLetterHandler::class) | ||
fun mockDeadLetterHandler() = spyk<DeadLetterHandler>() | ||
|
||
@Test | ||
fun `handles BookCreatedEvent`() { | ||
val event = cleanCode.createdEvent() | ||
|
||
publishEvent(event) | ||
|
||
verify(timeout = 3_000) { bookEventHandler.handleCreatedEvent(event) } | ||
} | ||
|
||
@Test | ||
fun `handles BookDeletedEvent`() { | ||
val event = cleanCode.deletedEvent() | ||
|
||
publishEvent(event) | ||
|
||
verify(timeout = 3_000) { bookEventHandler.handleDeletedEvent(event) } | ||
} | ||
|
||
@Test | ||
fun `puts failure messages to dead letter queue`(){ | ||
every { bookEventHandler.handleCreatedEvent(any()) } throws IllegalArgumentException("upsi") | ||
val event = cleanCode.createdEvent() // used as representation | ||
|
||
publishEvent(event) | ||
|
||
verify(timeout = 3_000) { deadLetterHandler.handleDeadLetterEvent(any()) } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/kafka/src/test/kotlin/example/micronaut/kafka/business/Examples.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.spring.boot.kafka.business | ||
|
||
import example.micronaut.kafka.business.Book | ||
import example.micronaut.kafka.business.BookRecord | ||
import java.util.UUID | ||
|
||
object Examples { | ||
|
||
val cleanCode = BookRecord( | ||
id = UUID.fromString("b3fc0be8-463e-4875-9629-67921a1e00f4"), | ||
book = Book( | ||
isbn = "9780132350884", | ||
title = "Clean Code" | ||
) | ||
) | ||
|
||
} |
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,5 @@ | ||
test-resources: | ||
containers: | ||
kafka: | ||
image-name: confluentinc/cp-kafka:7.0.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