protected bool RegisterIncomingMessage(TMessage message) { try { var resolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); resolver.DefaultMembersSearchFlags = resolver.DefaultMembersSearchFlags | System.Reflection.BindingFlags.NonPublic; using (var db = new DB.DataContext()) { var mess = new DB.MessageQueue() { IdMessageType = IdMessageType, StateType = DB.MessageStateType.NotProcessed, Direction = true, DateCreate = DateTime.Now, MessageInfo = Newtonsoft.Json.JsonConvert.SerializeObject(message, new Newtonsoft.Json.JsonSerializerSettings() { ContractResolver = resolver }), }; db.MessageQueue.Add(mess); db.SaveChanges(); return(true); } } catch (Exception ex) { this.RegisterEvent(Journaling.EventType.Error, "Ошибка регистрации входящего сообщения", null, ex); return(false); } }
protected bool RegisterOutcomingMessage(TMessage message, out MessageInfo <TMessage> messageInfo) { try { var resolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); resolver.DefaultMembersSearchFlags = resolver.DefaultMembersSearchFlags | System.Reflection.BindingFlags.NonPublic; using (var db = new DB.DataContext()) { var mess = new DB.MessageQueue() { IdMessageType = IdMessageType, StateType = DB.MessageStateType.NotProcessed, Direction = false, DateCreate = DateTime.Now, MessageInfo = Newtonsoft.Json.JsonConvert.SerializeObject(message, new Newtonsoft.Json.JsonSerializerSettings() { ContractResolver = resolver }), }; db.MessageQueue.Add(mess); db.SaveChanges(); var taskOutcomingMessages = _taskOutcomingMessages; if (taskOutcomingMessages != null) { AppCore.Get <TaskSchedulingManager>()?.ExecuteTask(taskOutcomingMessages); } messageInfo = new MessageInfo <TMessage>(new IntermediateStateMessage <TMessage>(message, mess)); return(true); } } catch (Exception ex) { this.RegisterEvent(Journaling.EventType.Error, "Ошибка регистрации исходящего сообщения", null, ex); messageInfo = null; return(false); } }
void IMessagingServiceInternal.PrepareIncomingReceive(TimeSpan executeInterval) { if (AppCore.GetState() != CoreComponentState.Started) { return; } var type = GetType(); if (!_executingFlags.TryLock(TasksIncomingReceive)) { return; } int messagesReceived = 0; try { using (var db = new DB.DataContext()) { var components = GetComponents(). OfType <IncomingMessageReceiver <TMessage> >(). Select(x => new { Component = x, IdTypeComponent = ItemTypeFactory.GetItemType(x.GetType())?.IdItemType }). OrderBy(x => ((IPoolObjectOrdered)x.Component).OrderInPool). ToList(); var timeEnd = DateTimeOffset.Now.Add(executeInterval); using (var scope = db.CreateScope(TransactionScopeOption.Suppress)) // Здесь Suppress вместо RequiresNew, т.к. весь процесс отправки занимает много времени и блокировать таблицу нельзя. { foreach (var componentInfo in components) { try { var messages = componentInfo.Component.OnReceive(this); if (messages != null && messages.Count > 0) { int countAdded = 0; foreach (var message in messages) { if (message == null) { continue; } var stateType = DB.MessageStateType.NotProcessed; if (message.IsComplete) { stateType = DB.MessageStateType.Complete; } if (message.IsError) { stateType = DB.MessageStateType.Error; } var mess = new DB.MessageQueue() { IdMessageType = IdMessageType, Direction = true, State = message.State, StateType = stateType, DateCreate = DateTime.Now, DateDelayed = message.DateDelayed, MessageInfo = Newtonsoft.Json.JsonConvert.SerializeObject(message.Message), }; db.MessageQueue.Add(mess); countAdded++; messagesReceived++; if (countAdded >= 50) { db.SaveChanges(); countAdded = 0; } } db.SaveChanges(); } } catch (Exception ex) { this.RegisterServiceEvent(Journaling.EventType.Error, $"Ошибка вызова '{nameof(componentInfo.Component.OnReceive)}'", $"Ошибка вызова '{nameof(componentInfo.Component.OnReceive)}' для компонента '{componentInfo?.Component?.GetType()?.FullName}'.", ex); continue; } try { while (true) { var message = componentInfo.Component.OnBeginReceive(this); if (message == null) { break; } DB.MessageQueue queueMessage = null; var queueState = DB.MessageStateType.NotProcessed; if (message.IsComplete) { queueState = DB.MessageStateType.Complete; } if (message.IsError) { queueState = DB.MessageStateType.Error; } try { var mess = new DB.MessageQueue() { IdMessageType = IdMessageType, Direction = true, State = message.State, StateType = DB.MessageStateType.IntermediateAdded, DateCreate = DateTime.Now, DateDelayed = message.DateDelayed, MessageInfo = Newtonsoft.Json.JsonConvert.SerializeObject(message.Message), }; db.MessageQueue.Add(mess); db.SaveChanges(); queueMessage = mess; } catch (Exception ex) { this.RegisterServiceEvent(Journaling.EventType.Error, $"Ошибка регистрации сообщения после '{nameof(componentInfo.Component.OnBeginReceive)}'", $"Ошибка регистрации сообщения после вызова '{nameof(componentInfo.Component.OnBeginReceive)}' для компонента '{componentInfo?.Component?.GetType()?.FullName}'.", ex); try { componentInfo.Component.OnEndReceive(false, message, this); } catch (Exception ex2) { this.RegisterServiceEvent(Journaling.EventType.Error, $"Ошибка вызова '{nameof(componentInfo.Component.OnBeginReceive)}'", $"Ошибка вызова '{nameof(componentInfo.Component.OnBeginReceive)}' для компонента '{componentInfo?.Component?.GetType()?.FullName}' после ошибки регистрации сообщения.", ex2); } continue; } try { var endReceiveResult = componentInfo.Component.OnEndReceive(true, message, this); if (endReceiveResult) { queueMessage.StateType = queueState; } else { db.MessageQueue.Remove(queueMessage); } db.SaveChanges(); } catch (Exception ex) { this.RegisterServiceEvent(Journaling.EventType.Error, $"Ошибка вызова '{nameof(componentInfo.Component.OnBeginReceive)}'", $"Ошибка вызова '{nameof(componentInfo.Component.OnBeginReceive)}' для компонента '{componentInfo?.Component?.GetType()?.FullName}' после успешной регистрации сообщения.", ex); } if (DateTimeOffset.Now >= timeEnd) { break; } } } catch (Exception ex) { this.RegisterServiceEvent(Journaling.EventType.Error, $"Ошибка вызова '{nameof(componentInfo.Component.OnBeginReceive)}'", $"Ошибка вызова '{nameof(componentInfo.Component.OnBeginReceive)}' для компонента '{componentInfo?.Component?.GetType()?.FullName}'.", ex); continue; } } db.SaveChanges(); scope.Complete(); } } if (messagesReceived > 0) { this.RegisterServiceState(ServiceStatus.RunningIdeal, $"Сообщений получено - {messagesReceived}."); } var service = AppCore.Get <Monitor>().GetService(ServiceID); if (service != null && (DateTime.Now - service.LastDateEvent).TotalHours >= 1) { this.RegisterServiceState(ServiceStatus.RunningIdeal, $"Сообщений нет, сервис работает без ошибок."); } } catch (Exception ex) { this.RegisterServiceState(ServiceStatus.RunningWithErrors, $"Сообщений получено - {messagesReceived}.", ex); } finally { _executingFlags.ReleaseLock(TasksIncomingReceive); } }
internal IntermediateStateMessage(TMessageType message, DB.MessageQueue messageSource) { Message = message; MessageSource = messageSource; }