Publish message into rabbitmq on create article
Create Redis and Rabbit in docker-compose
This commit is contained in:
54
src/main/kotlin/fr/dcproject/event/EventNotification.kt
Normal file
54
src/main/kotlin/fr/dcproject/event/EventNotification.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
package fr.dcproject.event
|
||||
|
||||
import fr.dcproject.entity.Article
|
||||
import fr.postgresjson.entity.immutable.UuidEntity
|
||||
import io.ktor.application.*
|
||||
import io.ktor.util.AttributeKey
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
import kotlinx.coroutines.DisposableHandle
|
||||
import org.joda.time.DateTime
|
||||
|
||||
abstract class Notification(
|
||||
val type: String,
|
||||
val createdAt: DateTime = DateTime.now()
|
||||
)
|
||||
abstract class EntityEvent(
|
||||
val target: UuidEntity,
|
||||
type: String,
|
||||
val action: String
|
||||
) : Notification(type) {
|
||||
enum class Type(val event: EventDefinition<ArticleUpdate>) {
|
||||
UPDATE_ARTICLE(EventDefinition<ArticleUpdate>())
|
||||
}
|
||||
}
|
||||
|
||||
class ArticleUpdate(
|
||||
target: Article
|
||||
) : EntityEvent(target, "article", "update")
|
||||
|
||||
/**
|
||||
* Installation Class
|
||||
*/
|
||||
class EventNotification {
|
||||
class Configuration(private val monitor: ApplicationEvents) {
|
||||
private val subscribers = mutableListOf<DisposableHandle>()
|
||||
fun <T: Notification> subscribe(definition: EventDefinition<T>, handler: EventHandler<T>): DisposableHandle {
|
||||
return monitor.subscribe(definition, handler).also {
|
||||
subscribers.add(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object Feature : ApplicationFeature<Application, Configuration, EventNotification> {
|
||||
override val key = AttributeKey<EventNotification>("EventNotification")
|
||||
|
||||
@KtorExperimentalAPI
|
||||
override fun install(
|
||||
pipeline: Application,
|
||||
configure: Configuration.() -> Unit
|
||||
): EventNotification {
|
||||
Configuration(pipeline.environment.monitor).apply(configure)
|
||||
return EventNotification()
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/main/kotlin/fr/dcproject/event/publisher/Publisher.kt
Normal file
30
src/main/kotlin/fr/dcproject/event/publisher/Publisher.kt
Normal file
@@ -0,0 +1,30 @@
|
||||
package fr.dcproject.event.publisher
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.rabbitmq.client.ConnectionFactory
|
||||
import fr.dcproject.config
|
||||
import fr.dcproject.event.EntityEvent
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class Publisher(
|
||||
private val mapper: ObjectMapper,
|
||||
private val factory: ConnectionFactory,
|
||||
private val logger: Logger = LoggerFactory.getLogger(Publisher::class.qualifiedName)
|
||||
) {
|
||||
fun <T: EntityEvent>publish(it: T): Job {
|
||||
return GlobalScope.launch {
|
||||
factory.newConnection().use { connection -> connection.createChannel().use { channel ->
|
||||
channel.basicPublish(config.exchangeNotificationName, "", null, it.serialize().toByteArray())
|
||||
logger.debug("Publish message ${it.target.id}")
|
||||
} }
|
||||
}
|
||||
}
|
||||
|
||||
private fun EntityEvent.serialize(): String {
|
||||
return mapper.writeValueAsString(this) ?: error("Unable tu serialize message")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user