- Horse MQ is a .NET Core messaging queue and communication framework.
- Horse MQ is not only queue messaging server. In provides direct messages, requests, responses and getting server and client informations.
- Horse MQ has many queue messaging structure: Broadcast, Push, Pull, Cache.
- Clients can subscribe events and gets information when another client is connected or does something.
class Program
{
static void Main(string[] args)
{
HorseServer server = new HorseServer();
server.UseHorseMq(cfg => cfg.UseJustAllowDeliveryHandler());
server.Run(22200);
}
}
class Program
{
static async Task Main(string[] args)
{
HorseServer server = new HorseServer();
HorseMq mq = server.UseHorseMq(cfg => cfg
.AddPersistentQueues(q => q.KeepLastBackup())
.UsePersistentDeliveryHandler(DeleteWhen.AfterAcknowledgeReceived, ProducerAckDecision.AfterReceived));
await mq.LoadPersistentQueues();
server.Run(22200);
}
}
Implementation
static async Task Main(string[] args)
{
HmqStickyConnector connector = new HmqStickyConnector(TimeSpan.FromSeconds(1));
connector.AutoSubscribe = true;
connector.ContentSerializer = new NewtonsoftContentSerializer();
connector.Observer.RegisterAssemblyConsumers(typeof(Program));
connector.AddHost("tmq://127.0.0.1:22200");
connector.Run();
while (true)
await Task.Delay(1000);
}
Model
[QueueName("model-a")]
public class ModelA
{
public string Foo { get; set; }
}
Consumer
[AutoAck]
[AutoNack]
public class QueueConsumerA : IQueueConsumer<ModelA>
{
public Task Consume(HorseMessage message, ModelA model, HorseClient client)
{
Console.WriteLine("Model A Consumed");
return Task.CompletedTask;
}
}
Model and Consumer same with example above. The Implementation change is here:
services.UseHorseBus(cfg => cfg.AddHost("tmq://127.0.0.1:22200")
.AddTransientConsumers(typeof(Program)));
If you use a service provider, you can inject other services to consumer objects.
Horse accepts producers and consumers as client. Each client can be producer and consumer at same time. With ConsumerFactory implementation, you can inject IHorseBus interface for being producer at same time. If you want to create only producer, you can skip Add..Consumers methods.
IHorseQueueBus queueBus; //injected
IHorseRouteBus routeBus; //injected
IHorseDirectBus directBus; //injected
//push to a queue
await queueBus.PushJson(new ModelA());
//publish to a router
await routeBus.PublishJson(new ModelA());
//to a direct target, ModelA requires DirectTarget attribute
//and ContentType attribute will be useful to recognize message type by receiver
directBus.SendJson(new ModelA());
Thanks to JetBrains for open source license to use on this project.