BAEL-2378 Fix Non-blocking Spring Boot with Kotlin Coroutines
This commit is contained in:
parent
9154f5e062
commit
0a4f2a1148
@ -5,6 +5,8 @@ import com.baeldung.nonblockingcoroutines.repository.ProductRepositoryCoroutines
|
|||||||
import kotlinx.coroutines.Deferred
|
import kotlinx.coroutines.Deferred
|
||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.FlowPreview
|
||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
|
import kotlinx.coroutines.CoroutineStart
|
||||||
|
import kotlinx.coroutines.coroutineScope
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
@ -28,17 +30,17 @@ class ProductControllerCoroutines {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}/stock")
|
@GetMapping("/{id}/stock")
|
||||||
suspend fun findOneInStock(@PathVariable id: Int): ProductStockView {
|
suspend fun findOneInStock(@PathVariable id: Int): ProductStockView = coroutineScope {
|
||||||
val product: Deferred<Product?> = GlobalScope.async {
|
val product: Deferred<Product?> = async(start = CoroutineStart.LAZY) {
|
||||||
productRepository.getProductById(id)
|
productRepository.getProductById(id)
|
||||||
}
|
}
|
||||||
val quantity: Deferred<Int> = GlobalScope.async {
|
val quantity: Deferred<Int> = async(start = CoroutineStart.LAZY) {
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.uri("/stock-service/product/$id/quantity")
|
.uri("/stock-service/product/$id/quantity")
|
||||||
.accept(APPLICATION_JSON)
|
.accept(APPLICATION_JSON)
|
||||||
.awaitExchange().awaitBody<Int>()
|
.awaitExchange().awaitBody<Int>()
|
||||||
}
|
}
|
||||||
return ProductStockView(product.await()!!, quantity.await())
|
ProductStockView(product.await()!!, quantity.await())
|
||||||
}
|
}
|
||||||
|
|
||||||
@FlowPreview
|
@FlowPreview
|
||||||
|
Loading…
x
Reference in New Issue
Block a user