Beispiel #1
0
 public QueueMessage(Message message)
 {
     this.Body = message.Body;
     this.CreatedTime = message.CreatedTime;
     this.MetadataInfo = message.MetadataInfo;
     this.RoutingKey = message.RoutingKey;
 }
Beispiel #2
0
        private void Processing(Message message)
        {
            if (message.IsNull())
                return;

            Task.Factory
                .StartNew((state) => {
                    this.MessageReceived(state, new EventArgs<Message>(message));
                }, this, TaskCreationOptions.PreferFairness);
        }
Beispiel #3
0
 public void SendAsync(Message message, Action successCallback, Action<Exception> failCallback)
 {
     this.SendAsync(new[] { message }, successCallback, failCallback);
 }
Beispiel #4
0
 public void Send(Message message)
 {
     this.SendAsync(message, () => { }, (ex) => { });
 }
Beispiel #5
0
        public bool TryAdd(Message message)
        {
            MessageQueue queue;
            if (queues.Length == 1) {
                queue = queues[0];
            }
            else {
                if (string.IsNullOrWhiteSpace(message.RoutingKey)) {
                    queue = queues.OrderBy(p => p.Count).First();
                }
                else {
                    var queueIndex = Math.Abs(message.RoutingKey.GetHashCode() % queues.Length);
                    queue = queues[queueIndex];
                }
            }


            var queueMsg = new QueueMessage(message) {
                Offset = Interlocked.Increment(ref produceOffset)
            };
            //queueMsg.Offset = topicOffset.AddOrUpdate(message.MetadataInfo[StandardMetadata.Kind], 0,
            //                (topic, offset) => Interlocked.Increment(ref offset));

            if (!offsetDict.TryAdd(produceOffset, queue.Id)) {
                return false;
            }
            queue.Enqueue(queueMsg);

            return true;
        }
Beispiel #6
0
        public void Complete(Message message = null)
        {
            var queueMsg = message as QueueMessage;

            bool release;
            if (queueMsg != null) {
                int completeQueueIndex = queueMsg.QueueId;
                release = Interlocked.CompareExchange(ref lastQueueIndex, -1, completeQueueIndex) == completeQueueIndex;
            }
            else {
                release = lastQueueIndex == -1;
            }

            if (release) {
                waiter.Set();
            }
        }
Beispiel #7
0
        public bool TryTake(out Message message)
        {
            int queueIndex;
            if (!offsetDict.TryGetValue(consumeOffset, out queueIndex) || lastQueueIndex == queueIndex) {
                message = null;
                return false;
            }

            if (offsetDict.TryRemove(consumeOffset, out queueIndex)) {
                //while (true) {
                //    var queueMsg = queues[queueIndex].Dequeue();
                //    if (queueMsg.Offset == consumeOffset) {
                //        message = queueMsg as Message;
                //        break;
                //    }
                //    else {
                //        queues[queueIndex].Enqueue(queueMsg);
                //    }
                //}
                message = queues[queueIndex].Dequeue();

                Interlocked.Exchange(ref lastQueueIndex, queueIndex);
                Interlocked.Increment(ref consumeOffset);
                
                return true;
            }            

            message = null;
            return false;
        }