-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Artemyev Vyacheslav
authored and
Artemyev Vyacheslav
committed
Nov 7, 2023
1 parent
d20fddb
commit 4ba6ab0
Showing
5 changed files
with
128 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/com/viartemev/thewhiterabbit/consumer/flow/ConsumerFlow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.viartemev.thewhiterabbit.consumer.flow | ||
|
||
import com.rabbitmq.client.Channel | ||
import com.rabbitmq.client.Delivery | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.channels.trySendBlocking | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import mu.KotlinLogging | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
class ConsumerFlow( | ||
private val amqpChannel: Channel, private val amqpQueue: String, private val prefetchSize: Int | ||
) { | ||
|
||
//TODO exception handling | ||
suspend fun consumerAutoAckFlow(): Flow<Delivery> = callbackFlow { | ||
amqpChannel.basicQos(prefetchSize, false) | ||
val tag = amqpChannel.basicConsume(amqpQueue, true, { consumerTag, message -> | ||
trySendBlocking(message) | ||
}, { consumerTag -> | ||
channel.close() | ||
}) | ||
awaitClose { | ||
amqpChannel.basicCancel(tag) | ||
amqpChannel.close() | ||
} | ||
} | ||
|
||
//TODO exception handling | ||
suspend fun consumerConfirmAckFlow(): Flow<Delivery> = callbackFlow { | ||
amqpChannel.basicQos(prefetchSize, false) | ||
val tag = amqpChannel.basicConsume(amqpQueue, false, { consumerTag, message -> | ||
trySendBlocking(message) | ||
amqpChannel.basicAck(message.envelope.deliveryTag, false) | ||
}, { consumerTag -> | ||
channel.close() | ||
}) | ||
awaitClose { | ||
amqpChannel.basicCancel(tag) | ||
amqpChannel.close() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/kotlin/com/viartemev/thewhiterabbit/consumer/flow/ConsumerFlowTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.viartemev.thewhiterabbit.consumer.flow | ||
|
||
import com.viartemev.thewhiterabbit.AbstractTestContainersTest | ||
import com.viartemev.thewhiterabbit.channel.confirmChannel | ||
import com.viartemev.thewhiterabbit.channel.publish | ||
import com.viartemev.thewhiterabbit.queue.QueueSpecification | ||
import com.viartemev.thewhiterabbit.queue.declareQueue | ||
import com.viartemev.thewhiterabbit.utils.createMessage | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.flow.take | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.jupiter.api.Test | ||
|
||
class ConsumerFlowTest : AbstractTestContainersTest() { | ||
private val QUEUE_NAME = "test_queue" | ||
|
||
@Test | ||
fun testAutoAckFlow(): Unit = runBlocking { | ||
factory.newConnection().use { connection -> | ||
connection.confirmChannel { | ||
declareQueue(QueueSpecification(QUEUE_NAME)) | ||
publish { | ||
(1..10).map { createMessage(queue = QUEUE_NAME, body = "1") } | ||
.map { m -> async { publishWithConfirm(m) } }.awaitAll() | ||
} | ||
ConsumerFlow(this, QUEUE_NAME, 2) | ||
.consumerAutoAckFlow() | ||
.take(10) | ||
.collect { delivery -> println(String(delivery.body)) } | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun testConfirmAckFlow(): Unit = runBlocking { | ||
factory.newConnection().use { connection -> | ||
connection.confirmChannel { | ||
declareQueue(QueueSpecification(QUEUE_NAME)) | ||
publish { | ||
(1..10).map { createMessage(queue = QUEUE_NAME, body = "1") } | ||
.map { m -> async { publishWithConfirm(m) } }.awaitAll() | ||
} | ||
ConsumerFlow(this, QUEUE_NAME, 2) | ||
.consumerConfirmAckFlow() | ||
.take(10) | ||
.collect { delivery -> println(String(delivery.body)) } | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<logger name="com.viartemev" level="TRACE"/> | ||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
</configuration> |