2019-08-17 12:19:29 +00:00
|
|
|
|
using System;
|
2021-12-10 08:56:37 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-08-17 12:19:29 +00:00
|
|
|
|
using ExampleLib;
|
|
|
|
|
using Messaging.TapetiExample;
|
|
|
|
|
using Tapeti.Annotations;
|
|
|
|
|
using Tapeti.Flow;
|
|
|
|
|
using Tapeti.Flow.Annotations;
|
|
|
|
|
|
|
|
|
|
namespace _03_FlowRequestResponse
|
|
|
|
|
{
|
|
|
|
|
[MessageController]
|
|
|
|
|
[DynamicQueue("tapeti.example.03")]
|
|
|
|
|
public class ParallelFlowController
|
|
|
|
|
{
|
|
|
|
|
private readonly IFlowProvider flowProvider;
|
|
|
|
|
private readonly IExampleState exampleState;
|
|
|
|
|
|
|
|
|
|
public string FirstQuote;
|
|
|
|
|
public string SecondQuote;
|
2021-12-10 08:56:37 +00:00
|
|
|
|
public string ThirdQuote;
|
2019-08-17 12:19:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ParallelFlowController(IFlowProvider flowProvider, IExampleState exampleState)
|
|
|
|
|
{
|
|
|
|
|
this.flowProvider = flowProvider;
|
|
|
|
|
this.exampleState = exampleState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Start]
|
|
|
|
|
public IYieldPoint StartFlow()
|
|
|
|
|
{
|
|
|
|
|
return flowProvider.YieldWithParallelRequest()
|
|
|
|
|
.AddRequestSync<QuoteRequestMessage, QuoteResponseMessage>(
|
|
|
|
|
new QuoteRequestMessage
|
|
|
|
|
{
|
|
|
|
|
Amount = 1
|
|
|
|
|
},
|
|
|
|
|
HandleFirstQuoteResponse)
|
2021-12-10 08:56:37 +00:00
|
|
|
|
.AddRequest<QuoteRequestMessage, QuoteResponseMessage>(
|
2019-08-17 12:19:29 +00:00
|
|
|
|
new QuoteRequestMessage
|
|
|
|
|
{
|
|
|
|
|
Amount = 2
|
|
|
|
|
},
|
|
|
|
|
HandleSecondQuoteResponse)
|
|
|
|
|
.YieldSync(AllQuotesReceived);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Continuation]
|
|
|
|
|
public void HandleFirstQuoteResponse(QuoteResponseMessage message)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] First quote response received");
|
|
|
|
|
FirstQuote = message.Quote;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Continuation]
|
2021-12-10 08:56:37 +00:00
|
|
|
|
public async Task HandleSecondQuoteResponse(QuoteResponseMessage message, IFlowParallelRequest parallelRequest)
|
2019-08-17 12:19:29 +00:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] Second quote response received");
|
|
|
|
|
SecondQuote = message.Quote;
|
2021-12-10 08:56:37 +00:00
|
|
|
|
|
|
|
|
|
// Example of adding a request to an ongoing parallel request
|
|
|
|
|
await parallelRequest.AddRequestSync<QuoteRequestMessage, QuoteResponseMessage>(
|
|
|
|
|
new QuoteRequestMessage
|
|
|
|
|
{
|
|
|
|
|
Amount = 3
|
|
|
|
|
},
|
|
|
|
|
HandleThirdQuoteResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Continuation]
|
|
|
|
|
public void HandleThirdQuoteResponse(QuoteResponseMessage message)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] First quote response received");
|
|
|
|
|
ThirdQuote = message.Quote;
|
2019-08-17 12:19:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IYieldPoint AllQuotesReceived()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] First quote: " + FirstQuote);
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] Second quote: " + SecondQuote);
|
2021-12-10 08:56:37 +00:00
|
|
|
|
Console.WriteLine("[ParallelFlowController] Third quote: " + ThirdQuote);
|
2019-08-17 12:19:29 +00:00
|
|
|
|
|
2021-12-10 10:45:09 +00:00
|
|
|
|
return flowProvider.YieldWithParallelRequest()
|
|
|
|
|
.YieldSync(ImmediateConvergeTest, FlowNoRequestsBehaviour.Converge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IYieldPoint ImmediateConvergeTest()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] Second parallel flow immediately converged");
|
|
|
|
|
|
2019-08-17 12:19:29 +00:00
|
|
|
|
exampleState.Done();
|
|
|
|
|
return flowProvider.End();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|