1
0
mirror of synced 2024-11-16 06:43:51 +00:00
Tapeti/Test/MarcoController.cs

126 lines
3.8 KiB
C#
Raw Normal View History

2016-12-07 09:19:16 +00:00
using System;
using System.Threading.Tasks;
2016-12-07 09:19:16 +00:00
using Tapeti;
using Tapeti.Annotations;
using Tapeti.Flow;
using Tapeti.Flow.Annotations;
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;
2016-12-07 09:19:16 +00:00
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
}
/**
* The Visualizer could've been injected through the constructor, which is
* the recommended way. Just testing the injection middleware here.
*/
public async Task<IYieldPoint> Marco(MarcoMessage message, Visualizer myVisualizer)
2016-12-07 09:19:16 +00:00
{
Console.WriteLine(">> Marco (yielding with request)");
2016-12-07 09:19:16 +00:00
await myVisualizer.VisualizeMarco();
StateTestGuid = Guid.NewGuid();
return flowProvider.YieldWithParallelRequest()
.AddRequestSync<PoloConfirmationRequestMessage, PoloConfirmationResponseMessage>(new PoloConfirmationRequestMessage
{
StoredInState = StateTestGuid
}, HandlePoloConfirmationResponse1)
2016-12-07 09:19:16 +00:00
.AddRequestSync<PoloConfirmationRequestMessage, PoloConfirmationResponseMessage>(new PoloConfirmationRequestMessage
{
StoredInState = StateTestGuid
}, 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.EndWithResponse(new PoloMessage());
}
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 PoloConfirmationResponseMessage PoloConfirmation(PoloConfirmationRequestMessage message)
2016-12-07 09:19:16 +00:00
{
Console.WriteLine(">> PoloConfirmation (returning confirmation)");
return new PoloConfirmationResponseMessage
{
ShouldMatchState = message.StoredInState
};
2016-12-07 09:19:16 +00:00
}
public void Polo(PoloMessage message)
{
Console.WriteLine(">> Polo");
}
2016-12-07 09:19:16 +00:00
}
[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
{
public Guid StoredInState { get; set; }
2016-12-07 09:19:16 +00:00
}
public class PoloConfirmationResponseMessage
{
public Guid ShouldMatchState { get; set; }
2016-12-07 09:19:16 +00:00
}
}