Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up handlers in RedMQ from events raised in my domain

Just getting my head around message queues and Redis MQ, excellent framework.

I understand that you have to use .RegisterHandler(...) to determine which handler will process the type of message/event that is in the message queue.

So if I have EventA, EventB etc should I have one Service which handles each of those Events, like :

public class DomainService : Service {

    public object Any(EventA eventA) {...}
    public object Any(EventB eventA) {...}
}

So these should be only queue/redis list created?

Also, what If I want a chain of events to happen, so for example a message of type EventA also has a handler that sends an Email providing handlers earlier on the chain are successful?

like image 967
JD. Avatar asked Dec 12 '12 12:12

JD.


People also ask

Where do I put my domain event handlers?

I place domain event handlers in application layer. Domain event is a way to tell the outside layers (or outside world) that something happened in the domain layer. What to do with the event depends on the application. Application may notify user about changes or may call another domain to do something.

How do you raise a domain event?

The deferred approach to raise and dispatch events Instead of dispatching to a domain event handler immediately, a better approach is to add the domain events to a collection and then to dispatch those domain events right before or right after committing the transaction (as with SaveChanges in EF).


1 Answers

ServiceStack has no distinction between services created for MQ's, REST, HTML or SOAP services, they're the same thing. i.e. they each accept a Request DTO and optionally return a Response DTO and the same service can handle calls from any endpoint or format, e.g HTML, REST, SOAP or MQ.

Refer to ServiceStack's Architecture diagram to see how MQ fits in.

Limitations

The only things you need to keep in mind are:

  • Like SOAP, MQ's only support 1 Verb so your methods need to be named Post or Any
  • Only Action Filters are executed (i.e. not Global or Attribute filters)
  • You get MqRequest and MqResponse stubs in place of IHttpRequest, IHttpResponse. You can still use .Items to pass data through the request pipeline but any HTTP actions like setting cookies or HTTP Headers are benign

Configuring a Redis MQ Host

The MQ Host itself is completely decoupled from the rest of the ServiceStack framework, who doesn't know the MQ exists until you pass the message into ServiceStack yourself, which is commonly done inside your registered handler, e.g:

var redisFactory = new PooledRedisClientManager("localhost:6379");
var mqHost = new RedisMqServer(redisFactory, retryCount:2);

mqHost.RegisterHandler<Hello>(m => {
    return this.ServiceController.ExecuteMessage(m);
});

//shorter version:
//mqHost.RegisterHandler<Hello>(ServiceController.ExecuteMessage);


mqHost.Start(); //Starts listening for messages

In your RegisterHandler<T> you specify the type of Request you want it to listen for.

By default you can only Register a single handler for each message and in ServiceStack a Request is tied to a known Service implementation, in the case of MQ's it's looking for a method signature first matching: Post(Hello) and if that doesn't exist it looks for the fallback Any(Hello).

Can add multiple handlers per message yourself

If you want to invoke multiple handlers then you would just maintain your own List<Handler> and just go through and execute them all when a request comes in.

Calling different services

If you want to call a different service, just translate it to a different Request DTO and pass that to the ServiceController instead.

When a MQ Request is sent by anyone, e.g:

mqClient.Publish(new Hello { Name = "Client" });

Your handler is invoked with an instance of type IMessage where the Request DTO is contained in the Body property. At that point you can choose to discard the message, validate it or alter it.

MQ Requests are the same as any other Service requests

In most cases you would typically just forward the message on to the ServiceController to process, the implementation of which is:

public object ExecuteMessage<T>(IMessage<T> mqMsg)
{
    return Execute(mqMsg.Body, new MqRequestContext(this.Resolver, mqMsg));
}

The implementation just extracts the Request DTO from the mqMsg.Body and processes that message as a normal service being passed a C# Request DTO from that point on, with a MqRequestContext that contains the MQ IHttpRequest, IHttpResponse stubs.

like image 198
mythz Avatar answered Nov 08 '22 05:11

mythz