예제 #1
0
        public void Handle(NetMQFrame[] sender, NetMQMessage message)
        {
            Logger.Debug("[Queue_AcknowledgeHandler] Received an ack.");

            var requestId = message.Pop();
            var context = message.Pop().ConvertToString();
            var queueId = message.Pop().ConvertToString();
            var subscriberId = message.Pop().ConvertToString();

            var allocationId = message.PopNullableInt64();
            var allocationSize = message.PopInt32();
            var allocationTimeInMilliseconds = message.PopInt32();

            var ack = new AcknowledgeQueue(context,
                queueId,
                subscriberId,
                allocationId,
                allocationSize, allocationTimeInMilliseconds);

            var queuedEvents = _storage.AcknowledgeAndFetchNext(ack);
            var events = queuedEvents.Events;

            var msg = new NetMQMessage();
            msg.Append(sender);
            msg.AppendEmptyFrame();
            msg.Append(ResProtocol.ResClient01);
            msg.Append(requestId);
            msg.Append(ResCommands.QueuedEvents);
            msg.Append(context);
            msg.Append(queueId);
            msg.Append(subscriberId);
            msg.Append(DateTime.UtcNow.ToNetMqFrame());
            msg.Append(queuedEvents.AllocationId.ToNetMqFrame());
            var count = events.Length;
            msg.Append(count.ToNetMqFrame());

            foreach (var e in events)
            {
                msg.Append(e.EventId.ToByteArray());
                msg.Append(e.Stream);
                msg.Append(e.Context);
                msg.Append(e.Sequence.ToNetMqFrame());
                msg.Append(e.Timestamp.ToNetMqFrame());
                msg.Append(e.TypeKey);
                msg.Append(e.Headers.ToNetMqFrame());
                msg.Append(e.Body);
            }

            var result = new QueuedMessagesFetched(msg);
            while (!_outBuffer.Offer(result))
                _spin.SpinOnce();
        }
예제 #2
0
        public QueuedEvents AcknowledgeAndFetchNext(AcknowledgeQueue request)
        {
            using (var connection = new SqlConnection(_connectionString))
            using (var command = new SqlCommand("Queues_Acknowledge", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("Context", request.Context);
                command.Parameters.AddWithValue("QueueId", request.QueueId);
                command.Parameters.AddWithValue("SubscriberId", request.SubscriberId);
                if (request.AllocationId.HasValue)
                    command.Parameters.AddWithValue("AllocationId", request.AllocationId.Value);
                command.Parameters.AddWithValue("Count", request.AllocationSize);
                command.Parameters.AddWithValue("AllocationTimeInMilliseconds", request.AllocationTimeInMilliseconds);

                command.Connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    var events = new List<EventInStorage>();

                    while (reader.Read())
                    {
                        var @event = readEventInStorage(reader, 0);
                        events.Add(@event);
                    }

                    reader.NextResult();

                    long? allocationId = null;

                    if (reader.Read())
                    {
                        var sqlInt64 = reader.GetSqlInt64(0);
                        allocationId = sqlInt64.IsNull ? (long?)null : sqlInt64.Value;
                    }

                    return new QueuedEvents(allocationId, events.ToArray());
                }
            }
        }