/// <summary> /// When processing a stream of an unknown size, it becomes a challenge to know when you have dealt with the /// last object in that stream. This class utilizes the Dispose() method to indicate that stream processing /// has completed. This is necessary in order to mark the last message with the isLast header flag /// set to true. The trick here is to ensure that the streamBuffer is not entirely empty when /// dispose gets called. /// </summary> private void FlushStreamBuffer() { while (_queuedEvents.Count > 0) { EventStreamQueueItem eventItem = _queuedEvents.Dequeue(); _eventBus.ProcessMessage(eventItem.MessageContext, () => { _eventBus.EnvelopeBus.Send(eventItem.Envelope); }); } }
public void Publish(object evt) { _log.Debug("enter publish to stream"); string sequence = _sequenceId.ToString(); Envelope env = StreamingEnvelopeHelper.BuildStreamingEnvelope(sequence, _position); env.SetMessageTopic(Topic); MessageContext context = new MessageContext(MessageContext.Directions.Out, env, evt); EventStreamQueueItem eventItem = new EventStreamQueueItem(context); _log.Debug("buffering event with sequenceId: " + sequence + ", position: " + _position); _queuedEvents.Enqueue(eventItem); if (_queuedEvents.Count == (_batchLimit)) { _log.Debug("flushing " + _batchLimit + " event(s) to stream."); FlushStreamBuffer(); } _position++; }
public void Publish(object evt) { _log.Debug("enter publish to stream"); string sequence = _sequenceId.ToString(); Envelope env = StreamingEnvelopeHelper.BuildStreamingEnvelope(sequence, _position); env.SetMessageTopic(Topic); MessageContext context = new MessageContext(MessageContext.Directions.Out, env, evt); EventStreamQueueItem eventItem = new EventStreamQueueItem(context); _log.Debug("buffering event with sequenceId: " + sequence + ", position: " + _position); _queuedEvents.Enqueue(eventItem); if (_queuedEvents.Count == (_batchLimit)) { _log.Debug("flushing " + _batchLimit + " event(s) to stream."); FlushStreamBuffer(); } _position++; }