private void ReadFromQueue(object o) { _queueStats.Start(); Thread.BeginThreadAffinity(); // ensure we are not switching between OS threads. Required at least for v8. const int spinmax = 5000; const int sleepmax = 500; var iterationsCount = 0; while (!_stop) { Message msg = null; try { if (!_queue.TryDequeue(out msg)) { _queueStats.EnterIdle(); iterationsCount += 1; if (iterationsCount < spinmax) { //do nothing... spin } else if (iterationsCount < sleepmax) { Thread.Sleep(1); } else { _starving = true; _msgAddEvent.WaitOne(100); _starving = false; } } else { _queueStats.EnterBusy(); iterationsCount = 0; var cnt = _queue.Count; _queueStats.ProcessingStarted(msg.GetType(), cnt); if (_watchSlowMsg) { var start = DateTime.UtcNow; _consumer.Handle(msg); var elapsed = DateTime.UtcNow - start; if (elapsed > _slowMsgThreshold) { Log.Trace("SLOW QUEUE MSG [{0}]: {1} - {2}ms. Q: {3}/{4}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count); if (elapsed > QueuedHandler.VerySlowMsgThreshold && !(msg is SystemMessage.SystemInit)) { Log.Error("---!!! VERY SLOW QUEUE MSG [{0}]: {1} - {2}ms. Q: {3}/{4}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count); } } } else { _consumer.Handle(msg); } _queueStats.ProcessingEnded(1); } } catch (Exception ex) { Log.ErrorException(ex, "Error while processing message {0} in queued handler '{1}'.", msg, Name); } } _queueStats.Stop(); _stopped.Set(); _queueMonitor.Unregister(this); Thread.EndThreadAffinity(); }
public void Handle(TInput message) { _handler.Handle((TOutput)message); // will throw if message type is wrong }
public void Handle(T message) { _logger.Debug($"Queue -> {_handler.GetType().Name}\nnote right: {message.GetType().Name}"); _handler.Handle(message); }
private void ReadFromQueue(object o) { try{ if (Interlocked.CompareExchange(ref _queueStatsState, 1, 0) == 0) { _queueStats.Start(); } bool proceed = true; while (proceed) { _stopped.Reset(); _queueStats.EnterBusy(); Message msg; while (!_stop && _queue.TryDequeue(out msg)) { #if DEBUG _queueStats.Dequeued(msg); #endif try { var queueCnt = _queue.Count; _queueStats.ProcessingStarted(msg.GetType(), queueCnt); if (_watchSlowMsg) { var start = DateTime.UtcNow; _consumer.Handle(msg); var elapsed = DateTime.UtcNow - start; if (elapsed > _slowMsgThreshold) { Log.Trace("SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevQueueCount}/{curQueueCount}.", _queueStats.Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, queueCnt, _queue.Count); if (elapsed > QueuedHandler.VerySlowMsgThreshold && !(msg is SystemMessage.SystemInit)) { Log.Error("---!!! VERY SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevQueueCount}/{curQueueCount}.", _queueStats.Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, queueCnt, _queue.Count); } } } else { _consumer.Handle(msg); } _queueStats.ProcessingEnded(1); } catch (Exception ex) { Log.ErrorException(ex, "Error while processing message {message} in queued handler '{queue}'.", msg, _queueStats.Name); #if DEBUG throw; #endif } } _queueStats.EnterIdle(); Interlocked.CompareExchange(ref _isRunning, 0, 1); if (_stop) { TryStopQueueStats(); } _stopped.Set(); // try to reacquire lock if needed proceed = !_stop && _queue.Count > 0 && Interlocked.CompareExchange(ref _isRunning, 1, 0) == 0; } } catch (Exception ex) { _tcs.TrySetException(ex); throw; } }
public void Handle(ProjectionCoreServiceMessage.CommittedEventDistributed message) { if (message.Data == null) { throw new NotSupportedException(); } // NOTE: we may receive here messages from heading event distribution point // and they may not pass out source filter. Discard them first var roundedProgress = (float)Math.Round(message.Progress, 2); bool progressChanged = _progress != roundedProgress; _progress = roundedProgress; if (!_eventFilter.PassesSource(message.ResolvedLinkTo, message.PositionStreamId)) { if (progressChanged) { _progressHandler.Handle( new ProjectionSubscriptionMessage.ProgressChanged( _projectionCorrelationId, _positionTracker.LastTag, _progress, _subscriptionMessageSequenceNumber++)); } return; } // NOTE: after joining heading distribution point it delivers all cached events to the subscription // some of this events we may have already received. The delivered events may have different order // (in case of partially ordered cases multi-stream reader etc). We discard all the messages that are not // after the last available checkpoint tag if (!_positionTagger.IsMessageAfterCheckpointTag(_positionTracker.LastTag, message)) { _logger.Trace( "Skipping replayed event {0}@{1} at position {2}. the last processed event checkpoint tag is: {3}", message.PositionSequenceNumber, message.PositionStreamId, message.Position, _positionTracker.LastTag); return; } var eventCheckpointTag = _positionTagger.MakeCheckpointTag(_positionTracker.LastTag, message); if (eventCheckpointTag <= _positionTracker.LastTag) { throw new Exception( string.Format( "Invalid checkpoint tag was built. Tag '{0}' must be greater than '{1}'", eventCheckpointTag, _positionTracker.LastTag)); } _positionTracker.UpdateByCheckpointTagForward(eventCheckpointTag); if (_eventFilter.Passes(message.ResolvedLinkTo, message.PositionStreamId, message.Data.EventType)) { _lastPassedOrCheckpointedEventPosition = message.Position; var convertedMessage = ProjectionSubscriptionMessage.CommittedEventReceived.FromCommittedEventDistributed( message, eventCheckpointTag, _eventFilter.GetCategory(message.PositionStreamId), _subscriptionMessageSequenceNumber++); _eventHandler.Handle(convertedMessage); } else { if (_checkpointUnhandledBytesThreshold != null && message.Position.CommitPosition - _lastPassedOrCheckpointedEventPosition.CommitPosition > _checkpointUnhandledBytesThreshold) { _lastPassedOrCheckpointedEventPosition = message.Position; _checkpointHandler.Handle( new ProjectionSubscriptionMessage.CheckpointSuggested( _projectionCorrelationId, _positionTracker.LastTag, message.Progress, _subscriptionMessageSequenceNumber++)); } else { if (progressChanged) { _progressHandler.Handle( new ProjectionSubscriptionMessage.ProgressChanged( _projectionCorrelationId, _positionTracker.LastTag, _progress, _subscriptionMessageSequenceNumber++)); } } } }
private void DispatchRecentMessagesTo( IHandle<ReaderSubscriptionMessage.CommittedEventDistributed> subscription, long fromTransactionFilePosition) { foreach (var m in _lastMessages) if (m.Data.Position.CommitPosition >= fromTransactionFilePosition) subscription.Handle(m); }
public override void Handle(BaseDomainEvent domainEvent) { _handler.Handle((T)domainEvent); }
private void ReadFromQueue(object o) { _queueStats.Start(); Thread.BeginThreadAffinity(); // ensure we are not switching between OS threads. Required at least for v8. while (!_stop) { Message msg = null; try { lock (_locker) { while (!_queue.TryDequeue(out msg) && !_stop) { _starving = true; _queueStats.EnterIdle(); Monitor.Wait(_locker, 100); } _starving = false; if (_stop) { break; } } _queueStats.EnterBusy(); var cnt = _queue.Count; _queueStats.ProcessingStarted(msg.GetType(), cnt); if (_watchSlowMsg) { var start = DateTime.UtcNow; _consumer.Handle(msg); var elapsed = DateTime.UtcNow - start; if (elapsed > _slowMsgThreshold) { Log.Trace("SLOW QUEUE MSG [{0}]: {1} - {2}ms. Q: {3}/{4}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count); if (elapsed > QueuedHandler.VerySlowMsgThreshold)// && !(msg is SystemMessage.SystemInit)) { Log.Error("---!!! VERY SLOW QUEUE MSG [{0}]: {1} - {2}ms. Q: {3}/{4}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count); } } } else { _consumer.Handle(msg); } _queueStats.ProcessingEnded(1); } catch (Exception ex) { Log.ErrorException(ex, $"Error while processing message {msg} in queued handler '{Name}'."); } } _queueStats.Stop(); _stopped.Set(); _queueMonitor.Unregister(this); Thread.EndThreadAffinity(); }
public void Handle(T message) { Console.WriteLine($"About to do command: {message.GetType()}"); next.Handle(message); Console.WriteLine($"Finished doing command: {message.GetType()}"); }
public override Task Handle(BaseDomainEvent domainEvent) { return(_handler.Handle((T)domainEvent)); }
public void RegisterEventHandler <TEvent>(IHandle <TEvent> handler) where TEvent : class, IEvent { _routes.Add(typeof(TEvent), message => handler.Handle(message as TEvent)); }
public void RegisterCommandHandler <TCommand>(IHandle <TCommand> handler) where TCommand : class, ICommand { _routes.Add(typeof(TCommand), message => handler.Handle(message as TCommand)); }
public IMessageBus Subscribe <T>(IHandle <T> handler) { _handlers[typeof(T)] = message => handler.Handle((T)message); return(this); }
private void ReadFromQueue(object o) { try { _queueStats.Start(); Thread.BeginThreadAffinity(); // ensure we are not switching between OS threads. Required at least for v8. var batch = new Message[128]; while (!_stop) { Message msg = null; try { QueueBatchDequeueResult dequeueResult; if (_queue.TryDequeue(batch, out dequeueResult) == false) { _starving = true; _queueStats.EnterIdle(); _msgAddEvent.Wait(100); _msgAddEvent.Reset(); _starving = false; } else { var estimatedQueueCount = dequeueResult.EstimateCurrentQueueCount; for (var i = 0; i < dequeueResult.DequeueCount; i++) { try { msg = batch[i]; _queueStats.EnterBusy(); #if DEBUG _queueStats.Dequeued(msg); #endif _queueStats.ProcessingStarted(msg.GetType(), estimatedQueueCount); if (_watchSlowMsg) { var start = DateTime.UtcNow; _consumer.Handle(msg); var elapsed = DateTime.UtcNow - start; if (elapsed > _slowMsgThreshold) { Log.Verbose( "SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevEstimatedQueueCount}/{curEstimatedQueueCount}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, estimatedQueueCount, _queue.EstimageCurrentQueueCount()); if (elapsed > QueuedHandler.VerySlowMsgThreshold && !(msg is SystemMessage.SystemInit)) { Log.Error( "---!!! VERY SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevEstimatedQueueCount}/{curEstimatedQueueCount}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, estimatedQueueCount, _queue.EstimageCurrentQueueCount()); } } } else { _consumer.Handle(msg); } } catch (Exception ex) { Log.Error(ex, "Error while processing message {message} in queued handler '{queue}'.", msg, Name); #if DEBUG throw; #endif } estimatedQueueCount -= 1; _queueStats.ProcessingEnded(1); } } } catch (Exception ex) { Log.Error(ex, "Error while processing message {message} in queued handler '{queue}'.", msg, Name); #if DEBUG throw; #endif } } } catch (Exception ex) { _tcs.TrySetException(ex); throw; } finally { _queueStats.Stop(); _stopped.Set(); _queueMonitor.Unregister(this); Thread.EndThreadAffinity(); } }
public Task Consume(ConsumeContext <TMessage> context) { return(_handler.Handle(context.Message)); }
public void RegisterHandler <TCommand>(IHandle <TCommand> handler) where TCommand : class, ICommand { _routes.Add(typeof(TCommand), command => handler.Handle(command as TCommand)); }
public Task Handle(Message msg) { return(_handler.Handle((T)msg)); }
private void ReadFromQueue(object o) { try { _queueStats.Start(); Thread.BeginThreadAffinity(); // ensure we are not switching between OS threads. Required at least for v8. const int spinmax = 5000; var iterationsCount = 0; while (!_stop) { Message msg = null; try { if (!_queue.TryDequeue(out msg)) { _queueStats.EnterIdle(); iterationsCount += 1; if (iterationsCount < spinmax) { //do nothing... spin } else { Thread.Sleep(1); } } else { _queueStats.EnterBusy(); #if DEBUG _queueStats.Dequeued(msg); #endif var cnt = _queue.Count; _queueStats.ProcessingStarted(msg.GetType(), cnt); if (_watchSlowMsg) { var start = DateTime.UtcNow; _consumer.Handle(msg); var elapsed = DateTime.UtcNow - start; if (elapsed > _slowMsgThreshold) { Log.Debug( "SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevQueueCount}/{curQueueCount}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count); if (elapsed > QueuedHandler.VerySlowMsgThreshold && !(msg is SystemMessage.SystemInit)) { Log.Error( "---!!! VERY SLOW QUEUE MSG [{queue}]: {message} - {elapsed}ms. Q: {prevQueueCount}/{curQueueCount}.", Name, _queueStats.InProgressMessage.Name, (int)elapsed.TotalMilliseconds, cnt, _queue.Count); } } } else { _consumer.Handle(msg); } _queueStats.ProcessingEnded(1); } } catch (Exception ex) { Log.Error(ex, "Error while processing message {message} in queued handler '{queue}'.", msg, Name); #if DEBUG throw; #endif } } } catch (Exception ex) { _tcs.TrySetException(ex); throw; } finally { _queueStats.Stop(); _stopped.Set(); _queueMonitor.Unregister(this); Thread.EndThreadAffinity(); } }
public void Handle(AuthorizationEnvelope <TMessage> envelope) { _authorizer.Authorize(envelope.UserId, envelope.Message); _handler.Handle(envelope.Message); }
public void Handle(TInput message) { _handler.Handle(message); }
public void Handle(SecurityContext <TMessage> context) { _authorizer.Authorize(context.AccountId, context.Message); _handler.Handle(context.Message); }
private void DispatchRecentMessagesTo( IHandle<ProjectionCoreServiceMessage.CommittedEventDistributed> subscription) { foreach (var m in _lastMessages) subscription.Handle(m); }
public void Register <T>(IHandle <T> handler) where T : class { _registeredHandlers.Add(new HandlerRegistration(typeof(T), handler.GetType().AssemblyQualifiedName, msg => handler.Handle(msg as T))); }