Skip to content

Commit 8749661

Browse files
authored
Merge pull request #98 from viartemev/add-prefetchSize-to-func
Add prefetchSize to a channel function
2 parents 1b25630 + 572fc8e commit 8749661

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/main/kotlin/com/viartemev/thewhiterabbit/channel/Channel.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import com.rabbitmq.client.Channel
44
import com.rabbitmq.client.Connection
55
import com.viartemev.thewhiterabbit.consumer.ConfirmConsumer
66

7-
fun Channel.consumer(queue: String) = ConfirmConsumer(this, queue)
7+
fun Channel.consumer(queue: String, prefetchSize: Int) = ConfirmConsumer(this, queue, prefetchSize)
88

99
suspend fun Connection.channel(block: suspend Channel.() -> Unit): Channel {
1010
val channel = this.createChannel()
1111
channel.use { block(it) }
1212
return channel
1313
}
1414

15-
suspend fun Channel.consume(queue: String, block: suspend ConfirmConsumer.() -> Unit) {
16-
val consumer = this.consumer(queue)
15+
suspend fun Channel.consume(queue: String, prefetchSize: Int = 0, block: suspend ConfirmConsumer.() -> Unit) {
16+
val consumer = this.consumer(queue, prefetchSize)
1717
try {
1818
block(consumer)
1919
} finally {

src/main/kotlin/com/viartemev/thewhiterabbit/consumer/ConfirmConsumer.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import kotlinx.coroutines.channels.Channel as KChannel
1515

1616
private val logger = KotlinLogging.logger {}
1717

18-
class ConfirmConsumer internal constructor(private val amqpChannel: Channel, amqpQueue: String, prefetchSize: Int = 0) {
18+
class ConfirmConsumer internal constructor(private val amqpChannel: Channel, amqpQueue: String, prefetchSize: Int) {
1919
private val deliveries = KChannel<Delivery>()
2020
private val consTag: String
2121

@@ -26,11 +26,11 @@ class ConfirmConsumer internal constructor(private val amqpChannel: Channel, amq
2626
try {
2727
deliveries.sendBlocking(message)
2828
} catch (e: Exception) {
29-
logger.info { "Consumer $consumerTag has been cancelled" }
29+
logger.debug { "Can't send a message. Consumer $consumerTag has been cancelled" }
3030
}
3131
},
3232
{ consumerTag ->
33-
logger.info { "Consumer $consumerTag has been cancelled" }
33+
logger.debug { "Consumer $consumerTag has been cancelled" }
3434
deliveries.cancel()
3535
}
3636
)

src/test/kotlin/com/viartemev/thewhiterabbit/consumer/ConfirmConsumerTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ConfirmConsumerTest {
3333
runBlocking {
3434
connection.channel {
3535
declareQueue(QueueSpecification(QUEUE_NAME))
36-
consume(QUEUE_NAME) {
36+
consume(QUEUE_NAME, 2) {
3737
for (i in 1..3) consumeWithConfirm({ handleDelivery(it) })
3838
}
3939
}

0 commit comments

Comments
 (0)