Ejemplo n.º 1
0
        public int StoreMessage(NGRIDMessageRecord messageRecord)
        {
            var       startTime     = DateTime.Now;
            Exception lastException = null;

            while (DateTime.Now.Subtract(startTime).TotalMilliseconds < TimeOut)
            {
                try
                {
                    return(_storageManager.StoreMessage(messageRecord));
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex.Message, ex);
                    lastException = ex;
                    Thread.Sleep(WaitTimeBeforeRetry);
                    RestartStorageManager();
                }
            }

            throw new NGRIDDatabaseException("Can not performed a database operation.", lastException);
        }
 /// <summary>
 /// Creates a new WaitingMessage object.
 /// </summary>
 /// <param name="messageRecord">Message record in storage manager</param>
 public WaitingMessage(NGRIDMessageRecord messageRecord)
 {
     MessageRecord = messageRecord;
     State = WaitingMessageStates.ReadyToSend;
 }
        /// <summary>
        /// This method is called when a NGRIDDataTransferMessage sent to this application.
        /// Stores message and adds to queue to send to remote application.
        /// </summary>
        /// <param name="message">Message</param>
        public void EnqueueMessage(NGRIDDataTransferMessage message)
        {
            //Create NGRIDMessageRecord from NGRIDDataTransferMessage to save message to database
            var messageRecord = new NGRIDMessageRecord(message) {NextServer = GetNextServerForMessage(message)};

            //Lock collection to be synchronized.
            lock (_waitingMessages)
            {
                if (message.TransmitRule == MessageTransmitRules.StoreAndForward)
                {
                    //Save message to database
                    StorageManager.StoreMessage(messageRecord);
                    /* If these conditions are true, then message also added to _waitingMessages 
                     * and message is sent to appliction from this queue instead of read again from database (for performance reasons):
                     * - _waitingMessages's message count is smaller than a maximum count (_waitingMessages.Count < MaxMessagesInQueue).
                     * - All messages in database is also in _waitingMessages (_biggestWaitingMessageIdInList >= _biggestWaitingMessageId) 
                     *   That means there is no message that is in database but not in _waitingMessages list.
                     */
                    if (_waitingMessages.Count < MaxMessagesInQueue &&
                        _biggestWaitingMessageIdInList >= _biggestWaitingMessageId)
                    {
                        //Add message to queue.
                        _waitingMessages.AddLast(new WaitingMessage(messageRecord));
                        //This message's Id is new biggest id on queue.
                        _biggestWaitingMessageIdInList = messageRecord.Id;
                    }

                    //This message's id is new biggest id in database, so update _biggestWaitingMessageId value
                    _biggestWaitingMessageId = messageRecord.Id;
                }
                else
                {
                    //Add message to queue.
                    _waitingMessages.AddFirst(new WaitingMessage(messageRecord));
                }

                //Pulse waiting thread that is in wait state because of no message to process.
                if (!_waitingForAnError)
                {
                    Monitor.PulseAll(_waitingMessages);
                }

                Logger.Debug("EnqueueMessage - WaitingMessages.Count = " + _waitingMessages.Count + ", Application = " + Name);
            }
        }
        public int StoreMessage(NGRIDMessageRecord messageRecord)
        {
            var startTime = DateTime.Now;
            Exception lastException = null;
            while (DateTime.Now.Subtract(startTime).TotalMilliseconds < TimeOut)
            {
                try
                {
                    return _storageManager.StoreMessage(messageRecord);
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex.Message, ex);
                    lastException = ex;
                    Thread.Sleep(WaitTimeBeforeRetry);
                    RestartStorageManager();
                }
            }

            throw new NGRIDDatabaseException("Can not performed a database operation.", lastException);
        }