/// <summary> /// Enqueue the messages in the collection for relaying. /// </summary> /// <param name="inboundMessages">Messages to enqueue.</param> public static void Enqueue(MtaMessageCollection inboundMessages) { Parallel.ForEach(inboundMessages, message => { Enqueue(MtaQueuedMessage.CreateNew(message)); }); RabbitMqManager.Ack(RabbitMqManager.RabbitMqQueue.Inbound, inboundMessages.Max(m => m.RabbitMqDeliveryTag), true); }
/// <summary> /// Dequeues a collection of inbound messages from RabbitMQ. /// </summary> /// <param name="maxItems">The maximum amount of messages to dequeue.</param> /// <returns>The dequeue messages.</returns> public static MtaMessageCollection Dequeue(int maxItems) { List <BasicDeliverEventArgs> items = RabbitMqManager.Dequeue(RabbitMqManager.RabbitMqQueue.Inbound, maxItems, 1 * 1000); MtaMessageCollection messages = new MtaMessageCollection(); if (items.Count == 0) { return(messages); } foreach (BasicDeliverEventArgs ea in items) { MtaMessage msg = Serialisation.Deserialise <MtaMessage>(ea.Body); msg.RabbitMqDeliveryTag = ea.DeliveryTag; messages.Add(msg); } return(messages); }
/// <summary> /// Does the actual bulk importing from RabbitMQ to SQL Server. /// </summary> private void DoSqlBulkInsertFromRabbitMQ() { // Keep going until Manta is stopping. while (!_isStopping) { try { // Get queued messages for bulk importing. MtaMessageCollection recordsToImportToSql = RabbitMq.RabbitMqInboundQueueManager.Dequeue(100); // If there are no messages to import then sleep and try again. if (recordsToImportToSql == null || recordsToImportToSql.Count == 0) { Thread.Sleep(RABBITMQ_MAX_TIME_IN_QUEUE); continue; } DataTable dt = new DataTable(); dt.Columns.Add("mta_msg_id", typeof(Guid)); dt.Columns.Add("mta_send_internalId", typeof(int)); dt.Columns.Add("mta_msg_rcptTo", typeof(string)); dt.Columns.Add("mta_msg_mailFrom", typeof(string)); foreach (MtaMessage msg in recordsToImportToSql) { dt.Rows.Add(new object[] { msg.ID, msg.InternalSendID, msg.RcptTo[0], msg.MailFrom }); } try { // Create a record of the messages in SQL server. using (SqlConnection conn = DAL.MantaDB.GetSqlConnection()) { SqlBulkCopy bulk = new SqlBulkCopy(conn); bulk.DestinationTableName = "man_mta_msg_staging"; foreach (DataColumn c in dt.Columns) { bulk.ColumnMappings.Add(c.ColumnName, c.ColumnName); } conn.Open(); bulk.WriteToServer(dt); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = @" BEGIN TRANSACTION MERGE man_mta_msg AS target USING (SELECT * FROM man_mta_msg_staging) AS source ON (target.[mta_msg_id] = source.[mta_msg_id]) WHEN NOT MATCHED THEN INSERT ([mta_msg_id], [mta_send_internalId], [mta_msg_rcptTo], [mta_msg_mailFrom]) VALUES (source.[mta_msg_id], source.[mta_send_internalId], source.[mta_msg_rcptTo], source.[mta_msg_mailFrom]); DELETE FROM [man_mta_msg_staging] COMMIT TRANSACTION"; cmd.ExecuteNonQuery(); } RabbitMq.RabbitMqManager.Ack(RabbitMq.RabbitMqManager.RabbitMqQueue.Inbound, recordsToImportToSql.Max(r => r.RabbitMqDeliveryTag), true); } catch (Exception ex) { Logging.Warn("Server Queue Manager", ex); } } catch (Exception) { //Logging.Error("Bulk Importer Error", ex); } } _hasStopped = true; }