From 6a027c9c0abaf8dd46403ed94f4cce3378c0f826 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Thu, 18 Apr 2019 14:57:06 -0700 Subject: amqp_consume_message now has a timeout RabbitMQ::consume() will return an empty string if the call to amqp_consume_message times out --- controller/RabbitMQ.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'controller/RabbitMQ.cpp') diff --git a/controller/RabbitMQ.cpp b/controller/RabbitMQ.cpp index 0b8cec73..eec9745d 100644 --- a/controller/RabbitMQ.cpp +++ b/controller/RabbitMQ.cpp @@ -80,9 +80,18 @@ std::string RabbitMQ::consume() amqp_envelope_t envelope; amqp_maybe_release_buffers(_conn); - res = amqp_consume_message(_conn, &envelope, NULL, 0); + struct timeval timeout; + timeout.tv_sec = 1; + timeout.tv_usec = 0; + + res = amqp_consume_message(_conn, &envelope, &timeout, 0); if (res.reply_type != AMQP_RESPONSE_NORMAL) { - throw std::runtime_error("Error getting message"); + if (res.reply_type == AMQP_RESPONSE_LIBRARY_EXCEPTION && res.library_error == AMQP_STATUS_TIMEOUT) { + // timeout waiting for message. Return empty string + return ""; + } else { + throw std::runtime_error("Error getting message"); + } } std::string msg( -- cgit v1.2.3