MassTransit.Courier : Request/Response

476 views
Skip to first unread message

Kiran

unread,
Nov 23, 2015, 3:04:13 PM11/23/15
to masstransit-discuss
hi,


I have a client submitting orders which will be validated and ran though some more processing logic before being submitted into the Order management system. Courier-Slip offers a perfect solution for this problem ( I already implemented it), The only issue is that the client needs an acknowledgement of success or failure of the submitted order. Can you please guide me as to how I can go about providing response to the client. 



Thanks
Kiran
MT Newbie 

Chris Patterson

unread,
Nov 23, 2015, 4:12:37 PM11/23/15
to masstrans...@googlegroups.com
How is the routing slip being executed? As part of a request consumer? If you capture the RequestId from the context in the request consumer, you can set that header on the response that is sent to the original requester. The RoutingSlipCompleted event can be captured by the request consumer and used to respond back to the requester.

That's how I would manage it to fit within the original request/response structure. Are you waiting at the requester for the routing slip to complete?


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/ecb08c62-f4f3-467d-9726-7ce3890e500c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kiran

unread,
Nov 23, 2015, 4:39:55 PM11/23/15
to masstransit-discuss

Correct, OrderRequest ->OrderConsumer (will create the RoutingSlip and execute it). RoutingSlipCompleted event should solve the problem, but then I have to wait for the routing slip to complete. Any ideas.

Thanks
Kiran




Chris Patterson

unread,
Nov 23, 2015, 4:55:01 PM11/23/15
to masstrans...@googlegroups.com
Just create a consumer for the RoutingSlipCompleted, pull the variables/output from the routing slip event, and use that to send the response message to the original requester. Carry the RequestId header with the routing slip so that it can be used to set the header on the response message.

YOu could do the same for RoutingSlipFaulted.

If you dont' want to wait for the routing slip to complete, just use context.Respond() and send the response immediately.


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

Kiran

unread,
Nov 23, 2015, 5:08:27 PM11/23/15
to masstransit-discuss
One last question, how do I create a consumer for RoutingSlipCompleted. I looking into "When_an_activity_runs_to_completion" but cannot rap by head round it. 

Chris Patterson

unread,
Nov 23, 2015, 7:04:59 PM11/23/15
to masstrans...@googlegroups.com
public Consumer : IConsumer<RoutingSlipCompleted>
{
}

Must be on a receive endpoint though, as routing slip events are published until they are subscribed explicitly.

On Mon, Nov 23, 2015 at 2:08 PM, Kiran <kiran....@gmail.com> wrote:
One last question, how do I create a consumer for RoutingSlipCompleted. I looking into "When_an_activity_runs_to_completion" but cannot rap by head round it. 

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

Kiran

unread,
Nov 24, 2015, 9:13:24 AM11/24/15
to masstransit-discuss

Sorry to be a pain. But I fail to under how to link the RoutingSlipCompleted Consumer and my IApexPostRequest Consumer.  I will be receiving an order on  public Task Consume(ConsumeContext<IApexPostRequest> context) which creates the slip and executes. How do I wait and transfer the content from Consume(ConsumeContext<RoutingSlipCompleted> context) onto Consume(ConsumeContext<IApexPostRequest> and respond back to the client.

public class APEXPostConsumer : IConsumer<IApexPostRequest>, IConsumer<RoutingSlipCompleted>
    {
        static IRabbitMqHost _host;

        public Task Consume(ConsumeContext<RoutingSlipCompleted> context)
        {
            throw new NotImplementedException();
        }

        public Task Consume(ConsumeContext<IApexPostRequest> context)
        {
            string validateQueueName = ConfigurationManager.AppSettings["ValidateActivityQueue"];
            Uri validateAddress = _host.Settings.GetQueueAddress(validateQueueName);
            string retrieveQueueName = ConfigurationManager.AppSettings["PostActivityQueue"];
            Uri retrieveAddress = _host.Settings.GetQueueAddress(retrieveQueueName);

            var builder = new RoutingSlipBuilder(NewId.NextGuid());
            
            builder.AddActivity("Validate", validateAddress);
            builder.AddActivity("Post", retrieveAddress);

            Trade localObj = new Trade();
            localObj.Account = "TestAccount";
            localObj.Detail = new List<TradeDetail>();
            localObj.Detail.Add(new TradeDetail() { AccountId = "TestAccount" });

            PostArguments localarguments = new PostArguments(NewId.NextGuid(), "Test_order", new List<Trade>() { localObj });


            builder.SetVariables(localarguments);
            builder.AddSubscription(validateAddress, RoutingSlipEvents.Completed);

            RoutingSlip routingSlip = builder.Build();
            context.Publish<RoutingSlipCreated>(new 
            {
                TrackingNumber = routingSlip.TrackingNumber,
                Timestamp = routingSlip.CreateTimestamp,
            });
            return context.Execute(routingSlip);

        }              
    }


Chris Patterson

unread,
Nov 24, 2015, 10:53:26 AM11/24/15
to masstrans...@googlegroups.com

You’re very close!

 

In the SetVariables, add:

 

builder.AddVariable(“requestId”, context.RequestId);

builder.AddVariable(“responseAddress”, context.ResponseAddress);

builder.AddVariable(“faultAddress”, context.FaultAddress);

 

Now, in your consumer where the RoutingSlipCompleted is received:

 

Var requestId = context.Message.GetVariable<Guid>(“requestId”);

Var responseAddress = context.Message.GetVariable<Uri>(“responseAddress”);

 

Var endpoint = context.GetSendEndpoint(responseQueueAddress);

 

// clever use of the host, by the way!

 

Endpoint.Send(new Response()…, sendContext => sendContext.RequestId = requestId);

 

Do the same with the RoutingSlipFaulted and you’re golden.

 


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

Kiran

unread,
Nov 30, 2015, 10:16:16 AM11/30/15
to masstransit-discuss
It worked like a charm. Thank you very much.

Reply all
Reply to author
Forward
0 new messages