コード例 #1
0
 protected virtual void runThread()
 {
     try {
         while (true)
         {
             while (this.webService.testConnection())
             {
                 // we got some connection
                 // make a dictionary which we later send in msgpack
                 var payLoad = new WebServicePayLoad();
                 payLoad.version = this.protocolversion;
                 payLoad.id      = Guid.NewGuid().ToString();
                 // logs will be dictionaries stored in an array
                 payLoad.logs = new List <Dictionary <string, string> >();
                 // get logs from the queue and send them
                 using (var session = queue.OpenSession()) {
                     try {
                         int i = 0;
                         while (i < this.logsPerBatch)
                         {
                             var pack = session.Dequeue();
                             // if task is null then queue is empty so we just submit
                             if (pack == null)
                             {
                                 break;
                             }
                             // unpack to dictionary and add to array
                             var eventPayload = this.logEventSerializer.UnpackSingleObject(pack);
                             payLoad.logs.Add(eventPayload);
                             i++;
                         }
                         // if we havn't got any logs after this the queue is empty and we break the connection loop (to wait the long time)
                         if (payLoad.logs.Count == 0)
                         {
                             break;
                         }
                         var dataToSend = this.logBatchSerializer.PackSingleObject(payLoad);
                         var resp       = this.webService.sendData(dataToSend, payLoad.id);
                         if (resp)
                         {
                             // everything went well so we acknowledge the stuff we picked from the queue
                             session.Flush();
                         }
                         else
                         {
                             // break the loop on error
                             InternalLogger.Error("get unexpected response {0} from log server", resp);
                             break;
                         }
                     } catch (Exception ex) {
                         InternalLogger.Error("error in messagepack target when creating batch {0} {1} {2}", ex.Message, ex.StackTrace, ex.ToString());
                         break;
                     }
                 }
                 // we submitted a batch and now sleep before the next one
                 Thread.Sleep(this.waitBetweenBatch);
             }
             // connect failed wait some time before trying again
             Thread.Sleep(this.waitBetweenConnections);
         }
     }
     catch (ThreadInterruptedException) {
         this.queue.Dispose();
     }
     catch (Exception ex)
     {
         InternalLogger.Error("Something failed in MsgPackTarget background thread {0} {1} {2}", ex.Message, ex.ToString(), ex.StackTrace);
     }
 }
コード例 #2
0
        protected virtual void runThread()
        {
            try {
                while (true) {
                    while (this.webService.testConnection()) {
                        // we got some connection
                        // make a dictionary which we later send in msgpack
                        var payLoad = new WebServicePayLoad();
                        payLoad.version = this.protocolversion;
                        payLoad.id = Guid.NewGuid().ToString();
                        // logs will be dictionaries stored in an array
                        payLoad.logs = new List<Dictionary<string,string>>();
                        // get logs from the queue and send them
                        using (var session = queue.OpenSession()) {
                            try {
                                int i = 0;
                                while (i<this.logsPerBatch) {
                                    var pack = session.Dequeue();
                                    // if task is null then queue is empty so we just submit
                                    if (pack == null) {
                                        break;
                                    }
                                    // unpack to dictionary and add to array
                                    var eventPayload = this.logEventSerializer.UnpackSingleObject(pack);
                                    payLoad.logs.Add(eventPayload);
                                    i++;
                                }
                                // if we havn't got any logs after this the queue is empty and we break the connection loop (to wait the long time)
                                if (payLoad.logs.Count == 0)
                                {
                                    break;
                                }
                                var dataToSend = this.logBatchSerializer.PackSingleObject(payLoad);
                                var resp = this.webService.sendData(dataToSend,payLoad.id);
                                if (resp) {
                                    // everything went well so we acknowledge the stuff we picked from the queue
                                     session.Flush();
                                }
                                else {
                                    // break the loop on error
                                    InternalLogger.Error("get unexpected response {0} from log server",resp);
                                    break;
                                }

                            } catch (Exception ex) {
                                InternalLogger.Error("error in messagepack target when creating batch {0} {1} {2}",ex.Message,ex.StackTrace,ex.ToString());
                                break;
                            }

                        }
                        // we submitted a batch and now sleep before the next one
                        Thread.Sleep(this.waitBetweenBatch);
                    }
                    // connect failed wait some time before trying again
                    Thread.Sleep(this.waitBetweenConnections);
                }
            }
            catch (ThreadInterruptedException) {
                this.queue.Dispose();
            }
            catch (Exception ex)
            {
                InternalLogger.Error("Something failed in MsgPackTarget background thread {0} {1} {2}", ex.Message, ex.ToString(), ex.StackTrace);
            }
        }