Skip to content

Instantly share code, notes, and snippets.

@pmihalcin
Created June 1, 2018 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmihalcin/96a8b0ac8df8ac518127c1ac391a871d to your computer and use it in GitHub Desktop.
Save pmihalcin/96a8b0ac8df8ac518127c1ac391a871d to your computer and use it in GitHub Desktop.
Amqp utils kung fu
package net.homecredit.mer.web
import org.springframework.amqp.rabbit.connection.Connection
import org.springframework.amqp.rabbit.connection.ConnectionFactory
import static java.lang.System.currentTimeMillis
import static net.homecredit.mer.web.WaitUtils.doWait
import static org.junit.Assert.fail
class AmqpUtils {
static void awaitQueueNonEmpty(ConnectionFactory connectionFactory, String queueName, long maxTimeoutMs, long timeoutMs) {
long start = currentTimeMillis()
def connection = connectionFactory.createConnection()
while (!isQueueNonEmpty(connection, queueName) && currentTimeMillis() - start < maxTimeoutMs) {
doWait(timeoutMs)
}
if (!isQueueNonEmpty(connection, queueName)) {
fail("Queue empty: $queueName. Total waiting time was ~ ${(currentTimeMillis() - start) / 1000} seconds")
}
connection.close()
}
static boolean isQueueNonEmpty(Connection connection, String queueName) {
def channel
try {
channel = connection.createChannel(false)
channel.messageCount(queueName) > 0
} finally {
if (channel != null) {
channel.close()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment