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

五迷三道 提交于 2020-01-12 07:26:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13839971/how-to-set-up-handlers-in-redmq-from-events-raised-in-my-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!