Reputation: 3070
I'm trying to figure out how to implement pub/sub using ServiceStack MQ abstraction
Let's say I have a publisher app publishing a Hello request that will have n subscribers (different apps)
// Publisher
namespace Publisher
{
public class RabbitPublisherAppHost : AppHostHttpListenerBase
{
public RabbitPublisherAppHost() : base("Rabbit Publisher Server", typeof(MainClass).Assembly) { }
public override void Configure(Container container)
{
Routes
.Add<Publish>("/publish/{Text}");
container.Register<IMessageService>(c => new RabbitMqServer());
var mqServer = container.Resolve<IMessageService>();
mqServer.Start();
}
}
}
namespace Publisher
{
public class PublishService : Service
{
public IMessageService MessageService { get; set; }
public void Any(Publish request)
{
PublishMessage(new Hello{Id=request.Text});
}
}
}
than I create the first subscriber
// Sub1
public class RabbitSubscriberAppHost : AppHostHttpListenerBase
{
public RabbitSubscriberAppHost() : base("Rabbit Subscriber 1", typeof(MainClass).Assembly) { }
public override void Configure(Container container)
{
container.Register<RabbitMqServer>(c => new RabbitMqServer());
var mqServer = container.Resolve<RabbitMqServer>();
mqServer.RegisterHandler<Hello>(ServiceController.ExecuteMessage, noOfThreads: 3);
mqServer.Start();
}
}
namespace Subscriber1
{
public class HelloService : Service
{
public object Any(Hello req)
{
//..
}
}
}
Now If I create a similar app acting as the second subscriber, the 2 subscribers are sharing the same queue, so instead of the pub/sub a race condition is happening. In other word, what has to be done to implement a registration for each subscribers? I'd like all subscribers recevive the Hello request published, not just one of them according to a race condition.
Upvotes: 0
Views: 1041
Reputation: 143339
ServiceStack's Messaging API follows the Services Request / Reply pattern (i.e. essentially using MQ instead of HTTP transport) and doesn't support Pub/Sub itself.
For RabbitMQ Pub/Sub you would have to implement it outside of the IMessaging
API abstraction, e.g. publishing it to a Rabbit MQ topic from within your Service implementation.
Related ServiceStack does include a Pub/Sub library using Redis. Also depending on your use-case you may be able to make use of notifying multiple subscribers with Server Events .
Upvotes: 2