Created
June 1, 2018 18:09
-
-
Save pmihalcin/96a8b0ac8df8ac518127c1ac391a871d to your computer and use it in GitHub Desktop.
Amqp utils kung fu
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
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