use kotlinx serialization
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
874480aadb
commit
e4bcfab2af
@ -1,6 +1,7 @@
|
||||
plugins {
|
||||
application
|
||||
kotlin("jvm") version "1.5.10"
|
||||
kotlin("plugin.serialization") version "1.5.10"
|
||||
id("com.github.johnrengelman.shadow") version "7.0.0"
|
||||
}
|
||||
|
||||
@ -16,7 +17,19 @@ repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
buildscript {
|
||||
repositories { mavenCentral() }
|
||||
|
||||
dependencies {
|
||||
val kotlinVersion = "1.5.10"
|
||||
classpath(kotlin("gradle-plugin", version = kotlinVersion))
|
||||
classpath(kotlin("serialization", version = kotlinVersion))
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1")
|
||||
implementation("com.rabbitmq:amqp-client:5.13.0")
|
||||
implementation("com.viartemev:the-white-rabbit:0.0.6")
|
||||
implementation("com.zaxxer:HikariCP:5.0.0")
|
||||
@ -26,7 +39,6 @@ dependencies {
|
||||
implementation("org.jetbrains.exposed:exposed-java-time:0.33.1")
|
||||
implementation("io.insert-koin:koin-core:3.1.2")
|
||||
implementation("org.postgresql:postgresql:42.2.23")
|
||||
implementation("com.google.code.gson:gson:2.8.7")
|
||||
}
|
||||
|
||||
tasks {
|
||||
|
@ -1,10 +1,11 @@
|
||||
package api
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import java.time.LocalDate
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ApiObject(
|
||||
@SerializedName("tag") val tag: String,
|
||||
@SerializedName("device_id") val device_id: Int,
|
||||
@SerializedName("device_date") val device_date: LocalDate
|
||||
val tag: String,
|
||||
val device_id: Int,
|
||||
val device_date: LocalDate
|
||||
)
|
@ -2,6 +2,7 @@ package database.service
|
||||
|
||||
import database.model.SampleObject
|
||||
import api.ApiObject
|
||||
import kotlinx.datetime.toJavaLocalDate
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
class SampleObjectService : ISampleObjectService {
|
||||
@ -10,7 +11,7 @@ class SampleObjectService : ISampleObjectService {
|
||||
transaction {
|
||||
SampleObject.new {
|
||||
tag = apiObject.tag
|
||||
device_date = apiObject.device_date
|
||||
device_date = apiObject.device_date.toJavaLocalDate()
|
||||
device_id = apiObject.device_id
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
package mq
|
||||
|
||||
import api.ApiObject
|
||||
import com.google.gson.GsonBuilder
|
||||
import kotlinx.serialization.json.*
|
||||
import com.rabbitmq.client.*
|
||||
import database.service.ISampleObjectService
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
|
||||
class DatabaseConsumer (channel: Channel) : Consumer, KoinComponent {
|
||||
private val resultObjectService: ISampleObjectService by inject()
|
||||
private val gson = GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssz").create()
|
||||
private val basicChannel = channel
|
||||
override fun handleConsumeOk(consumerTag: String?) {
|
||||
}
|
||||
@ -38,7 +38,7 @@ class DatabaseConsumer (channel: Channel) : Consumer, KoinComponent {
|
||||
) {
|
||||
try {
|
||||
val rawJson = body!!.toString(Charsets.UTF_8)
|
||||
val apiObject = gson.fromJson(rawJson, ApiObject::class.java)
|
||||
val apiObject = Json.decodeFromString<ApiObject>(rawJson)
|
||||
resultObjectService.addOne(apiObject)
|
||||
basicChannel.basicAck(envelope!!.deliveryTag,false)
|
||||
} catch (e: Exception) {
|
||||
|
Loading…
Reference in New Issue
Block a user