/// <summary> /// Gets the latest offset for a Topic partition. /// </summary> /// <param name="router">Router used to derive latest offset.</param> /// <param name="topic">Topic to determine latest offset for.</param> /// <param name="partitionID">Topic partition to determine latest offset for.</param> /// <returns>The latest offset for a partition in a topic.</returns> public static long LatestOffset(this BrokerRouter router, string topic, int partitionID = 0) { long offset = 0; FetchResponse response = router.Fetch(topic, partitionID); if ((object)response != null) { offset = response.HighWaterMark; } return(offset); }
private static Message ReadMessage(BrokerRouter router, string topic, int messageIndex, int maxBytes = 32768) { Message message = null; long offset = router.LatestOffset(topic) - (2 - messageIndex); List <Message> messages = router.Fetch(topic, 0, offset, maxBytes)?.Messages; if ((messages?.Count ?? 0) > 0) { message = messages?[0]; } if ((object)message == null) { throw new InvalidOperationException("No Kafka record to consume"); } return(message); }