/// <summary> /// Получить сообщение по его идентификатору. /// </summary> /// <param name="id">Идентификатор сообщения.</param> /// <returns>Сообщение, или <c>null</c>, если не найдено сообщение, соответствующее идентификатору.</returns> public virtual Message ReadMessage(string id) { var msg = new Message(); msg.SetExistObjectPrimaryKey(new KeyGuid(id)); try { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); _dataService.LoadObject(Message.Views.MessageEditView, msg); stopwatch.Stop(); long time = stopwatch.ElapsedMilliseconds; _statistics.NotifyAvgTimeSql(null, (int)time, "BaseSendingManager.ReadMessage() load message."); } catch (CantFindDataObjectException) { return(null); } _logger.LogOutgoingMessage(msg); _statistics.NotifyMessageSent(_subscriptionsManager.GetSubscriptions(msg.Recipient.ID).Single(s => s.MessageType.ID == msg.MessageType.ID)); return(msg); }
/// <summary> /// Попытаться добавить в очередь для отправки сообщение. /// </summary> /// <param name="message">Сообщение, которое нужно отправить.</param> /// <returns>Было ли сообщение добавлено в очередь.</returns> private bool TryEnqueue(Message message) { Subscription subscription = _subscriptionsManager .GetSubscriptions(message.Recipient.ID) .FirstOrDefault(x => x.MessageType.ID == message.MessageType.ID); if (subscription != null && _sendingTasksCount < MaxTasks) { message.IsSending = true; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); _dataService.UpdateObject(message); stopwatch.Stop(); long time = stopwatch.ElapsedMilliseconds; _statisticsService.NotifyAvgTimeSql(subscription, (int)time, "OptimizedSendingManager.TryEnqueue() update message."); Interlocked.Increment(ref _sendingTasksCount); _statisticsService.NotifyIncConnectionCount(subscription); Task <bool> .Factory.StartNew(SendMessage, new SendingTaskParam { Message = message, Subscription = subscription }, TaskCreationOptions.PreferFairness) .ContinueWith(SendingTaskContinuation, message); return(true); } return(false); }
/// <summary> /// Try to start sending a message. /// </summary> /// <param name="message">Message to send.</param> /// <returns><c>true</c> if sending was started, else <c>false</c>.</returns> private bool TryEnqueue(Message message) { Subscription subscription = _subscriptionsManager.GetSubscriptions(message.Recipient.ID).First(x => x.MessageType.ID == message.MessageType.ID); Guid clientId = (KeyGuid)subscription.Client.__PrimaryKey; int connectionsLimit = subscription.Client.ConnectionsLimit ?? DefaultConnectionsLimit; bool canSend = false; lock (_lock) { if (_clientConnections[clientId] < connectionsLimit && _sendingTasksCount < MaxTasks) { _clientConnections[clientId]++; _sendingTasksCount++; canSend = true; } } if (canSend) { message.IsSending = true; Stopwatch stopwatch = Stopwatch.StartNew(); _dataService.UpdateObject(message); stopwatch.Stop(); _statisticsService.NotifyAvgTimeSql(subscription, (int)stopwatch.ElapsedMilliseconds, "PrioritySendingManager.TryEnqueue(): Update message sending status."); if (EnableOnlineState) { _statisticsService.NotifyIncConnectionCount(subscription, message); } else { _statisticsService.NotifyIncConnectionCount(subscription); } Task <bool> .Factory.StartNew( SendMessage, new SendingTaskParam() { Message = message, Subscription = subscription }, TaskCreationOptions.PreferFairness) .ContinueWith(SendingTaskContinuation, message); } return(canSend); }
private List <SubscriptionQueueEntry> GetWatchedQueues() { var queueGetTasks = new List <Task <SubscriptionQueueEntry> >(); foreach (Subscription s in _esbSubscriptionsManager.GetSubscriptions()) { var name = _namingManager.GetClientQueueName(s.Client.ID, s.MessageType.ID); queueGetTasks.Add(_managementClient.GetQueueAsync(name, Vhost).ContinueWith(x => new SubscriptionQueueEntry(s, x.Result))); } var result = queueGetTasks.Select(x => x.Result).ToList(); return(result); }
private async Task ProcessEvent(MqttApplicationMessageReceivedEventArgs eventArgs) { var message = eventArgs.ApplicationMessage; var subscriptions = _subsManager.GetSubscriptions(message.Topic); if (subscriptions.IsNullOrEmpty()) { _logger.LogWarning($"No subscription for Mqtt topic: \"{message.Topic}\""); } else { foreach (var subscription in subscriptions) { using var scope = _scopeFactory.CreateScope(); await Task.Yield(); await _consumeMethodInvoker.InvokeAsync(scope.ServiceProvider, subscription, eventArgs); } } }