/// <summary> /// Creates new HorseResult with a model /// </summary> public HorseResult(TModel model, HorseMessage message, HorseResultCode code) : base(code) { Model = model; Message = message; if (code != HorseResultCode.Ok) { if (message.Content != null && message.Length > 0) { Reason = message.GetStringContent(); } } }
public async Task PullOrder(bool?fifo) { TestHorseMq server = new TestHorseMq(); await server.Initialize(); int port = server.Start(); HorseQueue queue = server.Server.FindQueue("pull-a"); await queue.Push("First Message"); await queue.Push("Second Message"); HorseClient client = new HorseClient(); await client.ConnectAsync("hmq://localhost:" + port); HorseResult joined = await client.Queues.Subscribe("pull-a", true); Assert.Equal(HorseResultCode.Ok, joined.Code); PullRequest request = new PullRequest { Queue = "pull-a", Count = 1, Order = !fifo.HasValue || fifo.Value ? MessageOrder.FIFO : MessageOrder.LIFO }; PullContainer container = await client.Queues.Pull(request); Assert.Equal(PullProcess.Completed, container.Status); HorseMessage msg = container.ReceivedMessages.FirstOrDefault(); Assert.NotNull(msg); string content = msg.GetStringContent(); if (fifo.HasValue && !fifo.Value) { Assert.Equal("Second Message", content); } else { Assert.Equal("First Message", content); } }
/// <summary> /// Deserializes message content and returns the object /// </summary> /// <param name="message">Message</param> /// <param name="type">Model type</param> public object Deserialize(HorseMessage message, Type type) { if (message.Content == null) { return(null); } string content = message.GetStringContent(); if (string.IsNullOrEmpty(content)) { return(null); } var model = _settings != null ? JsonConvert.DeserializeObject(content, type, _settings) : JsonConvert.DeserializeObject(content, type); return(model); }
public async Task SingleQueueSingleDirectResponseFromDirect() { TestHorseMq server = new TestHorseMq(); await server.Initialize(); int port = server.Start(300, 300); Router router = new Router(server.Server, "router", RouteMethod.Distribute); router.AddBinding(new QueueBinding("qbind-1", "push-a", 0, BindingInteraction.None)); router.AddBinding(new DirectBinding("dbind-1", "client-1", 0, BindingInteraction.Response)); server.Server.AddRouter(router); HorseClient producer = new HorseClient(); await producer.ConnectAsync("hmq://localhost:" + port); Assert.True(producer.IsConnected); HorseClient client1 = new HorseClient(); client1.ClientId = "client-1"; await client1.ConnectAsync("hmq://localhost:" + port); client1.MessageReceived += (c, m) => { HorseMessage response = m.CreateResponse(HorseResultCode.Ok); response.SetStringContent("Response"); client1.SendAsync(response); }; Assert.True(client1.IsConnected); HorseQueue queue1 = server.Server.FindQueue("push-a"); HorseMessage message = await producer.Routers.PublishRequest("router", "Hello, World!"); Assert.NotNull(message); Assert.Equal("Response", message.GetStringContent()); Assert.Equal(1, queue1.MessageCount()); }
/// <summary> /// When a message received to Horse Client, this method will be called /// </summary> private async void ClientOnMessageReceived(ClientSocketBase <HorseMessage> client, HorseMessage message) { try { ReadSource source; if (message.Type == MessageType.QueueMessage) { if (string.IsNullOrEmpty(message.Target)) { return; } source = ReadSource.Queue; } else if (message.Type == MessageType.DirectMessage) { source = message.WaitResponse ? ReadSource.Request : ReadSource.Direct; } else { return; } //find all subscriber actions List <ReadSubscription> subs = null; lock (_subscriptions) { switch (source) { case ReadSource.Queue: subs = _subscriptions.Where(x => x.Source == ReadSource.Queue && x.Queue.Equals(message.Target, StringComparison.InvariantCultureIgnoreCase)).ToList(); break; case ReadSource.Direct: subs = _subscriptions.Where(x => x.Source == ReadSource.Direct && x.ContentType == message.ContentType).ToList(); break; case ReadSource.Request: subs = _subscriptions.Where(x => x.Source == ReadSource.Request && x.ContentType == message.ContentType).ToList(); //direct consumer waits for ack if (subs.Count == 0) { subs = _subscriptions.Where(x => x.Source == ReadSource.Direct && x.ContentType == message.ContentType).ToList(); } break; } } if (subs == null || subs.Count == 0) { return; } //convert model, only one time to first susbcriber's type Type type = subs[0].MessageType; object model = type == typeof(string) ? message.GetStringContent() : _func(message, type); //call all subscriber methods if they have same type foreach (ReadSubscription sub in subs) { if (sub.MessageType == null || sub.MessageType == type) { try { if (sub.ConsumerExecuter != null) { await sub.ConsumerExecuter.Execute((HorseClient)client, message, model); } if (sub.Action != null) { if (sub.HmqMessageParameter) { if (sub.MessageType == null) { sub.Action.DynamicInvoke(message); } else { sub.Action.DynamicInvoke(model, message); } } else { sub.Action.DynamicInvoke(model); } } } catch (Exception ex) { OnException?.Invoke(message, ex); } } } } catch (Exception ex) { OnException?.Invoke(message, ex); } }