예제 #1
0
 /// <summary>
 /// Saves a MDSMessageRecord object.
 /// </summary>
 /// <param name="messageRecord"></param>
 public int StoreMessage(MDSMessageRecord messageRecord)
 {
     lock (_messages)
     {
         messageRecord.Id = (++_lastRecordId);
         _messages.Add(messageRecord.Id, messageRecord);
         return(messageRecord.Id);
     }
 }
예제 #2
0
 /// <summary>
 /// Saves a MDSMessageRecord object.
 /// </summary>
 /// <param name="messageRecord"></param>
 public int StoreMessage(MDSMessageRecord messageRecord)
 {
     lock (_messages)
     {
         messageRecord.Id = (++_lastRecordId);
         _messages.Add(messageRecord.Id, messageRecord);
         return messageRecord.Id;
     }
 }
예제 #3
0
        /// <summary>
        /// This method is called when a MDSDataTransferMessage sent to this application.
        /// Stores message and adds to queue to send to remote application.
        /// </summary>
        /// <param name="message">Message</param>
        public void EnqueueMessage(MDSDataTransferMessage message)
        {
            //Create MDSMessageRecord from MDSDataTransferMessage to save message to database
            var messageRecord = new MDSMessageRecord(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);
            }
        }
 /// <summary>
 /// Saves a MDSMessageRecord.
 /// </summary>
 /// <param name="messageRecord">MDSMessageRecord object to save</param>
 /// <returns>Auto Increment Id of saved message</returns>
 public int StoreMessage(MDSMessageRecord messageRecord)
 {
     var bytesOfMessage = MDSSerializationHelper.SerializeToByteArray(messageRecord.Message);
     var id = InsertAndGetLastId(
         "INSERT INTO messages(MessageId, DestServer, NextServer, DestApplication, MessageData, MessageDataLength, RecordDate) VALUES(?,?,?,?,?,?,now());",
         new OdbcParameter("?MessageId", messageRecord.MessageId),
         new OdbcParameter("?DestServer", messageRecord.DestServer),
         new OdbcParameter("?NextServer", messageRecord.NextServer),
         new OdbcParameter("?DestApplication", messageRecord.DestApplication),
         new OdbcParameter("?MessageData", bytesOfMessage),
         new OdbcParameter("?MessageDataLength", bytesOfMessage.Length)
         );
     messageRecord.Id = id;
     return id;
 }
예제 #5
0
        /// <summary>
        /// Saves a MDSMessageRecord.
        /// </summary>
        /// <param name="messageRecord">MDSMessageRecord object to save</param>
        /// <returns>Auto Increment Id of saved message</returns>
        public int StoreMessage(MDSMessageRecord messageRecord)
        {
            var bytesOfMessage = MDSSerializationHelper.SerializeToByteArray(messageRecord.Message);
            var id             = InsertAndGetLastId(
                "INSERT INTO Messages(MessageId, DestServer, NextServer, DestApplication, MessageData, MessageDataLength, RecordDate) VALUES(@MessageId, @DestServer, @NextServer, @DestApplication, @MessageData, @MessageDataLength, GetDate())",
                new SqlParameter("@MessageId", messageRecord.MessageId),
                new SqlParameter("@DestServer", messageRecord.DestServer),
                new SqlParameter("@NextServer", messageRecord.NextServer),
                new SqlParameter("@DestApplication", messageRecord.DestApplication),
                new SqlParameter("@MessageData", bytesOfMessage),
                new SqlParameter("@MessageDataLength", bytesOfMessage.Length)
                );

            messageRecord.Id = id;
            return(id);
        }
예제 #6
0
        public int StoreMessage(MDSMessageRecord messageRecord)
        {
            int id = 0;

            var bytesOfMessage = MDSSerializationHelper.SerializeToByteArray(messageRecord.Message);



            //Buscando id e insertando
            while (true)
            {
                var collection = _database.GetCollection <MDSMessageRecord>(_tableName);

                int lastId = collection.AsQueryable()
                             .OrderByDescending(m => m.Id)
                             .Select(m => m.Id)
                             .FirstOrDefault();

                id = lastId + 1;
                messageRecord.Id = id;

                try
                {
                    collection.InsertOne(messageRecord);
                }
                catch (Exception writeError)
                {
                    if (writeError.Message.Contains("11000"))
                    {
                        continue;
                    }
                    else
                    {
                        throw writeError;
                    }
                }

                break;
            }

            return(id);
        }
예제 #7
0
 /// <summary>
 /// Creates a new WaitingMessage object.
 /// </summary>
 /// <param name="messageRecord">Message record in storage manager</param>
 public WaitingMessage(MDSMessageRecord messageRecord)
 {
     MessageRecord = messageRecord;
     State         = WaitingMessageStates.ReadyToSend;
 }
예제 #8
0
 /// <summary>
 /// Saves a MDSMessageRecord.
 /// </summary>
 /// <param name="messageRecord">MDSMessageRecord object to save</param>
 /// <returns>Auto Increment Id of saved message</returns>
 public int StoreMessage(MDSMessageRecord messageRecord)
 {
     lock (_syncObj)
     {
         var bytesOfMessage = MDSSerializationHelper.SerializeToByteArray(messageRecord.Message);
         var id = InsertAndGetLastId(
             "INSERT INTO messages(MessageId, DestServer, NextServer, DestApplication, MessageData, MessageDataLength, RecordDate) VALUES(@MessageId,@DestServer,@NextServer,@DestApplication,@MessageData,@MessageDataLength,@RecordDate);",
             new SQLiteParameter("@MessageId", messageRecord.MessageId),
             new SQLiteParameter("@DestServer", messageRecord.DestServer),
             new SQLiteParameter("@NextServer", messageRecord.NextServer),
             new SQLiteParameter("@DestApplication", messageRecord.DestApplication),
             new SQLiteParameter("@MessageData", bytesOfMessage),
             new SQLiteParameter("@MessageDataLength", bytesOfMessage.Length),
             new SQLiteParameter("@RecordDate", DateTime.Now)
             );
         messageRecord.Id = id;
         return id;
     }
 }
 /// <summary>
 /// Creates a new WaitingMessage object.
 /// </summary>
 /// <param name="messageRecord">Message record in storage manager</param>
 public WaitingMessage(MDSMessageRecord messageRecord)
 {
     MessageRecord = messageRecord;
     State = WaitingMessageStates.ReadyToSend;
 }
        /// <summary>
        /// This method is called when a MDSDataTransferMessage sent to this application.
        /// Stores message and adds to queue to send to remote application.
        /// </summary>
        /// <param name="message">Message</param>
        public void EnqueueMessage(MDSDataTransferMessage message)
        {
            //Create MDSMessageRecord from MDSDataTransferMessage to save message to database
            var messageRecord = new MDSMessageRecord(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(MDSMessageRecord 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 MDSDatabaseException("Can not performed a database operation.", lastException);
        }