All checks were successful
continuous-integration/drone/push Build is passing
67 lines
2.3 KiB
Kotlin
67 lines
2.3 KiB
Kotlin
package consumer
|
|
|
|
import config.EnvConfig
|
|
import com.google.gson.Gson
|
|
import com.rabbitmq.client.*
|
|
import com.viartemev.thewhiterabbit.channel.confirmChannel
|
|
import com.viartemev.thewhiterabbit.channel.publish
|
|
import com.viartemev.thewhiterabbit.publisher.OutboundMessage
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.GlobalScope
|
|
import kotlinx.coroutines.launch
|
|
import model.OutputModel
|
|
import kotlin.random.Random
|
|
|
|
class GlueConsumer() : Consumer {
|
|
val envConfig = EnvConfig()
|
|
val factory = ConnectionFactory()
|
|
|
|
val outputConnection:Connection
|
|
val outputChannel:Channel
|
|
|
|
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 : AMQP.BasicProperties?, body : ByteArray?) {
|
|
val rawJson = body!!.toString()
|
|
println(rawJson)
|
|
val probability = Random.nextDouble(0.0,1.0)
|
|
val outputObject = OutputModel(tag=rawJson, probability=probability)
|
|
GlobalScope.launch(Dispatchers.Default) {
|
|
outputConnection.confirmChannel {
|
|
publish {
|
|
publishWithConfirm(
|
|
OutboundMessage(
|
|
envConfig.mqOutputExchange,
|
|
"",
|
|
MessageProperties.PERSISTENT_BASIC,
|
|
gson.toJson(outputObject).toString()
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
override fun handleShutdownSignal(p0 : String?, p1 : ShutdownSignalException?) {
|
|
println("got shutdown signal")
|
|
}
|
|
|
|
init {
|
|
factory.host = envConfig.mqHost
|
|
factory.username = envConfig.mqUserName
|
|
factory.password = envConfig.mqPassWord
|
|
outputConnection=factory.newConnection()
|
|
outputChannel=outputConnection.createChannel()
|
|
outputChannel.exchangeDeclare(envConfig.mqOutputExchange, BuiltinExchangeType.FANOUT)
|
|
}
|
|
} |