/// <summary> /// Clears messages in a queue. /// Required administration permission. /// If server has no implementation for administration authorization, request is not allowed. /// </summary> public Task <HorseResult> ClearMessages(string queue, bool clearPriorityMessages, bool clearMessages) { if (!clearPriorityMessages && !clearMessages) { return(Task.FromResult(HorseResult.Failed())); } HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.ClearMessages; message.SetTarget(queue); message.WaitResponse = true; message.SetMessageId(_client.UniqueIdGenerator.Create()); message.AddHeader(HorseHeaders.QUEUE_NAME, queue); if (clearPriorityMessages) { message.AddHeader(HorseHeaders.PRIORITY_MESSAGES, "yes"); } if (clearMessages) { message.AddHeader(HorseHeaders.MESSAGES, "yes"); } return(_client.WaitResponse(message, true)); }
/// <summary> /// Creates new Pull Request response message with no content /// </summary> internal static HorseMessage CreateNoContentPullResponse(HorseMessage request, string reason) { HorseMessage msg = new HorseMessage(MessageType.QueueMessage); msg.SetMessageId(request.MessageId); msg.SetTarget(request.Target); msg.ContentType = request.ContentType; msg.AddHeader(HorseHeaders.REQUEST_ID, request.MessageId); msg.AddHeader(HorseHeaders.NO_CONTENT, reason); return(msg); }
/// <summary> /// Publishes a byte array data to a router /// </summary> public async Task <HorseResult> Publish(string routerName, byte[] data, string messageId = null, bool waitForAcknowledge = false, ushort contentType = 0, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { HorseMessage msg = new HorseMessage(MessageType.Router, routerName, contentType); if (!string.IsNullOrEmpty(messageId)) { msg.SetMessageId(messageId); } else { msg.SetMessageId(_client.UniqueIdGenerator.Create()); } msg.WaitResponse = waitForAcknowledge; msg.Content = new MemoryStream(data); if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { msg.AddHeader(pair.Key, pair.Value); } } return(await _client.WaitResponse(msg, waitForAcknowledge)); }
/// <summary> /// Sends a JSON message by full name /// </summary> public async Task <HorseResult> SendJsonById <T>(string id, ushort contentType, T model, bool waitAcknowledge, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { HorseMessage message = new HorseMessage(); message.SetTarget(id); message.Type = MessageType.DirectMessage; message.ContentType = contentType; message.Serialize(model, _client.JsonSerializer); if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { message.AddHeader(pair.Key, pair.Value); } } if (waitAcknowledge) { return(await _client.SendAndGetAck(message)); } return(await _client.SendAsync(message)); }
/// <summary> /// Sends a request to target with a JSON model, waits response /// </summary> public async Task <HorseResult <TResponse> > RequestJson <TResponse>(string target, ushort?contentType, object model, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { TypeDeliveryDescriptor descriptor = _client.DeliveryContainer.GetDescriptor(model.GetType()); HorseMessage message = descriptor.CreateMessage(MessageType.DirectMessage, target, contentType); message.Serialize(model, _client.JsonSerializer); if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { message.AddHeader(pair.Key, pair.Value); } } HorseMessage responseMessage = await _client.Request(message); if (responseMessage.ContentType == 0) { TResponse response = responseMessage.Deserialize <TResponse>(_client.JsonSerializer); return(new HorseResult <TResponse>(response, message, HorseResultCode.Ok)); } return(new HorseResult <TResponse>(default, responseMessage, (HorseResultCode)responseMessage.ContentType));
/// <summary> /// Sends a memory stream message by full name /// </summary> private async Task <HorseResult> SendById(string id, ushort contentType, MemoryStream content, bool waitAcknowledge, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { HorseMessage message = new HorseMessage(); message.SetTarget(id); message.ContentType = contentType; message.Content = content; message.Type = MessageType.DirectMessage; if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { message.AddHeader(pair.Key, pair.Value); } } if (waitAcknowledge) { return(await _client.SendAndGetAck(message)); } return(await _client.SendAsync(message)); }
/// <summary> /// Sends a JSON message by full name /// </summary> public async Task <HorseResult> SendJson <T>(T model, bool waitAcknowledge, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { TypeDeliveryDescriptor descriptor = _client.DeliveryContainer.GetDescriptor(model.GetType()); HorseMessage message = descriptor.CreateMessage(MessageType.DirectMessage, null, null); if (string.IsNullOrEmpty(message.Target)) { return(new HorseResult(HorseResultCode.SendError)); } message.WaitResponse = waitAcknowledge; message.Serialize(model, _client.JsonSerializer); if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { message.AddHeader(pair.Key, pair.Value); } } if (waitAcknowledge) { return(await _client.SendAndGetAck(message)); } return(await _client.SendAsync(message)); }
/// <summary> /// Pushes a message to a queue /// </summary> public async Task <HorseResult> PushJson(string queue, object jsonObject, string messageId, bool waitAcknowledge, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { TypeDeliveryDescriptor descriptor = _client.DeliveryContainer.GetDescriptor(jsonObject.GetType()); HorseMessage message = descriptor.CreateMessage(MessageType.QueueMessage, queue, 0); if (!string.IsNullOrEmpty(messageId)) { message.SetMessageId(messageId); } message.WaitResponse = waitAcknowledge; if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { message.AddHeader(pair.Key, pair.Value); } } message.Serialize(jsonObject, _client.JsonSerializer); if (string.IsNullOrEmpty(message.MessageId) && waitAcknowledge) { message.SetMessageId(_client.UniqueIdGenerator.Create()); } return(await _client.WaitResponse(message, waitAcknowledge)); }
/// <summary> /// Publishes a JSON object to a router /// </summary> public async Task <HorseResult> PublishJson(string routerName, object model, string messageId = null, bool waitForAcknowledge = false, ushort?contentType = null, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { TypeDeliveryDescriptor descriptor = _client.DeliveryContainer.GetDescriptor(model.GetType()); HorseMessage message = descriptor.CreateMessage(MessageType.Router, routerName, contentType); if (!string.IsNullOrEmpty(messageId)) { message.SetMessageId(messageId); } else { message.SetMessageId(_client.UniqueIdGenerator.Create()); } message.WaitResponse = waitForAcknowledge; message.Serialize(model, _client.JsonSerializer); if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { message.AddHeader(pair.Key, pair.Value); } } return(await _client.WaitResponse(message, waitForAcknowledge)); }
/// <summary> /// Pushes a message to a queue /// </summary> public async Task <HorseResult> Push(string queue, MemoryStream content, string messageId, bool waitAcknowledge, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { HorseMessage message = new HorseMessage(MessageType.QueueMessage, queue, 0); message.Content = content; message.WaitResponse = waitAcknowledge; if (!string.IsNullOrEmpty(messageId)) { message.SetMessageId(messageId); } if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { message.AddHeader(pair.Key, pair.Value); } } if (string.IsNullOrEmpty(message.MessageId) && waitAcknowledge) { message.SetMessageId(_client.UniqueIdGenerator.Create()); } return(await _client.WaitResponse(message, waitAcknowledge)); }
/// <summary> /// Creates new router. /// Returns success result if router already exists. /// </summary> public async Task <HorseResult> Create(string name, RouteMethod method) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.CreateRouter; message.SetTarget(name); message.WaitResponse = true; message.AddHeader(HorseHeaders.ROUTE_METHOD, Convert.ToInt32(method).ToString()); message.SetMessageId(_client.UniqueIdGenerator.Create()); return(await _client.WaitResponse(message, true)); }
/// <summary> /// Remove a binding from a router /// </summary> public async Task <HorseResult> RemoveBinding(string routerName, string bindingName) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.RemoveBinding; message.SetTarget(routerName); message.WaitResponse = true; message.SetMessageId(_client.UniqueIdGenerator.Create()); message.AddHeader(HorseHeaders.BINDING_NAME, bindingName); return(await _client.WaitResponse(message, true)); }
/// <summary> /// Finds all queues in server /// </summary> public async Task <HorseModelResult <List <QueueInformation> > > List(string filter = null) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.SetMessageId(_client.UniqueIdGenerator.Create()); message.ContentType = KnownContentTypes.QueueList; message.AddHeader(HorseHeaders.FILTER, filter); return(await _client.SendAndGetJson <List <QueueInformation> >(message)); }
/// <summary> /// Gets all consumers of queue /// </summary> public async Task <HorseModelResult <List <ClientInformation> > > GetConsumers(string queue) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.SetTarget(queue); message.ContentType = KnownContentTypes.QueueConsumers; message.SetMessageId(_client.UniqueIdGenerator.Create()); message.AddHeader(HorseHeaders.QUEUE_NAME, queue); return(await _client.SendAndGetJson <List <ClientInformation> >(message)); }
/// <summary> /// Creates new queue in server /// </summary> public async Task <HorseResult> Create(string queue, Action <QueueOptions> optionsAction, string deliveryHandlerHeader = null, IEnumerable <KeyValuePair <string, string> > additionalHeaders = null) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.CreateQueue; message.SetTarget(queue); message.WaitResponse = true; message.AddHeader(HorseHeaders.QUEUE_NAME, queue); if (!string.IsNullOrEmpty(deliveryHandlerHeader)) { message.AddHeader(HorseHeaders.DELIVERY_HANDLER, deliveryHandlerHeader); } if (additionalHeaders != null) { foreach (KeyValuePair <string, string> pair in additionalHeaders) { message.AddHeader(pair.Key, pair.Value); } } if (optionsAction != null) { QueueOptions options = new QueueOptions(); optionsAction(options); message.Content = new MemoryStream(); await JsonSerializer.SerializeAsync(message.Content, options); } message.SetMessageId(_client.UniqueIdGenerator.Create()); return(await _client.WaitResponse(message, true)); }
public async Task PushWithCC() { TestHorseMq server = new TestHorseMq(); await server.Initialize(); int port = server.Start(300, 300); HorseClient producer = new HorseClient(); await producer.ConnectAsync("hmq://localhost:" + port); Assert.True(producer.IsConnected); HorseClient consumer1 = new HorseClient(); consumer1.ClientId = "consumer-1"; await consumer1.ConnectAsync("hmq://localhost:" + port); HorseClient consumer2 = new HorseClient(); consumer2.ClientId = "consumer-2"; await consumer2.ConnectAsync("hmq://localhost:" + port); Assert.True(consumer1.IsConnected); Assert.True(consumer2.IsConnected); int consumer1Msgs = 0; int consumer2Msgs = 0; consumer1.MessageReceived += (c, m) => { consumer1Msgs++; }; consumer2.MessageReceived += (c, m) => { consumer2Msgs++; }; HorseResult joined1 = await consumer1.Queues.Subscribe("push-a", true); Assert.Equal(HorseResultCode.Ok, joined1.Code); HorseResult joined2 = await consumer2.Queues.Subscribe("push-a-cc", true); Assert.Equal(HorseResultCode.Ok, joined2.Code); HorseMessage msg = new HorseMessage(MessageType.QueueMessage, "push-a"); msg.AddHeader(HorseHeaders.CC, "push-a-cc"); msg.SetStringContent("Hello, World!"); await producer.SendAsync(msg); await Task.Delay(1500); Assert.Equal(1, consumer1Msgs); Assert.Equal(1, consumer2Msgs); }
/// <summary> /// Request a message from Pull queue /// </summary> public async Task <PullContainer> Pull(PullRequest request, Func <int, HorseMessage, Task> actionForEachMessage = null) { HorseMessage message = new HorseMessage(MessageType.QueuePullRequest, request.Queue); message.SetMessageId(_client.UniqueIdGenerator.Create()); message.AddHeader(HorseHeaders.COUNT, request.Count); if (request.ClearAfter == ClearDecision.AllMessages) { message.AddHeader(HorseHeaders.CLEAR, "all"); } else if (request.ClearAfter == ClearDecision.PriorityMessages) { message.AddHeader(HorseHeaders.CLEAR, "High-Priority"); } else if (request.ClearAfter == ClearDecision.Messages) { message.AddHeader(HorseHeaders.CLEAR, "Default-Priority"); } if (request.GetQueueMessageCounts) { message.AddHeader(HorseHeaders.INFO, "yes"); } if (request.Order == MessageOrder.LIFO) { message.AddHeader(HorseHeaders.ORDER, HorseHeaders.LIFO); } foreach (KeyValuePair <string, string> pair in request.RequestHeaders) { message.AddHeader(pair.Key, pair.Value); } PullContainer container = new PullContainer(message.MessageId, request.Count, actionForEachMessage); lock (PullContainers) PullContainers.Add(message.MessageId, container); HorseResult sent = await _client.SendAsync(message); if (sent.Code != HorseResultCode.Ok) { lock (PullContainers) PullContainers.Remove(message.MessageId); container.Complete("Error"); } return(await container.GetAwaitableTask()); }
/// <summary> /// Sends a string request to router. /// Waits response from at least one binding. /// </summary> public async Task <HorseMessage> PublishRequest(string routerName, string message, ushort contentType = 0, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { HorseMessage msg = new HorseMessage(MessageType.Router, routerName, contentType); msg.WaitResponse = true; msg.Content = new MemoryStream(Encoding.UTF8.GetBytes(message)); if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { msg.AddHeader(pair.Key, pair.Value); } } return(await _client.Request(msg)); }
/// <summary> /// Updates queue options /// </summary> public async Task <HorseResult> SetOptions(string queue, Action <QueueOptions> optionsAction) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.UpdateQueue; message.SetTarget(queue); message.WaitResponse = true; message.SetMessageId(_client.UniqueIdGenerator.Create()); message.AddHeader(HorseHeaders.QUEUE_NAME, queue); QueueOptions options = new QueueOptions(); optionsAction(options); message.Content = new MemoryStream(); await JsonSerializer.SerializeAsync(message.Content, options); return(await _client.WaitResponse(message, true)); }
/// <summary> /// Subscribes to a queue /// </summary> public async Task <HorseResult> Subscribe(string queue, bool verifyResponse, IEnumerable <KeyValuePair <string, string> > headers = null) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.Subscribe; message.SetTarget(queue); message.WaitResponse = verifyResponse; if (headers != null) { foreach (KeyValuePair <string, string> header in headers) { message.AddHeader(header.Key, header.Value); } } if (verifyResponse) { message.SetMessageId(_client.UniqueIdGenerator.Create()); } return(await _client.WaitResponse(message, verifyResponse)); }
/// <summary> /// Applies descriptor information to the message /// </summary> public HorseMessage CreateMessage(MessageType type, string overrideTargetName, ushort?overrideContentType) { string target = overrideTargetName; ushort?contentType = overrideContentType; switch (type) { case MessageType.QueueMessage: if (string.IsNullOrEmpty(target)) { target = QueueName; } break; case MessageType.DirectMessage: if (string.IsNullOrEmpty(target)) { target = DirectTarget; } if (!contentType.HasValue) { contentType = ContentType; } break; case MessageType.Router: if (string.IsNullOrEmpty(target)) { target = RouterName; } if (!contentType.HasValue) { contentType = ContentType; } break; } HorseMessage message = new HorseMessage(type, target, contentType ?? 0); if (HighPriority) { message.HighPriority = HighPriority; } if (Acknowledge.HasValue) { switch (Acknowledge.Value) { case QueueAckDecision.None: message.AddHeader(HorseHeaders.ACKNOWLEDGE, "none"); break; case QueueAckDecision.JustRequest: message.AddHeader(HorseHeaders.ACKNOWLEDGE, "request"); break; case QueueAckDecision.WaitForAcknowledge: message.AddHeader(HorseHeaders.ACKNOWLEDGE, "wait"); break; } } if (HasQueueName) { message.AddHeader(HorseHeaders.QUEUE_NAME, QueueName); } if (QueueStatus.HasValue) { message.AddHeader(HorseHeaders.QUEUE_STATUS, QueueStatus.Value.ToString().Trim().ToLower()); } if (!string.IsNullOrEmpty(Topic)) { message.AddHeader(HorseHeaders.QUEUE_TOPIC, Topic); } if (DelayBetweenMessages.HasValue) { message.AddHeader(HorseHeaders.DELAY_BETWEEN_MESSAGES, DelayBetweenMessages.Value.ToString()); } if (PutBackDelay.HasValue) { message.AddHeader(HorseHeaders.PUT_BACK_DELAY, PutBackDelay.Value.ToString()); } foreach (KeyValuePair <string, string> pair in Headers) { message.AddHeader(pair.Key, pair.Value); } if (string.IsNullOrEmpty(target)) { throw new ArgumentNullException("Message target cannot be null"); } return(message); }