37 lines
1.3 KiB
Kotlin
37 lines
1.3 KiB
Kotlin
package com.kmalbz.consumer
|
|
|
|
import com.google.gson.Gson
|
|
import com.kmalbz.api.model.ApiObject
|
|
import com.kmalbz.database.service.SampleObjectService
|
|
import com.rabbitmq.client.AMQP.BasicProperties
|
|
import com.rabbitmq.client.Consumer
|
|
import com.rabbitmq.client.Envelope
|
|
import com.rabbitmq.client.ShutdownSignalException
|
|
import io.ktor.util.*
|
|
|
|
@KtorExperimentalAPI
|
|
class DatabaseConsumer : Consumer {
|
|
private val sampleObjectService = SampleObjectService()
|
|
|
|
private val gson = Gson()
|
|
override fun handleConsumeOk(consumerTag : String?) {
|
|
}
|
|
override fun handleCancelOk(p0 : String?) {
|
|
throw UnsupportedOperationException()
|
|
}
|
|
override fun handleRecoverOk(p0 : String?) {
|
|
throw UnsupportedOperationException()
|
|
}
|
|
override fun handleCancel(p0 : String?) {
|
|
throw UnsupportedOperationException()
|
|
}
|
|
override fun handleDelivery(consumerTag : String?, envelope : Envelope?, basicProperties : BasicProperties?, body : ByteArray?) {
|
|
val rawJson = body!!.toString(Charsets.UTF_8)
|
|
val apiObject = gson.fromJson(rawJson, ApiObject::class.java)
|
|
|
|
sampleObjectService.addOne(apiObject)
|
|
}
|
|
override fun handleShutdownSignal(p0 : String?, p1 : ShutdownSignalException?) {
|
|
println("got shutdown signal")
|
|
}
|
|
} |