2019-08-17 14:19:29 +02:00
|
|
|
|
using System;
|
2021-12-10 09:56:37 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2019-08-17 14:19:29 +02:00
|
|
|
|
using ExampleLib;
|
|
|
|
|
using Messaging.TapetiExample;
|
2023-04-14 15:47:27 +02:00
|
|
|
|
using Tapeti.Config.Annotations;
|
2019-08-17 14:19:29 +02:00
|
|
|
|
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;
|
|
|
|
|
|
2022-11-23 09:13:38 +01:00
|
|
|
|
public string? FirstQuote;
|
|
|
|
|
public string? SecondQuote;
|
|
|
|
|
public string? ThirdQuote;
|
2019-08-17 14:19:29 +02: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 09:56:37 +01:00
|
|
|
|
.AddRequest<QuoteRequestMessage, QuoteResponseMessage>(
|
2019-08-17 14:19:29 +02: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]
|
2022-02-09 12:19:05 +01:00
|
|
|
|
public async ValueTask HandleSecondQuoteResponse(QuoteResponseMessage message, IFlowParallelRequest parallelRequest)
|
2019-08-17 14:19:29 +02:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] Second quote response received");
|
|
|
|
|
SecondQuote = message.Quote;
|
2021-12-10 09:56:37 +01: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 14:19:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IYieldPoint AllQuotesReceived()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] First quote: " + FirstQuote);
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] Second quote: " + SecondQuote);
|
2021-12-10 09:56:37 +01:00
|
|
|
|
Console.WriteLine("[ParallelFlowController] Third quote: " + ThirdQuote);
|
2019-08-17 14:19:29 +02:00
|
|
|
|
|
2021-12-10 11:45:09 +01:00
|
|
|
|
return flowProvider.YieldWithParallelRequest()
|
|
|
|
|
.YieldSync(ImmediateConvergeTest, FlowNoRequestsBehaviour.Converge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IYieldPoint ImmediateConvergeTest()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[ParallelFlowController] Second parallel flow immediately converged");
|
|
|
|
|
|
2019-08-17 14:19:29 +02:00
|
|
|
|
exampleState.Done();
|
|
|
|
|
return flowProvider.End();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|