/// <summary> /// Get the next message off the Redis list, within a timeout /// </summary> /// <param name="timeoutInMilliseconds">The period to await a message</param> /// <returns>The message read from the list</returns> public Message Receive(int timeoutInMilliseconds) { _logger.Value.DebugFormat("RedisMessageConsumer: Preparing to retrieve next message from queue {0} with routing key {1} via exchange {2} on connection {3}", _queueName, _topic); var message = new Message(); IRedisClient client = null; try { client = GetClient(); EnsureConnection(client); (string msgId, string rawMsg)redisMessage = ReadMessage(client, timeoutInMilliseconds); message = new RedisMessageCreator().CreateMessage(redisMessage.rawMsg); _inflight.Add(message.Id, redisMessage.msgId); } catch (TimeoutException te) { _logger.Value.ErrorFormat("Could not connect to Redis client within {0} milliseconds", timeoutInMilliseconds.ToString()); throw new ChannelFailureException( string.Format("Could not connect to Redis client within {0} milliseconds", timeoutInMilliseconds.ToString()), te ); } catch (RedisException re) { _logger.Value.ErrorFormat($"Could not connect to Redis: {re.Message}"); throw new ChannelFailureException(string.Format("Could not connect to Redis client - see inner exception for details"), re); } finally { client?.Dispose(); } return(message); }
/// <summary> /// Get the next message off the Redis list, within a timeout /// </summary> /// <param name="timeoutInMilliseconds">The period to await a message</param> /// <returns>The message read from the list</returns> public Message[] Receive(int timeoutInMilliseconds) { _logger.Value.DebugFormat("RedisMessageConsumer: Preparing to retrieve next message from queue {0} with routing key {1} via exchange {2} on connection {3}", _queueName, Topic); if (_inflight.Any()) { _logger.Value.ErrorFormat("RedisMessageConsumer: Preparing to retrieve next message from queue {0}, but have unacked or not rejected message ", _queueName); throw new ChannelFailureException(string.Format("Unacked message still in flight with id: {0}", _inflight.Keys.First().ToString())); } var message = new Message(); IRedisClient client = null; try { client = GetClient(); EnsureConnection(client); (string msgId, string rawMsg)redisMessage = ReadMessage(client, timeoutInMilliseconds); message = new RedisMessageCreator().CreateMessage(redisMessage.rawMsg); if (message.Header.MessageType != MessageType.MT_NONE && message.Header.MessageType != MessageType.MT_UNACCEPTABLE) { _inflight.Add(message.Id, redisMessage.msgId); } } catch (TimeoutException te) { _logger.Value.ErrorFormat("Could not connect to Redis client within {0} milliseconds", timeoutInMilliseconds.ToString()); throw new ChannelFailureException( string.Format("Could not connect to Redis client within {0} milliseconds", timeoutInMilliseconds.ToString()), te ); } catch (RedisException re) { _logger.Value.ErrorFormat($"Could not connect to Redis: {re.Message}"); throw new ChannelFailureException(string.Format("Could not connect to Redis client - see inner exception for details"), re); } finally { client?.Dispose(); } return(new Message[] { message }); }