Beispiel #1
0
        private void RestoreQueueFromFile()
        {
            try
            {
                if (File.Exists(SAVEFILENAME))
                {
                    StreamReader streamReader = new StreamReader(SAVEFILENAME, true);

                    var fileContent = streamReader.ReadToEnd();
                    CurrentEncoding = streamReader.CurrentEncoding;

                    var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto };
                    var memoryCacheQueuePersist = JsonConvert.DeserializeObject<MemoryCacheQueuePersist>(fileContent, settings);

                    if (!memoryCacheQueuePersist.IsAllSentToRemoteApi)
                    {
                        this._safeQueue = Queue.Synchronized(new Queue(memoryCacheQueuePersist.WorkingQueue));
                    }

                    streamReader.Close();
                }
            }
            catch (Exception ex)
            {
                LogLog.Error(MethodBase.GetCurrentMethod().DeclaringType,
                       string.Format("RestoreQueueFromFile出现异常, ex = {0}", ex));
                InternalLogHelper.WriteLog(ex.ToString());
            }
        }
Beispiel #2
0
        private void DequeueWorkerFunc()
        {
            while (true)
            {
                try
                {
                    if (!IsProcessExit)
                    {
                        _workerThreadNeedToWorkAutoResetEvent.WaitOne(500);

                        /*取队列中的数据发送到远程接口*/
                        DequeueWorkerFunc_SendToRemoteApi();
                    }
                    else
                    {
                        /*当IsProcessExit = true时该线程直接退出*/

                        break;
                    }
                }
                catch (Exception e)
                {
                    ErrorHandler.Error(string.Format("MemoryCacheQueue->DequeueWorkerFunc出现异常, Exception = {0}", e));
                    InternalLogHelper.WriteLog(e.ToString());
                }
            }
        }
Beispiel #3
0
        private void SaveQueueToFile()
        {
            MemoryCacheQueuePersist memoryCacheQueuePersist = new MemoryCacheQueuePersist() { LastSaveTime = DateTime.Now };

            try
            {
                StreamWriter streamWriter = new StreamWriter(SAVEFILENAME, false, CurrentEncoding);

                var objArray = _safeQueue.ToArray();
                List<string> items = new List<string>();
                foreach (var o in objArray)
                {
                    items.Add(o as string);
                }
                memoryCacheQueuePersist.WorkingQueue = items;
                memoryCacheQueuePersist.IsAllSentToRemoteApi = _safeQueue.Count == 0;

                var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto };
                var serializeString = JsonConvert.SerializeObject(memoryCacheQueuePersist, settings);
                streamWriter.Write(serializeString);

                streamWriter.Close();
            }
            catch (Exception ex)
            {
                LogLog.Error(MethodBase.GetCurrentMethod().DeclaringType,
                    string.Format("SaveQueueToFile出现异常, ex = {0}", ex));
                InternalLogHelper.WriteLog(ex.ToString());
            }
        }
        public override void ActivateOptions()
        {
            InternalLogHelper.WriteLog("MemoryOptimizedAppender->ActivateOptions");

            MemoryCacheQueue.Instance().RemoteAddress = RemoteAddress;
            MemoryCacheQueue.Instance().ErrorHandler  = ErrorHandler;
            MemoryCacheQueue.Instance().Initialize();

            base.ActivateOptions();
            MemoryCacheQueue.Instance().StartDequeueWorkerThread(); /*Queue初始化完成后开始工作者线程*/
        }
Beispiel #5
0
 /*工作者线程调用的方法,只允许有一个工作者线程*/
 private void SendDataToRemoteApi(string data)
 {
     try
     {
         MakeSureRemoteDataSinkCanBeUsed();
         _remoteDataSink.NewLog(data);
     }
     catch (Exception e)
     {
         ErrorHandler.Error(string.Format("MemoryCacheQueue->SendDataToRemoteApi出现异常, Exception = {0}", e));
         InternalLogHelper.WriteLog(e.ToString());
     }
 }
 protected override void OnClose()
 {
     try
     {
         base.OnClose();
         MemoryCacheQueue.Instance().DoClean();
     }
     catch (Exception ex)
     {
         LogLog.Error(typeof(MemoryOptimizedAppender), ex.ToString());
         InternalLogHelper.WriteLog(ex.ToString());
     }
 }
Beispiel #7
0
 private void CreateRemoteDataSink()
 {
     try
     {
         Binding binding = new NetTcpBinding();
         EndpointAddress endpointAddress = new EndpointAddress(RemoteAddress);
         _remoteDataSink = ChannelFactory<IRemoteDataSink>.CreateChannel(binding, endpointAddress);
     }
     catch (Exception e)
     {
         ErrorHandler.Error(string.Format("MemoryCacheQueue->CreateRemoteDataSink出现异常, Exception = {0}", e));
         InternalLogHelper.WriteLog(e.ToString());
     }
 }
Beispiel #8
0
 public void StartDequeueWorkerThread()
 {
     _workerThread.Start();
     InternalLogHelper.WriteLog("MemoryCacheQueue->StartDequeueWorkerThread");
 }
Beispiel #9
0
        public void Initialize()
        {
            RestoreQueueFromFile();

            InternalLogHelper.WriteLog("MemoryCacheQueue->Initialize完成");
        }