使用Rhino Service Bus。我有一个处理处理的后端应用程序,并有另一个应用程序(客户端UI)将消息发布到后端。我在后端有一个Saga,在此期间我希望saga向自己发布消息,将处理分解为可以在自己的线程上运行的多个较小的任务。问题是如果通过Orchestrates接口订阅消息,则总是丢弃消息。我可以使用ConsumerOf在不同的类中订阅,并且消费者获取消息。
namespace Sagas
{
public class MoveJobSaga: ISaga<MoveJobState>,
InitiatedBy<TriggerMoveJobCommand>,
Orchestrates<TriggerMoveTerminalCommand>
{
private readonly IServiceBus _bus;
private readonly ITerminalFilesService _tfService;
public MoveJobSaga(IServiceBus bus)
{
_bus = bus;
State = new MoveJobState();
}
public MoveJobState State { get; set; }
public Guid Id { get; set; }
public bool IsCompleted { get; set; }
public void Consume(TriggerMoveJobCommand message)
{
State.TerminalsToProcess = message.Terminals.Count();
State.JobId = message.JobId;
foreach (var terminal in message.Terminals)
{
_bus.Publish(new TriggerMoveTerminalCommand()
{
CorrelationId = message.CorrelationId,
Name = terminal.Name
});
}
}
public void Consume(TriggerMoveTerminalCommand message)
{
var result = _tfService.MoveTerminalFiles(message.SourceTifDir, message.TargetTifDir, message.SourceDatDir, message.TargetDatDir);
State.TerminalsProcessed++;
if (State.TerminalsToProcess == State.TerminalsProcessed)
{
_bus.Publish(new MoveJobCompletedEvent()
{
Success = State.Success,
JobId = State.JobId });
}
}
}
public class MoveJobState
{
public MoveJobState()
{
Success = true;
}
public int TerminalsToProcess { get; set; }
public int TerminalsProcessed { get; set; }
public int JobId { get; set; }
public bool Success { get; set; }
}
}
主机配置:
<rhino.esb>
<bus threadCount="1" numberOfRetries="5" endpoint="msmq://localhost/myapp.host" />
<messages />
</rhino.esb>
自举:
public class HostBootStrapper: StructureMapBootStrapper
{
protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.Configure(sm =>
{
sm.For<ISagaPersister<MoveJobSaga>>().Use<InMemorySagaPersister<MoveJobSaga>>();
sm.Scan(x =>
{
x.TheCallingAssembly();
x.WithDefaultConventions();
});
});
}
}
答案 0 :(得分:0)
我需要将ISagaPersister注册为单身人士:
sm.For<ISagaPersister<MoveJobSaga>>()
.Singleton()
.Use<InMemorySagaPersister<MoveJobSaga>>();
此外,我不得不将该传奇所使用的所有消息发布到该传奇之外。所以我创建了一个服务类,它触发了启动消息,然后是作业中的每条消息。