This commit is contained in:
14
src/main/kotlin/Main.kt
Normal file
14
src/main/kotlin/Main.kt
Normal file
@@ -0,0 +1,14 @@
|
||||
import di.databasemodule
|
||||
import mq.ConsumerWrapper
|
||||
import org.koin.core.context.GlobalContext.startKoin
|
||||
|
||||
fun main(vararg args: String) {
|
||||
|
||||
startKoin {
|
||||
printLogger()
|
||||
modules(databasemodule)
|
||||
}
|
||||
|
||||
val consumerWrapper = ConsumerWrapper();
|
||||
consumerWrapper.recieve();
|
||||
}
|
||||
10
src/main/kotlin/api/ApiObject.kt
Normal file
10
src/main/kotlin/api/ApiObject.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
package api
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import java.time.LocalDate
|
||||
|
||||
data class ApiObject(
|
||||
@SerializedName("tag") val tag: String,
|
||||
@SerializedName("device_id") val device_id: Int,
|
||||
@SerializedName("device_date") val device_date: LocalDate
|
||||
)
|
||||
11
src/main/kotlin/config/EnvConfig.kt
Normal file
11
src/main/kotlin/config/EnvConfig.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
data class EnvConfig (
|
||||
var mqHost: String = System.getenv("MQ_HOST") ?: "localhost",
|
||||
var mqUserName: String = System.getenv("MQ_USERNAME") ?: "rabbitmq",
|
||||
var mqPassWord: String = System.getenv("MQ_PASSWORD") ?: "rabbitmq",
|
||||
var mqExchange: String = System.getenv("MQ_EXCHANGE") ?: "rabbitmq",
|
||||
var dbJdbc: String = System.getenv("DB_JDBC") ?: "input",
|
||||
var dbUsername: String = System.getenv("DB_USERNAME") ?: "output",
|
||||
var dbPassowrd: String = System.getenv("DB_PASSOWRD") ?: "output"
|
||||
)
|
||||
38
src/main/kotlin/database/DatabaseFactory.kt
Normal file
38
src/main/kotlin/database/DatabaseFactory.kt
Normal file
@@ -0,0 +1,38 @@
|
||||
package database
|
||||
|
||||
import config.EnvConfig
|
||||
import com.zaxxer.hikari.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
object DatabaseFactory {
|
||||
|
||||
private val envConfig = EnvConfig()
|
||||
private val dbUrl = envConfig.dbJdbc
|
||||
private val dbUser = envConfig.dbUsername
|
||||
private val dbPassword = envConfig.dbPassowrd
|
||||
|
||||
fun init() {
|
||||
Database.connect(hikari())
|
||||
}
|
||||
|
||||
private fun hikari(): HikariDataSource {
|
||||
val config = HikariConfig()
|
||||
config.driverClassName = "org.postgresql.Driver"
|
||||
config.jdbcUrl = dbUrl
|
||||
config.username = dbUser
|
||||
config.password = dbPassword
|
||||
config.maximumPoolSize = 3
|
||||
config.isAutoCommit = false
|
||||
config.transactionIsolation = "TRANSACTION_REPEATABLE_READ"
|
||||
config.validate()
|
||||
return HikariDataSource(config)
|
||||
}
|
||||
|
||||
suspend fun <T> dbQuery(block: () -> T): T =
|
||||
withContext(Dispatchers.IO) {
|
||||
transaction { block() }
|
||||
}
|
||||
}
|
||||
14
src/main/kotlin/database/dao/SampleObjects.kt
Normal file
14
src/main/kotlin/database/dao/SampleObjects.kt
Normal file
@@ -0,0 +1,14 @@
|
||||
package database.dao
|
||||
|
||||
import org.jetbrains.exposed.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.`java-time`.date
|
||||
import java.time.LocalDate
|
||||
|
||||
object SampleObjects : IntIdTable() {
|
||||
val tag: Column<String> = varchar("tag", 32)
|
||||
val timestamp: Column<LocalDate> = date("timestamp").default(LocalDate.now())
|
||||
val device_id: Column<Int> = integer("device_id")
|
||||
val device_date: Column<LocalDate> = date("device_date")
|
||||
override val primaryKey = PrimaryKey(id, name = "PK_SampleObject_Id")
|
||||
}
|
||||
14
src/main/kotlin/database/model/SampleObject.kt
Normal file
14
src/main/kotlin/database/model/SampleObject.kt
Normal file
@@ -0,0 +1,14 @@
|
||||
package database.model
|
||||
|
||||
import database.dao.SampleObjects
|
||||
import org.jetbrains.exposed.dao.IntEntity
|
||||
import org.jetbrains.exposed.dao.IntEntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
|
||||
class SampleObject(id: EntityID<Int>): IntEntity(id) {
|
||||
companion object : IntEntityClass<SampleObject>(SampleObjects)
|
||||
var tag by SampleObjects.tag
|
||||
var timestamp by SampleObjects.timestamp
|
||||
var device_id by SampleObjects.device_id
|
||||
var device_date by SampleObjects.device_date
|
||||
}
|
||||
8
src/main/kotlin/database/service/ISampleObjectService.kt
Normal file
8
src/main/kotlin/database/service/ISampleObjectService.kt
Normal file
@@ -0,0 +1,8 @@
|
||||
package database.service
|
||||
|
||||
import java.time.LocalDate
|
||||
import api.ApiObject
|
||||
|
||||
interface ISampleObjectService{
|
||||
fun addOne(apiObject: ApiObject)
|
||||
}
|
||||
18
src/main/kotlin/database/service/SampleObjectService.kt
Normal file
18
src/main/kotlin/database/service/SampleObjectService.kt
Normal file
@@ -0,0 +1,18 @@
|
||||
package database.service
|
||||
|
||||
import database.model.SampleObject
|
||||
import api.ApiObject
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
class SampleObjectService : ISampleObjectService {
|
||||
|
||||
override fun addOne(apiObject: ApiObject) {
|
||||
transaction {
|
||||
SampleObject.new {
|
||||
tag = apiObject.tag
|
||||
device_date = apiObject.device_date
|
||||
device_id = apiObject.device_id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/main/kotlin/di/DatabaseModule.kt
Normal file
9
src/main/kotlin/di/DatabaseModule.kt
Normal file
@@ -0,0 +1,9 @@
|
||||
package di
|
||||
|
||||
import database.service.ISampleObjectService
|
||||
import database.service.SampleObjectService
|
||||
import org.koin.dsl.module
|
||||
|
||||
val databasemodule = module(createdAtStart = true) {
|
||||
single<ISampleObjectService> { SampleObjectService() }
|
||||
}
|
||||
25
src/main/kotlin/mq/ConsumerWrapper.kt
Normal file
25
src/main/kotlin/mq/ConsumerWrapper.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package mq
|
||||
|
||||
import com.rabbitmq.client.BuiltinExchangeType
|
||||
import com.rabbitmq.client.ConnectionFactory
|
||||
import config.EnvConfig
|
||||
|
||||
class ConsumerWrapper {
|
||||
private val envConfig = EnvConfig()
|
||||
|
||||
fun recieve(){
|
||||
val factory = ConnectionFactory()
|
||||
factory.host = envConfig.mqHost
|
||||
factory.username = envConfig.mqUserName
|
||||
factory.password = envConfig.mqPassWord
|
||||
|
||||
val inputConnection = factory.newConnection()
|
||||
val inputChannel = inputConnection.createChannel()
|
||||
|
||||
inputChannel.exchangeDeclare(envConfig.mqExchange, BuiltinExchangeType.FANOUT)
|
||||
val inputQueueName = inputChannel.queueDeclare().queue
|
||||
inputChannel.queueBind(inputQueueName, envConfig.mqExchange, "")
|
||||
|
||||
inputChannel.basicConsume(inputQueueName, true, DatabaseConsumer())
|
||||
}
|
||||
}
|
||||
37
src/main/kotlin/mq/DatabaseConsumer.kt
Normal file
37
src/main/kotlin/mq/DatabaseConsumer.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
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?) {
|
||||
val rawJson = body!!.toString(Charsets.UTF_8)
|
||||
val apiObject = gson.fromJson(rawJson, ApiObject::class.java)
|
||||
resultObjectService.addOne(apiObject)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user