-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
20 changed files
with
769 additions
and
2 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
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
37 changes: 37 additions & 0 deletions
37
ok-marketplace-be/ok-marketplace-app-rabbit/build.gradle.kts
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 @@ | ||
plugins { | ||
id("build-jvm") | ||
application | ||
alias(libs.plugins.shadowJar) | ||
alias(libs.plugins.muschko.java) | ||
} | ||
|
||
application { | ||
mainClass.set("ru.otus.otuskotlin.marketplace.app.rabbit.ApplicationKt") | ||
} | ||
|
||
dependencies { | ||
|
||
implementation(kotlin("stdlib")) | ||
|
||
implementation(libs.rabbitmq.client) | ||
implementation(libs.jackson.databind) | ||
implementation(libs.logback) | ||
implementation(libs.coroutines.core) | ||
|
||
implementation(project(":ok-marketplace-common")) | ||
implementation(project(":ok-marketplace-app-common")) | ||
implementation("ru.otus.otuskotlin.marketplace.libs:ok-marketplace-lib-logging-logback") | ||
|
||
// v1 api | ||
implementation(project(":ok-marketplace-api-v1-jackson")) | ||
implementation(project(":ok-marketplace-api-v1-mappers")) | ||
|
||
// v2 api | ||
implementation(project(":ok-marketplace-api-v2-kmp")) | ||
|
||
implementation(project(":ok-marketplace-biz")) | ||
implementation(project(":ok-marketplace-stubs")) | ||
|
||
testImplementation(libs.testcontainers.rabbitmq) | ||
testImplementation(kotlin("test")) | ||
} |
20 changes: 20 additions & 0 deletions
20
ok-marketplace-be/ok-marketplace-app-rabbit/src/main/kotlin/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,20 @@ | ||
package ru.otus.otuskotlin.marketplace.app.rabbit | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import ru.otus.otuskotlin.marketplace.app.rabbit.config.MkplAppSettings | ||
import ru.otus.otuskotlin.marketplace.app.rabbit.config.RabbitConfig | ||
import ru.otus.otuskotlin.marketplace.app.rabbit.mappers.fromArgs | ||
import ru.otus.otuskotlin.marketplace.common.MkplCorSettings | ||
import ru.otus.otuskotlin.marketplace.logging.common.MpLoggerProvider | ||
import ru.otus.otuskotlin.marketplace.logging.jvm.mpLoggerLogback | ||
|
||
fun main(vararg args: String) = runBlocking { | ||
val appSettings = MkplAppSettings( | ||
rabbit = RabbitConfig.fromArgs(*args), | ||
corSettings = MkplCorSettings( | ||
loggerProvider = MpLoggerProvider { mpLoggerLogback(it) } | ||
) | ||
) | ||
val app = RabbitApp(appSettings = appSettings, this) | ||
app.start() | ||
} |
48 changes: 48 additions & 0 deletions
48
ok-marketplace-be/ok-marketplace-app-rabbit/src/main/kotlin/RabbitApp.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,48 @@ | ||
package ru.otus.otuskotlin.marketplace.app.rabbit | ||
|
||
import kotlinx.coroutines.* | ||
import ru.otus.otuskotlin.marketplace.app.rabbit.config.MkplAppSettings | ||
import ru.otus.otuskotlin.marketplace.app.rabbit.controllers.IRabbitMqController | ||
import ru.otus.otuskotlin.marketplace.app.rabbit.controllers.RabbitDirectControllerV1 | ||
import ru.otus.otuskotlin.marketplace.app.rabbit.controllers.RabbitDirectControllerV2 | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
// Класс запускает все контроллеры | ||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class RabbitApp( | ||
appSettings: MkplAppSettings, | ||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default), | ||
) : AutoCloseable { | ||
private val logger = appSettings.corSettings.loggerProvider.logger(this::class) | ||
private val controllers: List<IRabbitMqController> = listOf( | ||
RabbitDirectControllerV1(appSettings), | ||
RabbitDirectControllerV2(appSettings), | ||
) | ||
private val runFlag = AtomicBoolean(true) | ||
|
||
fun start() { | ||
runFlag.set(true) | ||
controllers.forEach { | ||
scope.launch( | ||
Dispatchers.IO.limitedParallelism(1) + CoroutineName("thread-${it.exchangeConfig.consumerTag}") | ||
) { | ||
while (runFlag.get()) { | ||
try { | ||
logger.info("Process...${it.exchangeConfig.consumerTag}") | ||
it.process() | ||
} catch (e: RuntimeException) { | ||
// логируем, что-то делаем | ||
logger.error("Обработка завалена, возможно из-за потери соединения с RabbitMQ. Рестартуем") | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun close() { | ||
runFlag.set(false) | ||
controllers.forEach { it.close() } | ||
scope.cancel() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
ok-marketplace-be/ok-marketplace-app-rabbit/src/main/kotlin/config/IMkplAppRabbitSettings.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 ru.otus.otuskotlin.marketplace.app.rabbit.config | ||
|
||
import ru.otus.otuskotlin.marketplace.app.common.IMkplAppSettings | ||
|
||
interface IMkplAppRabbitSettings: IMkplAppSettings { | ||
val rabbit: RabbitConfig | ||
val controllersConfigV1: RabbitExchangeConfiguration | ||
val controllersConfigV2: RabbitExchangeConfiguration | ||
} |
13 changes: 13 additions & 0 deletions
13
ok-marketplace-be/ok-marketplace-app-rabbit/src/main/kotlin/config/MkplAppSettings.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 ru.otus.otuskotlin.marketplace.app.rabbit.config | ||
|
||
import ru.otus.otuskotlin.marketplace.app.common.IMkplAppSettings | ||
import ru.otus.otuskotlin.marketplace.biz.MkplAdProcessor | ||
import ru.otus.otuskotlin.marketplace.common.MkplCorSettings | ||
|
||
data class MkplAppSettings( | ||
override val corSettings: MkplCorSettings = MkplCorSettings(), | ||
override val processor: MkplAdProcessor = MkplAdProcessor(corSettings), | ||
override val rabbit: RabbitConfig = RabbitConfig(), | ||
override val controllersConfigV1: RabbitExchangeConfiguration = RabbitExchangeConfiguration.NONE, | ||
override val controllersConfigV2: RabbitExchangeConfiguration = RabbitExchangeConfiguration.NONE, | ||
): IMkplAppSettings, IMkplAppRabbitSettings |
15 changes: 15 additions & 0 deletions
15
ok-marketplace-be/ok-marketplace-app-rabbit/src/main/kotlin/config/RabbitConfig.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 ru.otus.otuskotlin.marketplace.app.rabbit.config | ||
|
||
data class RabbitConfig( | ||
val host: String = HOST, | ||
val port: Int = PORT, | ||
val user: String = USER, | ||
val password: String = PASSWORD | ||
) { | ||
companion object { | ||
const val HOST = "localhost" | ||
const val PORT = 5672 | ||
const val USER = "guest" | ||
const val PASSWORD = "guest" | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...tplace-be/ok-marketplace-app-rabbit/src/main/kotlin/config/RabbitExchangeConfiguration.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 ru.otus.otuskotlin.marketplace.app.rabbit.config | ||
|
||
// наш класс настроек взаимодействия с RMQ | ||
data class RabbitExchangeConfiguration( | ||
val keyIn: String = "", | ||
val keyOut: String = "", | ||
// Отправляем сообщение в обменник | ||
val exchange: String = "", | ||
// Подписываемся на очередь | ||
val queue: String = "", | ||
val consumerTag: String = "", | ||
val exchangeType: String = "direct" // Объявляем обменник типа "direct" (сообщения передаются в те очереди, где ключ совпадает) | ||
) { | ||
companion object { | ||
val NONE = RabbitExchangeConfiguration() | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...rketplace-be/ok-marketplace-app-rabbit/src/main/kotlin/controllers/IRabbitMqController.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,10 @@ | ||
package ru.otus.otuskotlin.marketplace.app.rabbit.controllers | ||
|
||
import ru.otus.otuskotlin.marketplace.app.rabbit.config.RabbitExchangeConfiguration | ||
|
||
interface IRabbitMqController { | ||
val exchangeConfig: RabbitExchangeConfiguration | ||
suspend fun process() | ||
fun close() | ||
} | ||
|
50 changes: 50 additions & 0 deletions
50
...lace-be/ok-marketplace-app-rabbit/src/main/kotlin/controllers/RabbitDirectControllerV1.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,50 @@ | ||
package ru.otus.otuskotlin.marketplace.app.rabbit.controllers | ||
|
||
import com.rabbitmq.client.Channel | ||
import com.rabbitmq.client.Delivery | ||
import ru.otus.otuskotlin.marketplace.api.v1.apiV1Mapper | ||
import ru.otus.otuskotlin.marketplace.api.v1.models.IRequest | ||
import ru.otus.otuskotlin.marketplace.app.common.controllerHelper | ||
import ru.otus.otuskotlin.marketplace.app.rabbit.config.MkplAppSettings | ||
import ru.otus.otuskotlin.marketplace.common.MkplContext | ||
import ru.otus.otuskotlin.marketplace.common.helpers.asMkplError | ||
import ru.otus.otuskotlin.marketplace.common.models.MkplState | ||
import ru.otus.otuskotlin.marketplace.mappers.v1.fromTransport | ||
import ru.otus.otuskotlin.marketplace.mappers.v1.toTransportAd | ||
|
||
// наследник RabbitProcessorBase, увязывает транспортную и бизнес-части | ||
class RabbitDirectControllerV1( | ||
private val appSettings: MkplAppSettings, | ||
) : RabbitProcessorBase( | ||
rabbitConfig = appSettings.rabbit, | ||
exchangeConfig = appSettings.controllersConfigV1, | ||
loggerProvider = appSettings.corSettings.loggerProvider, | ||
) { | ||
override suspend fun Channel.processMessage(message: Delivery) { | ||
appSettings.controllerHelper( | ||
{ | ||
val req = apiV1Mapper.readValue(message.body, IRequest::class.java) | ||
fromTransport(req) | ||
}, | ||
{ | ||
val res = toTransportAd() | ||
apiV1Mapper.writeValueAsBytes(res).also { | ||
basicPublish(exchangeConfig.exchange, exchangeConfig.keyOut, null, it) | ||
} | ||
}, | ||
this@RabbitDirectControllerV1::class, | ||
"rabbitmq-v1-processor" | ||
) | ||
} | ||
|
||
override fun Channel.onError(e: Throwable, delivery: Delivery) { | ||
val context = MkplContext() | ||
e.printStackTrace() | ||
context.state = MkplState.FAILING | ||
context.errors.add(e.asMkplError()) | ||
val response = context.toTransportAd() | ||
apiV1Mapper.writeValueAsBytes(response).also { | ||
basicPublish(exchangeConfig.exchange, exchangeConfig.keyOut, null, it) | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...lace-be/ok-marketplace-app-rabbit/src/main/kotlin/controllers/RabbitDirectControllerV2.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,51 @@ | ||
package ru.otus.otuskotlin.marketplace.app.rabbit.controllers | ||
|
||
import com.rabbitmq.client.Channel | ||
import com.rabbitmq.client.Delivery | ||
import ru.otus.otuskotlin.marketplace.api.v2.apiV2RequestDeserialize | ||
import ru.otus.otuskotlin.marketplace.api.v2.apiV2ResponseSerialize | ||
import ru.otus.otuskotlin.marketplace.api.v2.mappers.fromTransport | ||
import ru.otus.otuskotlin.marketplace.api.v2.mappers.toTransportAd | ||
import ru.otus.otuskotlin.marketplace.api.v2.models.IRequest | ||
import ru.otus.otuskotlin.marketplace.app.common.controllerHelper | ||
import ru.otus.otuskotlin.marketplace.app.rabbit.config.MkplAppSettings | ||
import ru.otus.otuskotlin.marketplace.common.MkplContext | ||
import ru.otus.otuskotlin.marketplace.common.helpers.asMkplError | ||
import ru.otus.otuskotlin.marketplace.common.models.MkplState | ||
|
||
class RabbitDirectControllerV2( | ||
private val appSettings: MkplAppSettings, | ||
) : RabbitProcessorBase( | ||
rabbitConfig = appSettings.rabbit, | ||
exchangeConfig = appSettings.controllersConfigV2, | ||
loggerProvider = appSettings.corSettings.loggerProvider, | ||
) { | ||
|
||
override suspend fun Channel.processMessage(message: Delivery) { | ||
appSettings.controllerHelper( | ||
{ | ||
val req = apiV2RequestDeserialize<IRequest>(String(message.body)) | ||
fromTransport(req) | ||
}, | ||
{ | ||
val res = toTransportAd() | ||
apiV2ResponseSerialize(res).also { | ||
basicPublish(exchangeConfig.exchange, exchangeConfig.keyOut, null, it.toByteArray()) | ||
} | ||
}, | ||
RabbitDirectControllerV2::class, | ||
"rabbitmq-v2-processor" | ||
) | ||
} | ||
|
||
override fun Channel.onError(e: Throwable, delivery: Delivery) { | ||
val context = MkplContext() | ||
e.printStackTrace() | ||
context.state = MkplState.FAILING | ||
context.errors.add(e.asMkplError()) | ||
val response = context.toTransportAd() | ||
apiV2ResponseSerialize(response).also { | ||
basicPublish(exchangeConfig.exchange, exchangeConfig.keyOut, null, it.toByteArray()) | ||
} | ||
} | ||
} |
Oops, something went wrong.