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

前端 未结 1 1928
-上瘾入骨i
-上瘾入骨i 2021-02-15 23:26

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 pro

相关标签:
1条回答
  • 2021-02-15 23:43

    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.

    0 讨论(0)
提交回复
热议问题