use kotlinx serialization

This commit is contained in:
Torma Kristóf 2021-08-18 16:05:34 +02:00
parent 79b68af64c
commit 2eb23dd9a6
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
5 changed files with 23 additions and 16 deletions

View File

@ -1,9 +1,20 @@
plugins { plugins {
application application
kotlin("jvm") version "1.5.10" kotlin("jvm") version "1.5.10"
kotlin("plugin.serialization") version "1.5.10"
id("com.github.johnrengelman.shadow") version "7.0.0" id("com.github.johnrengelman.shadow") version "7.0.0"
} }
buildscript {
repositories { mavenCentral() }
dependencies {
val kotlinVersion = "1.5.10"
classpath(kotlin("gradle-plugin", version = kotlinVersion))
classpath(kotlin("serialization", version = kotlinVersion))
}
}
application { application {
mainClassName = "MainKt" mainClassName = "MainKt"
} }
@ -20,13 +31,14 @@ dependencies {
implementation("com.rabbitmq:amqp-client:5.13.0") implementation("com.rabbitmq:amqp-client:5.13.0")
implementation("com.viartemev:the-white-rabbit:0.0.6") implementation("com.viartemev:the-white-rabbit:0.0.6")
implementation("com.zaxxer:HikariCP:5.0.0") implementation("com.zaxxer:HikariCP:5.0.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1")
implementation("org.jetbrains.exposed:exposed-core:0.33.1") implementation("org.jetbrains.exposed:exposed-core:0.33.1")
implementation("org.jetbrains.exposed:exposed-dao:0.33.1") implementation("org.jetbrains.exposed:exposed-dao:0.33.1")
implementation("org.jetbrains.exposed:exposed-jdbc:0.33.1") implementation("org.jetbrains.exposed:exposed-jdbc:0.33.1")
implementation("org.jetbrains.exposed:exposed-java-time:0.33.1") implementation("org.jetbrains.exposed:exposed-java-time:0.33.1")
implementation("io.insert-koin:koin-core:3.1.2") implementation("io.insert-koin:koin-core:3.1.2")
implementation("org.postgresql:postgresql:42.2.23") implementation("org.postgresql:postgresql:42.2.23")
implementation("com.google.code.gson:gson:2.8.7")
} }
tasks { tasks {

View File

@ -1,8 +1,9 @@
package api package api
import com.google.gson.annotations.SerializedName import kotlinx.serialization.Serializable
@Serializable
data class ApiObject( data class ApiObject(
@SerializedName("tag") val tag: String, val tag: String,
@SerializedName("probability") val probability: Double val probability: Double
) )

View File

@ -2,12 +2,12 @@ package database.dao
import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.`java-time`.date import org.jetbrains.exposed.sql.`java-time`.datetime
import java.time.LocalDate import java.time.LocalDateTime
object ResultObjects : IntIdTable() { object ResultObjects : IntIdTable() {
val tag: Column<String> = varchar("tag", 32) val tag: Column<String> = varchar("tag", 32)
val date: Column<LocalDate> = date("date").default(LocalDate.now()) val date: Column<LocalDateTime> = datetime("date").default(LocalDateTime.now())
val probability: Column<Double> = double("probability") val probability: Column<Double> = double("probability")
override val primaryKey = PrimaryKey(id, name = "PK_ResultObject_Id") override val primaryKey = PrimaryKey(id, name = "PK_ResultObject_Id")
} }

View File

@ -1,14 +1,8 @@
package database.service package database.service
import database.DatabaseFactory.dbQuery
import database.model.ResultObject import database.model.ResultObject
import database.dao.ResultObjects
import api.ApiObject import api.ApiObject
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import java.time.LocalDate
class ResultObjectService : IResultObjectService { class ResultObjectService : IResultObjectService {

View File

@ -1,15 +1,15 @@
package mq package mq
import api.ApiObject import api.ApiObject
import com.google.gson.Gson
import com.rabbitmq.client.* import com.rabbitmq.client.*
import database.service.IResultObjectService import database.service.IResultObjectService
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
import org.koin.core.component.inject import org.koin.core.component.inject
class DatabaseConsumer(channel: Channel): Consumer, KoinComponent { class DatabaseConsumer(channel: Channel): Consumer, KoinComponent {
private val resultObjectService : IResultObjectService by inject() private val resultObjectService : IResultObjectService by inject()
private val gson = Gson()
private val basicChannel = channel private val basicChannel = channel
override fun handleConsumeOk(consumerTag : String?) { override fun handleConsumeOk(consumerTag : String?) {
} }
@ -30,7 +30,7 @@ class DatabaseConsumer(channel: Channel): Consumer, KoinComponent {
override fun handleDelivery(consumerTag : String?, envelope : Envelope?, basicProperties : AMQP.BasicProperties?, body : ByteArray?) { override fun handleDelivery(consumerTag : String?, envelope : Envelope?, basicProperties : AMQP.BasicProperties?, body : ByteArray?) {
try { try {
val rawJson = body!!.toString(Charsets.UTF_8) val rawJson = body!!.toString(Charsets.UTF_8)
val apiObject = gson.fromJson(rawJson, ApiObject::class.java) val apiObject = Json.decodeFromString<ApiObject>(rawJson)
resultObjectService.addOne(apiObject) resultObjectService.addOne(apiObject)
basicChannel.basicAck(envelope!!.deliveryTag, false) basicChannel.basicAck(envelope!!.deliveryTag, false)
} catch (e: Exception) { } catch (e: Exception) {