Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefetchSize to a channel function #98

Merged
merged 1 commit into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import com.rabbitmq.client.Channel
import com.rabbitmq.client.Connection
import com.viartemev.thewhiterabbit.consumer.ConfirmConsumer

fun Channel.consumer(queue: String) = ConfirmConsumer(this, queue)
fun Channel.consumer(queue: String, prefetchSize: Int) = ConfirmConsumer(this, queue, prefetchSize)

suspend fun Connection.channel(block: suspend Channel.() -> Unit): Channel {
val channel = this.createChannel()
channel.use { block(it) }
return channel
}

suspend fun Channel.consume(queue: String, block: suspend ConfirmConsumer.() -> Unit) {
val consumer = this.consumer(queue)
suspend fun Channel.consume(queue: String, prefetchSize: Int = 0, block: suspend ConfirmConsumer.() -> Unit) {
val consumer = this.consumer(queue, prefetchSize)
try {
block(consumer)
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlinx.coroutines.channels.Channel as KChannel

private val logger = KotlinLogging.logger {}

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

Expand All @@ -26,11 +26,11 @@ class ConfirmConsumer internal constructor(private val amqpChannel: Channel, amq
try {
deliveries.sendBlocking(message)
} catch (e: Exception) {
logger.info { "Consumer $consumerTag has been cancelled" }
logger.debug { "Can't send a message. Consumer $consumerTag has been cancelled" }
}
},
{ consumerTag ->
logger.info { "Consumer $consumerTag has been cancelled" }
logger.debug { "Consumer $consumerTag has been cancelled" }
deliveries.cancel()
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ConfirmConsumerTest {
runBlocking {
connection.channel {
declareQueue(QueueSpecification(QUEUE_NAME))
consume(QUEUE_NAME) {
consume(QUEUE_NAME, 2) {
for (i in 1..3) consumeWithConfirm({ handleDelivery(it) })
}
}
Expand Down