49 lines
1.4 KiB
Kotlin
49 lines
1.4 KiB
Kotlin
package mq
|
|
|
|
import api.ApiObject
|
|
import com.google.gson.Gson
|
|
import com.rabbitmq.client.AMQP
|
|
import com.rabbitmq.client.Consumer
|
|
import com.rabbitmq.client.Envelope
|
|
import com.rabbitmq.client.ShutdownSignalException
|
|
import database.service.ISampleObjectService
|
|
import org.koin.core.component.KoinComponent
|
|
import org.koin.core.component.inject
|
|
|
|
class DatabaseConsumer : Consumer, KoinComponent {
|
|
private val resultObjectService: ISampleObjectService by inject()
|
|
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 handleShutdownSignal(consumerTag: String?, sig: ShutdownSignalException?) {
|
|
println("got shutdown signal")
|
|
}
|
|
|
|
override fun handleDelivery(
|
|
consumerTag: String?,
|
|
envelope: Envelope?,
|
|
basicProperties: AMQP.BasicProperties?,
|
|
body: ByteArray?
|
|
) {
|
|
try {
|
|
val rawJson = body!!.toString(Charsets.UTF_8)
|
|
val apiObject = gson.fromJson(rawJson, ApiObject::class.java)
|
|
resultObjectService.addOne(apiObject)
|
|
} catch (e: Exception) {
|
|
println(e.stackTrace.toString())
|
|
}
|
|
}
|
|
} |