Esempio n. 1
0
 public void Add(Message message)
 {
     //_messages.Enqueue(message);
     using (var session = _diskQueue.OpenSession())
     {
         var byteMessage = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
         session.Enqueue(byteMessage);
         session.Flush();
     }
 }
 public T Dequeue()
 {
     using (var session = queue.OpenSession())
     {
         var bytes = session.Dequeue();
         if (bytes == null)
         {
             throw new NoElementsInQueueException();
         }
         session.Flush();
         return(bytes.FromBytes <T>());
     }
 }
Esempio n. 3
0
        public void can_enqueue_during_a_long_dequeue()
        {
            var s1 = _q?.OpenSession();

            using (var s2 = _q?.OpenSession())
            {
                s2?.Enqueue(new byte[] { 1, 2, 3, 4 });
                s2?.Flush();
            }

            var x = s1?.Dequeue();

            s1?.Flush();
            s1?.Dispose();

            Assert.That(x !.SequenceEqual(new byte[] { 1, 2, 3, 4 }));
        }
		public void setup()
		{
			_a_message = new byte[]{1,2,3,4};
			_b_message = new byte[] { 4, 3, 2, 1 };

			_session = Substitute.For<IPersistentQueueSession>();

			_queue = Substitute.For<IPersistentQueue>();
			_queue.OpenSession().Returns(_session);

			_queueFactory = Substitute.For<IOutgoingQueueFactory>();
			_queueFactory.PrepareQueue().Returns(_queue);



			_subject = new PersistentWorkQueue(_queueFactory, Substitute.For<ISleepWrapper>());
		}
		/// <summary>
		/// Try to move messages from the incoming queue to the dispatch queue
		/// </summary>
		void TryPumpingMessages(IPersistentQueue dispatchQueue)
		{
			try
			{
				using (var incomingQueue = PersistentQueue.WaitFor(_incomingPath, TimeSpan.FromSeconds(1)))
				using (var dst = dispatchQueue.OpenSession())
				using (var src = incomingQueue.OpenSession())
				{
					byte[] data;
					while ((data = src.Dequeue()) != null)
					{
						dst.Enqueue(data);
					}
					dst.Flush();
					src.Flush();
				}
			}
			catch (TimeoutException)
			{
				Ignore();
			}
		}
 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);
     }
 }