public RawOutboundEnvelope( IEnumerable <MessageHeader>?headers, IProducerEndpoint endpoint, IOffset?offset = null, IDictionary <string, string>?additionalLogData = null) : this(null, headers, endpoint, offset, additionalLogData) { }
public async IAsyncEnumerable <IOutboundEnvelope> ProcessMessage(IOutboundEnvelope envelope) { Check.NotNull(envelope, nameof(envelope)); if (envelope.RawMessage == null) { throw new InvalidOperationException("RawMessage is null"); } var settings = envelope.Endpoint.Chunk; var chunkSize = settings?.Size ?? int.MaxValue; var bufferArray = ArrayPool <byte> .Shared.Rent(chunkSize); var bufferMemory = bufferArray.AsMemory(0, chunkSize); var chunksCount = envelope.RawMessage.CanSeek ? (int?)Math.Ceiling(envelope.RawMessage.Length / (double)chunkSize) : null; IOffset?firstChunkOffset = null; int chunkIndex = 0; var readBytesCount = await envelope.RawMessage.ReadAsync(bufferMemory).ConfigureAwait(false); while (readBytesCount > 0) { var chunkEnvelope = CreateChunkEnvelope( chunkIndex, chunksCount, bufferMemory.Slice(0, readBytesCount).ToArray(), envelope); if (chunkIndex > 0) { chunkEnvelope.Headers.AddOrReplace(DefaultMessageHeaders.FirstChunkOffset, firstChunkOffset?.Value); } // Read the next chunk and check if we were at the end of the stream (to not rely on Length property) readBytesCount = await envelope.RawMessage.ReadAsync(bufferMemory).ConfigureAwait(false); if (readBytesCount == 0) { chunkEnvelope.Headers.AddOrReplace(DefaultMessageHeaders.IsLastChunk, true.ToString()); } yield return(chunkEnvelope); // Read and store the offset of the first chunk, after it has been produced (after yield return) if (chunkIndex == 0) { firstChunkOffset = chunkEnvelope.Offset; } chunkIndex++; } }
public RawOutboundEnvelope( Stream?rawMessage, IEnumerable <MessageHeader>?headers, IProducerEndpoint endpoint, IOffset?offset = null, IDictionary <string, string>?additionalLogData = null) : base(rawMessage, headers, endpoint, additionalLogData) { Offset = offset; }
public void RecordConsumedMessage(IOffset?offset) { if (Status == ConsumerStatus.Connected) { ChangeStatus(ConsumerStatus.Consuming); } ConsumedMessagesCount++; LatestConsumedMessageTimestamp = DateTime.Now; LatestConsumedMessageOffset = offset; }
public int CompareTo(IOffset?other) { if (other == null) { return(0); } long thisValue = long.Parse(Value, CultureInfo.InvariantCulture); long otherValue = long.Parse(other.Value, CultureInfo.InvariantCulture); return(thisValue.CompareTo(otherValue)); }
/// <inheritdoc cref="IComparable{T}.CompareTo" /> public int CompareTo(IOffset?other) { if (ReferenceEquals(this, other)) { return(0); } if (other is null) { return(1); } return(other is KafkaOffset otherKafkaOffset ? CompareTo(otherKafkaOffset) : throw new ArgumentException($"Object must be of type {nameof(KafkaOffset)}")); }
public bool Equals(IOffset?other) { if (ReferenceEquals(this, other)) { return(true); } if (ReferenceEquals(other, null)) { return(false); } if (!(other is TestOffset otherRabbitOffset)) { return(false); } return(Key == otherRabbitOffset.Key && Value == otherRabbitOffset.Value); }
/// <inheritdoc cref="IEquatable{T}.Equals(T)" /> public bool Equals(IOffset?other) { if (ReferenceEquals(this, other)) { return(true); } if (ReferenceEquals(other, null)) { return(false); } if (!(other is RabbitOffset otherRabbitOffset)) { return(false); } return(ConsumerTag == otherRabbitOffset.ConsumerTag && DeliveryTag == otherRabbitOffset.DeliveryTag); }
public async Task TestHandleMessage( object message, MessageHeaderCollection?headers, IOffset?offset = null, IMessageSerializer?serializer = null) { if (serializer == null) { serializer = new JsonMessageSerializer(); } headers ??= new MessageHeaderCollection(); var stream = await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty); var buffer = await stream.ReadAllAsync(); await TestHandleMessage(buffer, headers, offset); }
/// <inheritdoc cref="IEquatable{T}.Equals(T)" /> public bool Equals(IOffset?other) { if (ReferenceEquals(this, other)) { return(true); } if (ReferenceEquals(other, null)) { return(false); } if (!(other is KafkaOffset otherKafkaOffset)) { return(false); } return(Topic == otherKafkaOffset.Topic && Partition == otherKafkaOffset.Partition && Offset == otherKafkaOffset.Offset); }
public OutboundEnvelope( object?message, IEnumerable <MessageHeader>?headers, IProducerEndpoint endpoint, bool autoUnwrap = false, IOffset?offset = null) : base(headers, endpoint, offset) { _message = message; if (Message is byte[] rawMessage) { RawMessage = new MemoryStream(rawMessage); } if (Message is Stream stream) { RawMessage = stream; } AutoUnwrap = autoUnwrap; }
public async Task TestHandleMessage(byte[]?rawMessage, MessageHeaderCollection headers, IOffset?offset = null) { if (!Broker.IsConnected) { throw new InvalidOperationException("The broker is not connected."); } if (!IsConnected) { throw new InvalidOperationException("The consumer is not ready."); } await HandleMessageAsync( rawMessage, headers, "test-topic", offset ?? new TestOffset(), null); }
public Task TestConsume( byte[]?rawMessage, IEnumerable <MessageHeader>?headers = null, IOffset?offset = null) => TestHandleMessage(rawMessage, new MessageHeaderCollection(headers), offset);
public Task TestHandleMessage( object message, IEnumerable <MessageHeader>?headers = null, IOffset?offset = null, IMessageSerializer?serializer = null) => TestHandleMessage(message, new MessageHeaderCollection(headers), offset, serializer);
public bool Equals(IOffset?other) => false;