1
0
mirror of synced 2024-11-22 01:13:49 +00:00

Implemented adding requests to parallel flow

Fixed console logger outputting incorrect message when declaring queues
This commit is contained in:
Mark van Renswoude 2021-12-10 09:56:37 +01:00
parent 2b4dd8e251
commit bc00d476bd
7 changed files with 60 additions and 64 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Threading.Tasks;
using ExampleLib; using ExampleLib;
using Messaging.TapetiExample; using Messaging.TapetiExample;
using Tapeti.Annotations; using Tapeti.Annotations;
@ -16,6 +17,7 @@ namespace _03_FlowRequestResponse
public string FirstQuote; public string FirstQuote;
public string SecondQuote; public string SecondQuote;
public string ThirdQuote;
public ParallelFlowController(IFlowProvider flowProvider, IExampleState exampleState) public ParallelFlowController(IFlowProvider flowProvider, IExampleState exampleState)
@ -35,7 +37,7 @@ namespace _03_FlowRequestResponse
Amount = 1 Amount = 1
}, },
HandleFirstQuoteResponse) HandleFirstQuoteResponse)
.AddRequestSync<QuoteRequestMessage, QuoteResponseMessage>( .AddRequest<QuoteRequestMessage, QuoteResponseMessage>(
new QuoteRequestMessage new QuoteRequestMessage
{ {
Amount = 2 Amount = 2
@ -54,10 +56,26 @@ namespace _03_FlowRequestResponse
[Continuation] [Continuation]
public void HandleSecondQuoteResponse(QuoteResponseMessage message) public async Task HandleSecondQuoteResponse(QuoteResponseMessage message, IFlowParallelRequest parallelRequest)
{ {
Console.WriteLine("[ParallelFlowController] Second quote response received"); Console.WriteLine("[ParallelFlowController] Second quote response received");
SecondQuote = message.Quote; SecondQuote = message.Quote;
// 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;
} }
@ -65,6 +83,7 @@ namespace _03_FlowRequestResponse
{ {
Console.WriteLine("[ParallelFlowController] First quote: " + FirstQuote); Console.WriteLine("[ParallelFlowController] First quote: " + FirstQuote);
Console.WriteLine("[ParallelFlowController] Second quote: " + SecondQuote); Console.WriteLine("[ParallelFlowController] Second quote: " + SecondQuote);
Console.WriteLine("[ParallelFlowController] Third quote: " + ThirdQuote);
exampleState.Done(); exampleState.Done();
return flowProvider.End(); return flowProvider.End();

View File

@ -25,8 +25,7 @@ namespace _03_FlowRequestResponse
break; break;
default: default:
// We have to return a response. quote = new string('\'', message.Amount);
quote = null;
break; break;
} }

View File

@ -40,16 +40,10 @@ namespace Tapeti.Flow.Default
// Remove Continuation now because the IYieldPoint result handler will store the new state // Remove Continuation now because the IYieldPoint result handler will store the new state
flowContext.FlowState.Continuations.Remove(flowContext.ContinuationID); flowContext.FlowState.Continuations.Remove(flowContext.ContinuationID);
var converge = flowContext.FlowState.Continuations.Count == 0 &&
flowContext.ContinuationMetadata.ConvergeMethodName != null;
if (converge)
// Indicate to the FlowBindingMiddleware that the state must not to be stored
flowPayload.FlowIsConverging = true;
await next(); await next();
if (converge) if (flowPayload.FlowIsConverging)
await CallConvergeMethod(context, controllerPayload, await CallConvergeMethod(context, controllerPayload,
flowContext.ContinuationMetadata.ConvergeMethodName, flowContext.ContinuationMetadata.ConvergeMethodName,
flowContext.ContinuationMetadata.ConvergeMethodSync); flowContext.ContinuationMetadata.ConvergeMethodSync);

View File

@ -249,19 +249,19 @@ namespace Tapeti.Flow.Default
if (!context.MessageContext.TryGet<FlowMessageContextPayload>(out var flowPayload)) if (!context.MessageContext.TryGet<FlowMessageContextPayload>(out var flowPayload))
return null; return null;
return new ParallelRequest(config, SendRequest, flowPayload.FlowContext);
} }
private delegate Task SendRequestFunc(FlowContext context,
object message,
ResponseHandlerInfo responseHandlerInfo,
string convergeMethodName,
bool convergeMethodSync);
private class ParallelRequestBuilder : IFlowParallelRequestBuilder private class ParallelRequestBuilder : IFlowParallelRequestBuilder
{ {
public delegate Task SendRequestFunc(FlowContext context,
object message,
ResponseHandlerInfo responseHandlerInfo,
string convergeMethodName,
bool convergeMethodSync);
private class RequestInfo private class RequestInfo
{ {
public object Message { get; set; } public object Message { get; set; }
@ -283,41 +283,23 @@ namespace Tapeti.Flow.Default
public IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler) public IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler)
{ {
requests.Add(new RequestInfo return InternalAddRequest(message, responseHandler);
{
Message = message,
ResponseHandlerInfo = GetResponseHandlerInfo(config, message, responseHandler)
});
return this;
} }
public IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler) public IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler)
{ {
requests.Add(new RequestInfo return InternalAddRequest(message, responseHandler);
{
Message = message,
ResponseHandlerInfo = GetResponseHandlerInfo(config, message, responseHandler)
});
return this;
} }
public IFlowParallelRequestBuilder AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler) public IFlowParallelRequestBuilder AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler)
{ {
requests.Add(new RequestInfo return InternalAddRequest(message, responseHandler);
{
Message = message,
ResponseHandlerInfo = GetResponseHandlerInfo(config, message, responseHandler)
});
return this;
} }
public IFlowParallelRequestBuilder AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse, IFlowParallelRequest> responseHandler) public IFlowParallelRequestBuilder InternalAddRequest(object message, Delegate responseHandler)
{ {
requests.Add(new RequestInfo requests.Add(new RequestInfo
{ {
@ -368,32 +350,39 @@ namespace Tapeti.Flow.Default
{ {
private readonly ITapetiConfig config; private readonly ITapetiConfig config;
private readonly SendRequestFunc sendRequest; private readonly SendRequestFunc sendRequest;
private readonly FlowContext flowContext;
public ParallelRequestBuilder(ITapetiConfig config, SendRequestFunc sendRequest) public ParallelRequest(ITapetiConfig config, SendRequestFunc sendRequest, FlowContext flowContext)
{ {
this.config = config; this.config = config;
this.sendRequest = sendRequest; this.sendRequest = sendRequest;
this.flowContext = flowContext;
} }
public IFlowParallelRequest AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler)
public Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler)
{ {
throw new NotImplementedException(); return InternalAddRequest(message, responseHandler);
} }
public IFlowParallelRequest AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler)
public Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler)
{ {
throw new NotImplementedException(); return InternalAddRequest(message, responseHandler);
} }
public IFlowParallelRequest AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler)
public Task AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler)
{ {
throw new NotImplementedException(); return InternalAddRequest(message, responseHandler);
} }
public IFlowParallelRequest AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse, IFlowParallelRequest> responseHandler)
private Task InternalAddRequest(object message, Delegate responseHandler)
{ {
throw new NotImplementedException(); var responseHandlerInfo = GetResponseHandlerInfo(config, message, responseHandler);
return sendRequest(flowContext, message, responseHandlerInfo, flowContext.ContinuationMetadata.ConvergeMethodName, flowContext.ContinuationMetadata.ConvergeMethodSync);
} }
} }

View File

@ -16,7 +16,9 @@ namespace Tapeti.Flow
/// parallel flow is done and the convergeMethod will be called. /// parallel flow is done and the convergeMethod will be called.
/// Temporarily disables storing the flow state. /// Temporarily disables storing the flow state.
/// </summary> /// </summary>
public bool FlowIsConverging { get; set; } public bool FlowIsConverging => FlowContext != null &&
FlowContext.FlowState.Continuations.Count == 0 &&
FlowContext.ContinuationMetadata.ConvergeMethodName != null;
public FlowMessageContextPayload(FlowContext flowContext) public FlowMessageContextPayload(FlowContext flowContext)

View File

@ -192,14 +192,14 @@ namespace Tapeti.Flow
/// </summary> /// </summary>
/// <param name="message"></param> /// <param name="message"></param>
/// <param name="responseHandler"></param> /// <param name="responseHandler"></param>
Task<IFlowParallelRequest> AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler); Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler);
/// <remarks> /// <remarks>
/// This overload allows the response handler access to the IFlowParallelRequest interface, which /// This overload allows the response handler access to the IFlowParallelRequest interface, which
/// can be used to add additional requests to the parallel request before the continuation method passed to the Yield method is called. /// can be used to add additional requests to the parallel request before the continuation method passed to the Yield method is called.
/// </remarks> /// </remarks>
/// <inheritdoc cref="AddRequest{TRequest,TResponse}(TRequest,Func{TResponse,Task})"/> /// <inheritdoc cref="AddRequest{TRequest,TResponse}(TRequest,Func{TResponse,Task})"/>
Task<IFlowParallelRequest> AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler); Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler);
/// <summary> /// <summary>
/// Publish a request message and continue the flow when the response arrives. /// Publish a request message and continue the flow when the response arrives.
@ -209,14 +209,7 @@ namespace Tapeti.Flow
/// </summary> /// </summary>
/// <param name="message"></param> /// <param name="message"></param>
/// <param name="responseHandler"></param> /// <param name="responseHandler"></param>
Task<IFlowParallelRequest> AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler); Task AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler);
/// <remarks>
/// This overload allows the response handler access to the IFlowParallelRequest interface, which
/// can be used to add additional requests to the parallel request before the continuation method passed to the Yield method is called.
/// </remarks>
/// <inheritdoc cref="AddRequestSync{TRequest,TResponse}(TRequest,Action{TResponse})"/>
Task<IFlowParallelRequest> AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse, IFlowParallelRequest> responseHandler);
} }

View File

@ -76,8 +76,8 @@ namespace Tapeti.Default
public void QueueDeclare(string queueName, bool durable, bool passive) public void QueueDeclare(string queueName, bool durable, bool passive)
{ {
Console.WriteLine(passive Console.WriteLine(passive
? $"[Tapeti] Declaring {(durable ? "durable" : "dynamic")} queue {queueName}" ? $"[Tapeti] Verifying durable queue {queueName}"
: $"[Tapeti] Verifying durable queue {queueName}"); : $"[Tapeti] Declaring {(durable ? "durable" : "dynamic")} queue {queueName}");
} }
/// <inheritdoc /> /// <inheritdoc />