예제 #1
0
        /// <summary>
        /// Send Queue Messages
        /// </summary>
        /// <param name="messageQueueing"></param>
        /// <param name="outboundSemaphoreKey"></param>
        /// <returns></returns>
        public async Task <ResponseModel <List <MessageQueue> > > SendQueueMessages(List <IMessageQueueConfiguration> messageQueueConfigurations, string outboundSemaphoreKey, ConnectionStrings connectionStrings)
        {
            ResponseModel <List <MessageQueue> > returnResponse = new ResponseModel <List <MessageQueue> >();

            returnResponse.Entity = new List <MessageQueue>();

            lock (_sendingLock)
            {
                if (_sending)
                {
                    Console.WriteLine("Aborted iteration still sending");
                    return(returnResponse);
                }

                _sending = true;
            }

            TransactionQueueSemaphore transactionQueueSemaphore = null;

            try
            {
                _purchaseOrderManagementDataService.OpenConnection(connectionStrings.PrimaryDatabaseConnectionString);
                _purchaseOrderManagementDataService.BeginTransaction((int)IsolationLevel.Serializable);

                transactionQueueSemaphore = await _purchaseOrderManagementDataService.GetTransactionQueueSemaphore(outboundSemaphoreKey);

                if (transactionQueueSemaphore == null)
                {
                    transactionQueueSemaphore = new TransactionQueueSemaphore();
                    transactionQueueSemaphore.SemaphoreKey = outboundSemaphoreKey;
                    await _purchaseOrderManagementDataService.CreateTransactionQueueSemaphore(transactionQueueSemaphore);
                }
                else
                {
                    await _purchaseOrderManagementDataService.UpdateTransactionQueueSemaphore(transactionQueueSemaphore);
                }

                List <TransactionQueueOutbound> transactionQueue = await _purchaseOrderManagementDataService.GetOutboundTransactionQueue();

                foreach (TransactionQueueOutbound transactionQueueItem in transactionQueue)
                {
                    MessageQueue message = new MessageQueue();
                    message.ExchangeName       = transactionQueueItem.ExchangeName;
                    message.TransactionQueueId = transactionQueueItem.TransactionQueueOutboundId;
                    message.TransactionCode    = transactionQueueItem.TransactionCode;
                    message.Payload            = transactionQueueItem.Payload;

                    IMessageQueueConfiguration messageQueueConfiguration = messageQueueConfigurations.Where(x => x.TransactionCode == message.TransactionCode).FirstOrDefault();
                    if (messageQueueConfiguration == null)
                    {
                        break;
                    }

                    ResponseModel <MessageQueue> messageQueueResponse = messageQueueConfiguration.SendMessage(message);

                    if (messageQueueResponse.ReturnStatus == true)
                    {
                        transactionQueueItem.SentToExchange     = true;
                        transactionQueueItem.DateSentToExchange = DateTime.UtcNow;
                        await _purchaseOrderManagementDataService.UpdateOutboundTransactionQueue(transactionQueueItem);

                        returnResponse.Entity.Add(message);
                    }
                }

                await _purchaseOrderManagementDataService.UpdateDatabase();

                _purchaseOrderManagementDataService.CommitTransaction();
                _purchaseOrderManagementDataService.CloseConnection();
            }
            catch (Exception ex)
            {
                _purchaseOrderManagementDataService.RollbackTransaction();
                returnResponse.ReturnStatus = false;
                returnResponse.ReturnMessage.Add(ex.Message);
            }
            finally
            {
                _purchaseOrderManagementDataService.CloseConnection();
                _sending = false;
            }

            return(returnResponse);
        }