1
1
package com.omprakash.springbootplayground.controllers
2
2
3
+ import brave.baggage.BaggageField
3
4
import com.omprakash.springbootplayground.kafka.BookCreatedKafkaProducer
4
5
import com.omprakash.springbootplayground.models.Book
6
+ import com.omprakash.springbootplayground.models.request.CreateBook
5
7
import com.omprakash.springbootplayground.services.BookService
8
+ import io.micrometer.core.instrument.kotlin.asContextElement
9
+ import io.micrometer.observation.ObservationRegistry
6
10
import kotlinx.coroutines.flow.Flow
11
+ import kotlinx.coroutines.reactor.mono
12
+ import org.slf4j.LoggerFactory
13
+ import org.springframework.beans.factory.annotation.Qualifier
7
14
import org.springframework.web.bind.annotation.GetMapping
15
+ import org.springframework.web.bind.annotation.PostMapping
16
+ import org.springframework.web.bind.annotation.RequestBody
8
17
import org.springframework.web.bind.annotation.RequestMapping
9
18
import org.springframework.web.bind.annotation.RestController
19
+ import reactor.core.publisher.Mono
10
20
11
21
@RestController
12
22
@RequestMapping(" /books" )
13
- class BookController (private val bookService : BookService , private val bookKafkaProducer : BookCreatedKafkaProducer ) {
23
+ class BookController (
24
+ private val bookService : BookService ,
25
+ private val bookKafkaProducer : BookCreatedKafkaProducer ,
26
+ @Qualifier(" bookId" ) var bookIdBaggageField : BaggageField ,
27
+ private val observationRegistry : ObservationRegistry
28
+ ) {
29
+
30
+ var logger = LoggerFactory .getLogger(this ::class .java)
14
31
15
32
@GetMapping
16
33
fun findAll (): Flow <Book > {
17
34
return bookService.findAll()
18
35
}
19
36
20
- @GetMapping(" /publish" )
21
- suspend fun publishBook (): String {
22
- return try {
23
- bookKafkaProducer.publishBook(1L , " Test" , " 1234" ).toString()
24
- } catch (e: Exception ) {
25
- println (" Exception while publishing books ${e.message} " )
26
- e.message ? : " Exception"
37
+ @PostMapping(" /publish" )
38
+ fun publishBook (@RequestBody createBook : CreateBook ): Mono <String > {
39
+ return mono(observationRegistry.asContextElement()) {
40
+ try {
41
+ bookIdBaggageField.updateValue(createBook.id.toString())
42
+ bookKafkaProducer.publishBook(createBook.id, createBook.title, createBook.isbn).toString()
43
+ logger.info(" Book published" )
44
+ } catch (e: Exception ) {
45
+ println (" Exception while publishing books ${e.message} " )
46
+ e.message ? : " Exception"
47
+ }
48
+ " Completed"
27
49
}
28
50
}
29
51
}
0 commit comments