public void Handle(NetMQFrame[] sender, NetMQMessage message) { Logger.Debug("[Queue_SubscribeHandler] Received subscribe request."); var requestId = message.Pop(); var context = message.Pop().ConvertToString(); var queueId = message.Pop().ConvertToString(); var subscriberId = message.Pop().ConvertToString(); var filter = message.Pop().ConvertToString(); var utcStartTime = message.PopDateTime(); var allocationSize = message.PopInt32(); var allocationTimeInMilliseconds = message.PopInt32(); var subscribe = new SubscribeToQueue(context, queueId, subscriberId, filter, utcStartTime, allocationSize, allocationTimeInMilliseconds); var queuedEvents = _storage.Subscribe(subscribe); 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(); }
public QueuedEvents Subscribe(SubscribeToQueue request) { using (var connection = new SqlConnection(_connectionString)) using (var command = new SqlCommand("Queues_Subscribe", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("Context", request.Context); command.Parameters.AddWithValue("QueueId", request.QueueId); command.Parameters.AddWithValue("SubscriberId", request.SubscriberId); command.Parameters.AddWithValue("Filter", request.Filter); command.Parameters.AddWithValue("StartTime", request.UtcQueueStartTime); command.Parameters.AddWithValue("Count", request.AllocationSize); command.Parameters.AddWithValue("AllocationTimeInMilliseconds", request.AllocationTimeoutInMilliseconds); command.Connection.Open(); try { 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()); } } catch (SqlException ex) { if (ex.Number == 2627) //pk violation { throw new QueueAlreadyExistsInContextWithDifferentFilterException(request.Context, request.QueueId, request.Filter); } throw; } } }