我有一个nservice总线项目,我将其称为连接器。我的连接器接收各种消息,例如ClientChangeMessage,ClientContactChangeMessage。我的连接器没有实现saga所以我有每个消息的处理程序,所以对于ClientChangeMessage我有ClientChangeMessageHandler,当连接器收到ClientChangeMessage时会被触发,而当我收到ClientContactChangeMessage时会触发ClientContactChangeMessageHandler。
现在,在查看sagas实现时,我发现自己编写了以下代码(如果客户端联系消息出现在ClientChange消息之前,即数据库中不存在客户端):
public class ClientContactChangeMessageHandler : ClientMessageHandler,
IHandleMessages<ClientContactChangeMessage>,
IAmStartedByMessages<ClientContactChangeMessage>,
IHandleMessages<ClientChangeMessage>
{
[SetterProperty]
public IClientContactChangeDb ClientContactChangeDb{get;set;}
[SetterProperty]
public IBusRefTranslator BusRefTranslator{get;set;}
static ClientContactChangeMessageHandler()
{
Logger = LogManager.GetLogger(typeof (ClientContactChangeMessageHandler));
}
static ILog Logger;
public void Handle(ClientContactChangeMessage message)
{
//Some handling logic
}
public void Handle(ClientChangeMessage message)
{
throw new NotImplementedException();
}
public override void ConfigureHowToFindSaga()
{
ConfigureMapping<ClientContactChangeMessage>(s => s.Id, m => m.Id);
ConfigureMapping<ClientChangeMessage>(s => s.Id, m => m.Id);
// Notice that we have no mappings for the OrderAuthorizationResponseMessage message. This is not needed since the HR
// endpoint will do a Bus.Reply and NServiceBus will then automatically correlate the reply back to
// the originating saga
}
}
public class ClientMessageHandler : BaseMessageHandler
{
}
public class BaseMessageHandler : Saga<MySagaData>
{
}
public class MySagaData : IContainSagaData
{
public Guid Id { get; set; }
public string Originator { get; set; }
public string OriginalMessageId { get; set; }
}
从示例中可以看出我现在必须实现CLientChangeMessage的句柄,现在我已经为我的ClientChangeMessage定义了一个处理程序,我是否必须在这里再次处理它,因为如果进一步及时ClientChangeMessage确实来了,我希望ClientChangeMessageHandler能够捕获并处理它,而不是这个。
当且仅当我在数据库中找不到客户端的本地引用时,我想存储一条消息。看看网上传奇的例子,我看不出任何特定的地方或条件会被处理。我希望我将消息存储在ClientContactChange句柄方法中。
非常感谢任何帮助,
由于
更新:
似乎我没有正确理解如何实施NService Bus Saga。根据我的说法,我在这里犯的错误是我认为客户联系人更改为单个实体,即独立于客户更改消息。因此,我认为我实施Saga只是为了客户联系变更我做错了。以下是我必须更改代码的方法:
public class ClientSaga : Saga<ClientSagaState>,
IAmStartedByMessages<ClientChangeMessage>,
IAmStartedByMessages<ClientContactChangeMessage>,
IHandleTimeout<ClientSagaState>
{
[SetterProperty]
public IClientContactChangeDb ClientContactChangeDb{get;set;}
[SetterProperty]
public IBusRefTranslator BusRefTranslator{get;set;}
public void Handle(ClientContactChangeMessage message)
{
//Some handling logic
//Check if client is not in database then store the state
this.ClientContactChange=message;
//if client is in the data base then
MarkAsComplete();
}
public void Handle(ClientChangeMessage message)
{
//Update or create the client depending on the situation
//check for dependencies
if(this.ClientContactChange !=null)
{
//Handle the contact change
}
}
public override void ConfigureHowToFindSaga()
{
ConfigureMapping<ClientContactChangeMessage>(s => s.ClientRef, m => m.ClientRef);
ConfigureMapping<ClientChangeMessage>(s => s.ClienttnRef, m => m.Id);
// Notice that we have no mappings for the OrderAuthorizationResponseMessage message. This is not needed since the HR
// endpoint will do a Bus.Reply and NServiceBus will then automatically correlate the reply back to
// the originating saga
}
}
public class ClientSagaState: IContainSagaData
{
//i dont need these three fields
public Guid Id { get; set; }
public string Originator { get; set; }
public string OriginalMessageId { get; set; }
// the fields which i needed
public Guid ClientRef {gee; set;}
public ClientChangeMessage ClientChange {get;set;}
public ClientContactChange ClientContactChange {get;set;}
}
答案 0 :(得分:1)
由于两个处理程序都处理相同的消息类型,因此将调用它们。如果您愿意,可以使用ISpecifyMessageHandlerOrdering指定调用它们的顺序。此外,您可以根据可能解决次要问题的条件来短路此链。
如果这不起作用,您可能需要考虑对消息进行版本控制,以便以优雅的方式支持这两种方案。