private void GenericSubscriptionQueue_MessageReceived (RabbitMQ.Client.IBasicConsumer sender, RabbitMQ.Client.Events.BasicDeliverEventArgs args) { RabbitMQ.Client.Events.EventingBasicConsumer consumer = (RabbitMQ.Client.Events.EventingBasicConsumer)sender; try { // don't reprocess items that have been processed if (genericSubscriptionQueue.GetLastProcessedUndelivered() != args.DeliveryTag) { BaseNotification notification = NotificationExtension.Deserialize(Encoding.ASCII.GetString(args.Body)); eventLogRepository.WriteInformationLog("Processing notification type: {NotificationType}. Data: {QueueMessage}".Inject(new { QueueMessage = notification.ToJson(), NotificationType = notification.NotificationType.ToString() })); var handler = notificationHandlerFactory(notification.NotificationType); // autofac will get the right handler handler.ProcessNotification(notification); // Always clear the context at the end of a transaction _uow.ClearContext(); } genericSubscriptionQueue.Ack(consumer, args.DeliveryTag); } catch (QueueDataError <string> serializationEx) { eventLogRepository.WriteErrorLog("Serializing problem with notification.", serializationEx); } catch (QueueDataError <BaseNotification> notificationEx) { eventLogRepository.WriteErrorLog("Error processing notification.", notificationEx); PublishToQueue(notificationEx.ProcessingObject, Configuration.RabbitMQNotificationErrorQueue); } catch (Exception ex) { eventLogRepository.WriteErrorLog("Unhandled error processing notification.", ex); } }
private void ConsumeMessages() { while (consumingMessages && doListenForMessagesInTask) { string msg = ConsumeMessageFromQueue(); if (msg != null) { BaseNotification notification = NotificationExtension.Deserialize(msg); eventLogRepository.WriteInformationLog("Processing notification from queue. Notification: {QueueMessage}".InjectSingleValue("QueueMessage", msg)); var handler = notificationHandlerFactory(notification.NotificationType); // autofac will get the right handler handler.ProcessNotification(notification); // Always clear the context at the end of a transaction _uow.ClearContext(); } else { consumingMessages = false; } } }
static void Main(string[] args) { using (NotificationContext dbContext = new NotificationContext()) { dbContext.Configuration.AutoDetectChangesEnabled = false; GenericQueueRepositoryImpl rmq = new GenericQueueRepositoryImpl(); string rmqMessage = rmq.ConsumeFromQueue(Configuration.RabbitMQNotificationServer, Configuration.RabbitMQNotificationUserNameConsumer, Configuration.RabbitMQNotificationUserPasswordConsumer, Configuration.RabbitMQVHostNotification, Configuration.RabbitMQQueueNotification); while (rmqMessage != null) { BaseNotification rmqNotification = NotificationExtension.Deserialize(rmqMessage); if (rmqNotification.NotificationType == NotificationType.Eta) { KeithLink.Svc.Core.Models.Messaging.Queue.EtaNotification eta = (KeithLink.Svc.Core.Models.Messaging.Queue.EtaNotification)rmqNotification; Console.WriteLine(string.Format("Processing {0} orders", eta.Orders.Count)); foreach (OrderEta order in eta.Orders) { Models.EtaNotification notice = new Models.EtaNotification(); if (order.ActualTime != null && order.ActualTime.Length > 0) { notice.ActualTime = DateTime.Parse(order.ActualTime); } notice.Branch = order.BranchId; if (order.EstimatedTime != null && order.EstimatedTime.Length > 0) { notice.EstimatedTime = DateTime.Parse(order.EstimatedTime); } notice.OrderId = order.OrderId; if (order.OutOfSequence.HasValue) { notice.OutOfSequence = order.OutOfSequence.Value; } notice.RouteId = order.RouteId; if (order.ScheduledTime != null && order.ScheduledTime.Length > 0) { notice.ScheduledTime = DateTime.Parse(order.ScheduledTime); } notice.StopNumber = order.StopNumber; dbContext.Notifications.Add(notice); dbContext.SaveChanges(); } // end foreach } // end if rmqMessage = rmq.ConsumeFromQueue(Configuration.RabbitMQNotificationServer, Configuration.RabbitMQNotificationUserNameConsumer, Configuration.RabbitMQNotificationUserPasswordConsumer, Configuration.RabbitMQVHostNotification, Configuration.RabbitMQQueueNotification); } // wend if (dbContext.Database.Connection.State == System.Data.ConnectionState.Open) { dbContext.Database.Connection.Close(); } } //end using }