Refactoring of Notification system
This commit is contained in:
@@ -9,6 +9,9 @@ import java.io.IOException
|
||||
class Mailer(
|
||||
private val key: String
|
||||
) {
|
||||
/**
|
||||
* Send email via Sendgrid
|
||||
*/
|
||||
fun sendEmail(action: () -> Mail): Boolean {
|
||||
val mail = action()
|
||||
|
||||
|
||||
10
src/main/kotlin/fr/dcproject/common/utils/Ktor.kt
Normal file
10
src/main/kotlin/fr/dcproject/common/utils/Ktor.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
package fr.dcproject.common.utils
|
||||
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.application.ApplicationStopped
|
||||
|
||||
fun Application.onApplicationStopped(callback: Application.() -> Unit) {
|
||||
environment.monitor.subscribe(ApplicationStopped) {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
30
src/main/kotlin/fr/dcproject/common/utils/RabbitConsume.kt
Normal file
30
src/main/kotlin/fr/dcproject/common/utils/RabbitConsume.kt
Normal file
@@ -0,0 +1,30 @@
|
||||
package fr.dcproject.common.utils
|
||||
|
||||
import com.rabbitmq.client.AMQP
|
||||
import com.rabbitmq.client.Channel
|
||||
import com.rabbitmq.client.Consumer
|
||||
import com.rabbitmq.client.DefaultConsumer
|
||||
import com.rabbitmq.client.Envelope
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.io.IOException
|
||||
|
||||
fun Channel.consumeQueue(queueName: String, callback: DefaultConsumer.(ByteArray) -> Unit) {
|
||||
val consumer: Consumer = object : DefaultConsumer(this) {
|
||||
@Throws(IOException::class)
|
||||
override fun handleDelivery(
|
||||
consumerTag: String,
|
||||
envelope: Envelope,
|
||||
properties: AMQP.BasicProperties,
|
||||
body: ByteArray
|
||||
) = runBlocking {
|
||||
try {
|
||||
callback(body)
|
||||
basicAck(envelope.deliveryTag, false)
|
||||
} catch (e: Throwable) {
|
||||
basicNack(envelope.deliveryTag, false, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Launch Consumer */
|
||||
basicConsume(queueName, false, consumer)
|
||||
}
|
||||
Reference in New Issue
Block a user