1
0
mirror of synced 2024-11-24 01:33:50 +00:00
Tapeti/Test/MarcoController.cs

188 lines
5.7 KiB
C#
Raw Normal View History

2016-12-07 09:19:16 +00:00
using System;
2017-02-12 20:43:30 +00:00
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
2016-12-07 09:19:16 +00:00
using Tapeti.Annotations;
using Tapeti.Flow;
using Tapeti.Flow.Annotations;
2016-12-07 09:19:16 +00:00
// ReSharper disable UnusedMember.Global
2016-12-07 09:19:16 +00:00
namespace Test
{
[MessageController]
2016-12-07 09:19:16 +00:00
[DynamicQueue]
public class MarcoController
2016-12-07 09:19:16 +00:00
{
//private readonly IPublisher publisher;
private readonly IFlowProvider flowProvider;
//private readonly Visualizer visualizer;
2016-12-07 09:19:16 +00:00
// Public properties are automatically stored and retrieved while in a flow
public Guid StateTestGuid { get; set; }
2016-12-07 09:19:16 +00:00
public int Phase;
public MarcoController(/*IPublisher publisher, */IFlowProvider flowProvider/*, Visualizer visualizer*/)
2016-12-07 09:19:16 +00:00
{
//this.publisher = publisher;
this.flowProvider = flowProvider;
//this.visualizer = visualizer;
2016-12-07 09:19:16 +00:00
}
[Start]
public async Task<IYieldPoint> StartFlow(bool go)
{
Console.WriteLine("Phase = " + Phase + " Starting stand-alone flow");
await Task.Delay(10);
Phase = 1;
if (go)
return flowProvider.YieldWithRequestSync<PoloConfirmationRequestMessage, PoloConfirmationResponseMessage>
(new PoloConfirmationRequestMessage(),
HandlePoloConfirmationResponse);
Console.WriteLine("Phase = " + Phase + " Ending stand-alone flow prematurely");
return flowProvider.End();
}
[Continuation]
public IYieldPoint HandlePoloConfirmationResponse(PoloConfirmationResponseMessage msg)
{
Console.WriteLine("Phase = " + Phase + " Handling the first response and sending the second...");
Phase = 2;
return flowProvider.YieldWithRequestSync<PoloConfirmationRequestMessage, PoloConfirmationResponseMessage>
(new PoloConfirmationRequestMessage(),
HandlePoloConfirmationResponseEnd);
}
[Continuation]
public IYieldPoint HandlePoloConfirmationResponseEnd(PoloConfirmationResponseMessage msg)
{
Console.WriteLine("Phase = " + Phase + " Handling the second response and Ending stand-alone flow");
return flowProvider.End();
}
[Start]
public IYieldPoint TestParallelRequest()
2016-12-07 09:19:16 +00:00
{
Console.WriteLine(">> Marco (yielding with request)");
2016-12-07 09:19:16 +00:00
StateTestGuid = Guid.NewGuid();
Console.WriteLine($"Starting parallel request with StateTestGuid {StateTestGuid}");
return flowProvider.YieldWithParallelRequest()
.AddRequestSync<PoloConfirmationRequestMessage, PoloConfirmationResponseMessage>(new PoloConfirmationRequestMessage
{
StoredInState = StateTestGuid,
EnumValue = TestEnum.Value1,
}, HandlePoloConfirmationResponse1)
2016-12-07 09:19:16 +00:00
.AddRequestSync<PoloConfirmationRequestMessage, PoloConfirmationResponseMessage>(new PoloConfirmationRequestMessage
{
StoredInState = StateTestGuid,
EnumValue = TestEnum.Value2,
OptionalEnumValue = TestEnum.Value1
}, HandlePoloConfirmationResponse2)
.YieldSync(ContinuePoloConfirmation);
}
2016-12-07 09:19:16 +00:00
[Continuation]
public void HandlePoloConfirmationResponse1(PoloConfirmationResponseMessage message)
{
Console.WriteLine(">> HandlePoloConfirmationResponse1");
Console.WriteLine(message.ShouldMatchState.Equals(StateTestGuid) ? "Confirmed!" : "Oops! Mismatch!");
}
[Continuation]
public void HandlePoloConfirmationResponse2(PoloConfirmationResponseMessage message)
{
Console.WriteLine(">> HandlePoloConfirmationResponse2");
Console.WriteLine(message.ShouldMatchState.Equals(StateTestGuid) ? "Confirmed!" : "Oops! Mismatch!");
}
private IYieldPoint ContinuePoloConfirmation()
{
Console.WriteLine("> ConvergePoloConfirmation (ending flow)");
return flowProvider.End();
}
2016-12-07 09:19:16 +00:00
/**
* For simple request response patterns, the return type can be used.
* This will automatically include the correlationId in the response and
* use the replyTo header of the request if provided.
*/
public async Task<PoloConfirmationResponseMessage> PoloConfirmation(PoloConfirmationRequestMessage message)
2016-12-07 09:19:16 +00:00
{
Console.WriteLine(">> PoloConfirmation (returning confirmation)");
await Task.Delay(100);
return new PoloConfirmationResponseMessage
{
ShouldMatchState = message.StoredInState,
EnumValue = message.EnumValue,
OptionalEnumValue = message.OptionalEnumValue
};
2016-12-07 09:19:16 +00:00
}
2018-08-31 17:17:56 +00:00
[DynamicQueue("custom.prefix")]
public void Polo(PoloMessage message)
{
Console.WriteLine(">> Polo");
}
2016-12-07 09:19:16 +00:00
}
public enum TestEnum
{
Value1,
Value2
}
[Request(Response = typeof(PoloMessage))]
2016-12-07 09:19:16 +00:00
public class MarcoMessage
{
}
public class PoloMessage
{
}
[Request(Response = typeof(PoloConfirmationResponseMessage))]
public class PoloConfirmationRequestMessage
2016-12-07 09:19:16 +00:00
{
2017-02-12 20:43:30 +00:00
[Required]
public Guid StoredInState { get; set; }
public TestEnum EnumValue;
public TestEnum? OptionalEnumValue;
2016-12-07 09:19:16 +00:00
}
public class PoloConfirmationResponseMessage
{
2017-02-12 20:43:30 +00:00
[Required]
public Guid ShouldMatchState { get; set; }
public TestEnum EnumValue;
public TestEnum? OptionalEnumValue;
2016-12-07 09:19:16 +00:00
}
}