Beispiel #1
0
        /// <summary>
        /// Process Messages
        /// </summary>
        /// <returns></returns>
        public async Task <ResponseModel <List <MessageQueue> > > ProcessMessages(string inboundSemaphoreKey, ConnectionStrings connectionStrings)
        {
            ResponseModel <List <MessageQueue> > returnResponse = new ResponseModel <List <MessageQueue> >();

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

            TransactionQueueSemaphore transactionQueueSemaphore = null;

            lock (_processingLock)
            {
                if (_processing == true)
                {
                    Console.WriteLine("Processing iteration aborted");
                    return(returnResponse);
                }

                _processing = true;
            }

            try
            {
                _inventoryManagementDataService.OpenConnection(connectionStrings.PrimaryDatabaseConnectionString);

                _inventoryManagementDataService.BeginTransaction((int)IsolationLevel.Serializable);

                Console.WriteLine("Get Lock at " + DateTime.Now.ToString());
                transactionQueueSemaphore = await _inventoryManagementDataService.GetTransactionQueueSemaphore(inboundSemaphoreKey);

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

                List <TransactionQueueInbound> transactionQueue = await _inventoryManagementDataService.GetInboundTransactionQueue();

                foreach (TransactionQueueInbound transactionQueueItem in transactionQueue)
                {
                    int    senderId        = transactionQueueItem.SenderTransactionQueueId;
                    string exchangeName    = transactionQueueItem.ExchangeName;
                    string transactionCode = transactionQueueItem.TransactionCode;

                    TransactionQueueInboundHistory transactionHistory = await _inventoryManagementDataService.GetInboundTransactionQueueHistoryBySender(senderId, exchangeName);

                    if (transactionHistory != null)
                    {
                        await LogDuplicateMessage(transactionQueueItem);

                        await _inventoryManagementDataService.DeleteInboundTransactionQueueEntry(transactionQueueItem.TransactionQueueInboundId);
                    }
                    else if (transactionCode == TransactionQueueTypes.PurchaseOrderSubmitted)
                    {
                        await PurchaseOrderSubmitted(transactionQueueItem);

                        await _inventoryManagementDataService.DeleteInboundTransactionQueueEntry(transactionQueueItem.TransactionQueueInboundId);
                    }
                    else if (transactionCode == TransactionQueueTypes.SalesOrderSubmitted)
                    {
                        await SalesOrderSubmitted(transactionQueueItem);

                        await _inventoryManagementDataService.DeleteInboundTransactionQueueEntry(transactionQueueItem.TransactionQueueInboundId);
                    }
                    else if (transactionCode == TransactionQueueTypes.Acknowledgement)
                    {
                        await ProcessAcknowledgement(transactionQueueItem);

                        await _inventoryManagementDataService.DeleteInboundTransactionQueueEntry(transactionQueueItem.TransactionQueueInboundId);
                    }
                }

                await _inventoryManagementDataService.UpdateDatabase();

                _inventoryManagementDataService.CommitTransaction();

                _inventoryManagementDataService.CloseConnection();
            }
            catch (Exception ex)
            {
                _inventoryManagementDataService.RollbackTransaction();
                returnResponse.ReturnStatus = false;
                returnResponse.ReturnMessage.Add(ex.Message);
            }
            finally
            {
                _inventoryManagementDataService.CloseConnection();
                _processing = false;
            }

            return(returnResponse);
        }