RipplesMQ is a simple Message Broker, this library contain the client library, use Grumpy.RipplesMQ.Server to setup RipplesMQ Server. See Grumpy.RipplesMQ.Sample for example.
This library makes it easy to use the RipplesMQ Message Broker, both for setting up handlers in services and for using the message broker to publish messages or call requests.
Protocol for using RipplesMQ Client (The Message Bus):
- Build Message Bus
- Setup handlers (Subscribers and Request Handlers)
- Start Message Bus
- Publish and Request from the Message Bus
- Dispose Message Bus
MessageBus messageBus = new MessageBusBuilder().WithServiceName("MyService");
ServiceName is defaulted to name of current process.
public void SetupSubscriber(IMessageBus messageBus)
{
var config = new PublishSubscribeConfig { Persistent = false, Topic = "MyTopicA" };
messageBus.SubscribeHandler<MyDtoA>(config, MyTopicAHandler, "MySubscriberName");
}
public void MyTopicAHandler(MyDtoA dto, CancellationToken token)
{
// Do stuff with dto
}
The subscribe handler method can be static or member of a class, the CancellationToken is obtional. It is recommented to define the DTO and config class in the API Definition of your service
public void SetupRequestHandler(IMessageBus messageBus)
{
var config = new RequestResponseConfig { Name = "MyRequesterA", MillisecondsTimeout = 100 };
messageBus.RequestHandler<MyRequestDtoA, MyResponseDtoA>(config, MyRequestAHandler);
}
public MyResponseDtoA MyRequestAHandler(MyRequestDtoA dto, CancellationToken token)
{
// Do stuff with dto
return new MyResponseDtoA();
}
The request handler method can be static or member of a class, the CancellationToken is obtional. It is recommented to define the DTO and config class in the API Definition of your service
messageBus.Start(cancellationToken);
var config = new PublishSubscribeConfig { Persistent = false, Topic = "MyTopicB" };
messageBus.Publish(config, new MyDtoB());
var config = new RequestResponseConfig { Name = "MyRequesterA", MillisecondsTimeout = 100 };
var message = messageBus.RequestAsync<MyRequestDtoB, MyResponseDtoB>(config, new MyRequestDtoB());
var response = message.Result;
Result of RequestAsync is a Task, to get response get Result from the task. There is also a synchronous version just called Request.
messageBus.Stop();
messageBus.Dispose();
Dispose will also stop, so no actual need to call Stop.