Skip to content

Client library for using the RipplesMQ Message Broker Server

License

Notifications You must be signed in to change notification settings

GrumpyBusted/Grumpy.RipplesMQ.Client

Repository files navigation

Build status codecov nuget downloads

Grumpy.RipplesMQ.Client

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):

  1. Build Message Bus
  2. Setup handlers (Subscribers and Request Handlers)
  3. Start Message Bus
  4. Publish and Request from the Message Bus
  5. Dispose Message Bus

Create Message Bus

MessageBus messageBus = new MessageBusBuilder().WithServiceName("MyService");

ServiceName is defaulted to name of current process.

Setup Subscribe handler

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

Setup Request handler

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

Start Message Bus

messageBus.Start(cancellationToken);

Publish a Message for a Subscriber

var config = new PublishSubscribeConfig { Persistent = false, Topic = "MyTopicB" };

messageBus.Publish(config, new MyDtoB());

Invoke Request Handler

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.

Stop Message Bus

messageBus.Stop();
messageBus.Dispose();

Dispose will also stop, so no actual need to call Stop.