Histórico da Página
...
- Criar um projeto (classLibrary .NET) na solution do segmento em questão, com o seguinte padrão de nome: RM.[Segmento].XXX.SmartLink.Service.
- Exemplo: RM.Glb.SGDP.SmartLink.Service.dll
- Adicionar referência para a dll "RM.Lib.SmartLink.dll";
Criar uma classe que receba em seu construtor instâncias das seguintes interfaces:
"IRMSSmartLinkPublisherService": interface de serviço responsável em incluir a mensagem na fila do RM.SmartLink.Client.
"IRMSLogger": serviço usado para incluir logs relacionados à regra de negócio em questão.
Informações Todos os logs adicionados nesse serviço serão gravados automaticamente na tabela "GTotvsLinkLog"
Para publicar uma mensagem na fila do RM.SmartLink.Client, basta chamar o método "AddMessage" da instância de interface "IRMSSmartLinkPublisherService". Os seguinte dados devem ser enviados através de uma classe de parâmetros do tipo "SmartLinkPublisherAddMessageParams":
Propriedade Descrição Command Nome do comando a ser incluído na fila do SmartLink. Ex: SGDPUpdateTenantMetada CorrelatedId Pode ser ser enviado nessa propriedade um identificador (guid) que correlacionam mensagens de envio e resposta. Data Dados da mensagem contendo informações relacionadas ao negócio. Pode ser em qualquer formato (json, xml, etc) desde que o consumidor consiga interpretá-lo.
RouterMessage Rota de envio da mensagem. Será concatenada ao endpoint do serviço do Totvs SmartLink.Server. ex: /api/v1/link/send/SGDPMaskResponse/SGDP
- Diagrama de classes contendo um publicador de mensagem de exemplo:
Na versão 12.1.2402, que utiliza api REST para envio de mensagem, o valor do Audience é definido no final do RouterMessage. No caso desse exemplo o Audience é o SGDP.
A partir da versão 12.1.2406 não é necessário passar o RouterMessage como parâmetro pois utiliza o protocolo gRPC para envio de mensagem.Audience Código do TOTVS App específico que irá receber a mensagem.
Para versões 12.1.2406 e acima, que já utiliza o gRPC para envio de mensagem, o Audience deve ser passado por esse parâmetro.
- Diagrama de classes contendo um publicador de mensagem de exemplo:
5. Diagrama de sequência contendo um exemplo do ciclo de 5. Diagrama de sequência contendo um exemplo do ciclo de vida da inclusão da mensagem "SGDPTenantMetada"
5 6. Código fonte de exemplo (extraído da classe "GlbSGDPPublisherMessageService" localizada na solution de Globais, projeto "RM.Glb.SGDP.SmartLink.Service". Esse exemplo é para versões a partir da 12.1.2406 que já utiliza o protocolo gRPC.
Bloco de código | ||||||
---|---|---|---|---|---|---|
| ||||||
using RM.Glb.SGDP.SmartLink.Service.Domain; using RM.Glb.SGDP.SmartLink.Service.Domain.Interfaces; using RM.Lib.Log; using RM.Lib.SmartLink.Domain.DataModel; using RM.Lib.SmartLink.Domain.Interfaces; using System; using System.Collections.Generic; using System.Linq; namespace RM.Glb.SGDP.SmartLink.Service { /// <summary> /// SGDP Publisher Message Service class /// </summary> public class GlbSGDPPublisherMessageService : IGlbSGDPPublisherMessageService { private readonly IGlbSGDPResolverService _sgdpResolverService; private readonly IRMSSmartLinkPublisherService _smartLinkPublisherService; private readonly IRMSLogger _logService; private const string ctSGDPUpdateTenantMetadataEndPointctSGDPAudience = "SGDP"; /api/v1/link/send/SGDPUpdateTenantMetadata/SGDP"; / <-- SEU CÓDIGO DE AUDIENCE DEVE VIR AQUI. NO CASO DESSE EXEMPLO É SGDP. OBTER O CÓDIGO COM A EQUIPE DE TOTVS APPS. private const string ctSGDPUpdateTenantMetadata = "SGDPUpdateTenantMetadata"; private const string ctSGDPDataCommand = "SGDPDataCommand"; private const string ctSGDPResponseDataCommand = "SGDPDataResponse"; private const string ctSGDPMaskCommand = "SGDPMaskCommand"; private const string ctSGDPResponseMaskCommand = "SGDPMaskResponse"; private const string ctSGDPLogsCommand = "SGDPLogsCommand"; private const string ctSGDPResponseLogCommand = "SGDPLogsResponse"; private const string ctSmartLinkSetupCommand = "setup"; private const string ctSmartLinkUnSetupCommand = "unSetup"; /// <summary> /// Constructor /// </summary> /// <param name="smartLinkPublisherService"></param> /// <param name="sgdpResolverService"></param> /// <param name="logService"></param> public GlbSGDPPublisherMessageService(IRMSSmartLinkPublisherService smartLinkPublisherService, IGlbSGDPResolverService sgdpResolverService, IRMSLogger logService) { _smartLinkPublisherService = smartLinkPublisherService; _sgdpResolverService = sgdpResolverService; _logService = logService; } /// <summary> /// Adiciona uma mensagem de reposta para o comando "SGDPResponseDataCommand". /// </summary> /// <param name="parms"></param> public void AddResponseDataCommand(AddResponseDataCommandParms parms) { SmartLinkPublisherAddMessageParams parPublisher = new SmartLinkPublisherAddMessageParams { Command = ctSGDPResponseDataCommand, CorrelatedId = parms.CorrelatedId, Data = parms.Message, Audience = ctSGDPAudience }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseDataCommandGravadoNaFila); _smartLinkPublisherService.AddMessage(parPublisher); } /// <summary> /// Adiciona uma mensagem de reposta para o comando "SGDPResponseMaskCommand". /// </summary> /// <param name="parms"></param> public void AddResponseMaskCommand(AddResponseMaskCommandParms parms) { SmartLinkPublisherAddMessageParams parPublisher = new SmartLinkPublisherAddMessageParams { Command = ctSGDPResponseMaskCommand, CorrelatedId = parms.CorrelatedId, Data = parms.Message, Audience = ctSGDPAudience }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseMaskCommandGravadoNaFila); _smartLinkPublisherService.AddMessage(parPublisher); } /// <summary> /// Adiciona uma mensagem de reposta para o comando "SGDPResponseLogCommand". /// </summary> /// <param name="parms">Parms</param> public void AddResponseLogCommand(AddResponseLogCommandParms parms) { SmartLinkPublisherAddMessageParams parPublisher = new SmartLinkPublisherAddMessageParams { Command = ctSGDPResponseLogCommand, CorrelatedId = parms.CorrelatedId, Data = parms.Message, Audience = ctSGDPAudience }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseLogCommandGravadoNaFila); _smartLinkPublisherService.AddMessage(parPublisher); } /// <summary> /// Adiciona uma mensagem para envio de um comando de sgldpdateapplicationmetada. /// </summary> /// <param name="parms"></param> public void AddMessageUpdateTenantMetadata(AddMessageUpdateTenantMetadataParms parms) { try { if (string.IsNullOrEmpty(parms.TenantId)) { _logService.NotifyLogWarning(Properties.Resources.sSGDPTenantMetadataInvalidSGDPUpdateTenantMetadata); return; } List<string> jsons = GetUpdateTenandMetadata(parms.TenantId); foreach (string json in jsons) { SmartLinkPublisherAddMessageParams smartLinkParms = new SmartLinkPublisherAddMessageParams { Command = ctSGDPUpdateTenantMetadata, CorrelatedId = "", Data = json, Audience = ctSGDPAudience }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPTenantMetadaGravadoNaFila); _smartLinkPublisherService.AddMessage(smartLinkParms); } } catch (Exception ex) { _logService.NotifyLogWarning(ex, Properties.Resources.sconTotvsAppErroAoEnviarSGDPUpdateTenantMetadata); } } private List<string> GetUpdateTenandMetadata(string tenantId) { return _sgdpResolverService.GetSGDPTenantMetadata( new MetadataTenantParms() { TenantId = tenantId } )?.JsonsResult; } /// <summary> /// Adiciona mensagem UnSetup /// </summary> public void AddMessageUnSetup() { var pendingMessages = _smartLinkPublisherService.GetPendingMessages(); var messagesSGDP = pendingMessages?.Where(y => y.TypeEvent.ToUpper() == ctSGDPUpdateTenantMetadata.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPDataCommand.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPMaskCommand.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPLogsCommand.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPResponseDataCommand.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPResponseMaskCommand.ToUpper()).ToList(); if (messagesSGDP != null) { foreach (SmartLinkMessageEntity model in messagesSGDP) { _smartLinkPublisherService.RemoveMessageById(model.Id); } } SmartLinkPublisherAddMessageParams parms = new SmartLinkPublisherAddMessageParams { Command = ctSmartLinkUnSetupCommand, CorrelatedId = "", Audience = ctSGDPAudience, Data = _sgdpResolverService.GetSGDPSetup().JsonResult }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppUnSetupGravadoNaFila); _smartLinkPublisherService.AddMessage(parms); } /// <summary> /// Adiciona messagem setup /// </summary> public void AddMessageSetup() { SmartLinkPublisherAddMessageParams parms = new SmartLinkPublisherAddMessageParams { Command = ctSmartLinkSetupCommand, CorrelatedId = "", Audience = ctSGDPAudience, Data = _sgdpResolverService.GetSGDPSetup().JsonResult }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSetupGravadoNaFila); _smartLinkPublisherService.AddMessage(parms); } } } |
7. Código fonte de exemplo (extraído da classe "GlbSGDPPublisherMessageService" localizada na solution de Globais, projeto "RM.Glb.SGDP.SmartLink.Service". Esse exemplo é para a versão 12.1.2402 que ainda utiliza api REST para envio de mensagem. Nesse caso o Audience é definido no final do endpoint passado como parâmetro para o RouterMessage.
Bloco de código | ||||||
---|---|---|---|---|---|---|
| ||||||
using RM.Glb.SGDP.SmartLink.Service.Domain; using RM.Glb.SGDP.SmartLink.Service.Domain.Interfaces; using RM.Lib.Log; using RM.Lib.SmartLink.Domain; using RM.Lib.SmartLink.Domain.Interfaces; using System; using System.Collections.Generic; using System.Linq; namespace RM.Glb.SGDP.SmartLink.Service { public class GlbSGDPPublisherMessageService : IGlbSGDPPublisherMessageService { private readonly IGlbSGDPResolverService _sgdpResolverService; private readonly IRMSSmartLinkPublisherService _smartLinkPublisherService; private readonly IRMSLogger _logService; private const string ctSGDPDataResponseEndPoint = "/api/v1/link/send/SGDPDataResponse/SGDP"; private const string ctSGDPUpdateTenantMetadata = "SGDPUpdateTenantMetadata"; private const string ctSGDPDataCommand = "SGDPDataCommand"; private const string ctSGDPResponseDataCommand = "SGDPResponseDataCommand"; private const string ctSGDPMaskResponseEndPoint = "/api/v1/link/send/SGDPMaskResponse/SGDP"; private const string ctSGDPMaskCommand = "SGDPMaskCommand"; private const string ctSGDPResponseMaskCommand = "SGDPResponseMaskCommand"; private const string ctSGDPLogResponseEndPoint = "/api/v1/link/send/SGDPLogsResponse/SGDP"; private const string ctSGDPLogsCommand = "SGDPLogsCommand"; private const string ctSGDPResponseLogCommandctSGDPUpdateTenantMetadataEndPoint = "SGDPResponseLogsCommand/api/v1/link/send/SGDPUpdateTenantMetadata/SGDP"; private const string ctSmartLinkSetupCommandctSGDPDataResponseEndPoint = "setup/api/v1/link/send/SGDPDataResponse/SGDP"; private const string ctSmartLinkUnSetupCommandctSGDPUpdateTenantMetadata = "unSetupSGDPUpdateTenantMetadata"; public GlbSGDPPublisherMessageService(IRMSSmartLinkPublisherService smartLinkPublisherService, IGlbSGDPResolverService sgdpResolverService, IRMSLogger logService) { private const string _smartLinkPublisherServicectSGDPDataCommand = smartLinkPublisherService"SGDPDataCommand"; private const _sgdpResolverServicestring ctSGDPResponseDataCommand = sgdpResolverService"SGDPDataResponse"; private const string _logServicectSGDPMaskResponseEndPoint = logService"/api/v1/link/send/SGDPMaskResponse/SGDP"; } private const string ctSGDPMaskCommand /// <summary>= "SGDPMaskCommand"; ///private Adicionaconst uma mensagem de reposta para o comando "SGDPResponseDataCommand".string ctSGDPResponseMaskCommand = "SGDPMaskResponse"; /// </summary> /// <param name="parms"></param>private const string ctSGDPLogResponseEndPoint = "/api/v1/link/send/SGDPLogsResponse/SGDP"; publicprivate voidconst AddResponseDataCommand(AddResponseDataCommandParms parms) {string ctSGDPLogsCommand = "SGDPLogsCommand"; private const SmartLinkPublisherAddMessageParamsstring parPublisherctSGDPResponseLogCommand = new SmartLinkPublisherAddMessageParams {"SGDPLogsResponse"; private const string CommandctSmartLinkSetupCommand = ctSGDPResponseDataCommand,"setup"; private const string ctSmartLinkUnSetupCommand CorrelatedId = parms.CorrelatedId"unSetup"; public }; GlbSGDPPublisherMessageService(IRMSSmartLinkPublisherService smartLinkPublisherService, IGlbSGDPResolverService ;sgdpResolverService, IRMSLogger logService) parPublisher.Data = parms.Message;{ parPublisher.RouterMessage_smartLinkPublisherService = ctSGDPDataResponseEndPointsmartLinkPublisherService; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseDataCommandGravadoNaFila)sgdpResolverService = sgdpResolverService; _smartLinkPublisherService.AddMessage(parPublisher)logService = logService; } /// <summary> /// Adiciona uma mensagem de reposta para o comando "SGDPResponseMaskCommandSGDPResponseDataCommand". /// </summary> /// <param name="parms"></param> public void AddResponseMaskCommandAddResponseDataCommand(AddResponseMaskCommandParmsAddResponseDataCommandParms parms) { SmartLinkPublisherAddMessageParams parPublisher = new SmartLinkPublisherAddMessageParams { Command = ctSGDPResponseMaskCommandctSGDPResponseDataCommand, CorrelatedId = parms.CorrelatedId, Data = parms.Message, RouterMessage = ctSGDPMaskResponseEndPointctSGDPDataResponseEndPoint }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseMaskCommandGravadoNaFilasconTotvsAppSGDPResponseDataCommandGravadoNaFila); _smartLinkPublisherService.AddMessage(parPublisher); } /// <summary> /// Adiciona uma mensagem de reposta para o comando "SGDPResponseLogCommandSGDPResponseMaskCommand". /// </summary> /// <param name="parms">Parms<></param> public void AddResponseLogCommandAddResponseMaskCommand(AddResponseLogCommandParmsAddResponseMaskCommandParms parms) { SmartLinkPublisherAddMessageParams parPublisher = new SmartLinkPublisherAddMessageParams { Command = ctSGDPResponseLogCommandctSGDPResponseMaskCommand, CorrelatedId = parms.CorrelatedId, Data = parms.Message, RouterMessage = ctSGDPLogResponseEndPointctSGDPMaskResponseEndPoint }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseLogCommandGravadoNaFilasconTotvsAppSGDPResponseMaskCommandGravadoNaFila); _smartLinkPublisherService.AddMessage(parPublisher); } /// <summary> /// Adiciona uma mensagem parade envioreposta depara umo comando de sgldpdateapplicationmetada"SGDPResponseLogCommand". /// </summary> public/// void AddMessageUpdateTenantMetadata(AddMessageUpdateTenantMetadataParms parms) {<param name="parms">Parms</param> public void if (CanAddTenantMetadataMessage()AddResponseLogCommand(AddResponseLogCommandParms parms) { SmartLinkPublisherAddMessageParams parPublisher try = new SmartLinkPublisherAddMessageParams { Command List<string> jsons = GetUpdateTenandMetadata(parms.TenantId);ctSGDPResponseLogCommand, CorrelatedId foreach (string json in jsons)= parms.CorrelatedId, Data = {parms.Message, RouterMessage SmartLinkPublisherAddMessageParams smartLinkParms = new SmartLinkPublisherAddMessageParams();ctSGDPLogResponseEndPoint }; smartLinkParms.Command = ctSGDPUpdateTenantMetadata_logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseLogCommandGravadoNaFila); _smartLinkPublisherService.AddMessage(parPublisher); } smartLinkParms.CorrelatedId = ""; /// <summary> /// Adiciona uma mensagem para envio de um smartLinkParms.Datacomando =de json;sgldpdateapplicationmetada. /// </summary> public smartLinkParms.RouterMessage = ctSGDPUpdateTenantMetadataEndPoint;void AddMessageUpdateTenantMetadata(AddMessageUpdateTenantMetadataParms parms) { try _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPTenantMetadaGravadoNaFila); { _smartLinkPublisherService.AddMessage(smartLinkParms);if(string.IsNullOrEmpty(parms.TenantId)) { } }_logService.NotifyLogWarning(Properties.Resources.sSGDPTenantMetadataInvalidSGDPUpdateTenantMetadata); catch (Exception ex)return; {} List<string> _logService.NotifyLogWarning(ex, Properties.Resources.sconTotvsAppErroAoEnviarSGDPUpdateTenantMetadatajsons = GetUpdateTenandMetadata(parms.TenantId); } foreach (string json in jsons) } } { private bool CanAddTenantMetadataMessage() { SmartLinkPublisherAddMessageParams smartLinkParms = new //SeSmartLinkPublisherAddMessageParams existir alguma mensagem de sgdp para ser processada, então não{ envia atualizaçaõ de medatdos pois esse processo é caro. Command = ctSGDPUpdateTenantMetadata, var pendingMessages = _smartLinkPublisherService.GetPendingMessages(); List<SmartLinkMessageDataModel> messageUpstreamCorrelatedId = pendingMessages?.Messages?.Where("", y => y.TypeEventData == ctSGDPUpdateTenantMetadata || json, RouterMessage y.TypeEvent == ctSGDPDataCommand ||ctSGDPUpdateTenantMetadataEndPoint }; y.TypeEvent == ctSGDPMaskCommand || _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPTenantMetadaGravadoNaFila); y.TypeEvent == ctSGDPResponseDataCommand || _smartLinkPublisherService.AddMessage(smartLinkParms); } y.TypeEvent == ctSGDPResponseMaskCommand).ToList(); } ifcatch (messageUpstream?.Count > 0)//Se existir alguma não inclua na fila...Exception ex) { return false_logService.NotifyLogWarning(ex, Properties.Resources.sconTotvsAppErroAoEnviarSGDPUpdateTenantMetadata); return true;} } private List<string> GetUpdateTenandMetadata(string tenantId) { return _sgdpResolverService.GetSGDPTenantMetadata( new MetadataTenantParms() { TenantId = tenantId } )?.JsonsResult; } public void AddMessageUnSetup() { var pendingMessages = _smartLinkPublisherService.GetPendingMessages(); List<SmartLinkMessageDataModel>var messagesSGDP = pendingMessages?.Messages?.Where(y => y.TypeEvent.ToUpper() == ctSGDPUpdateTenantMetadata.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPDataCommand.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPMaskCommand.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPLogsCommand.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPResponseDataCommand.ToUpper() && y.TypeEvent.ToUpper() == ctSGDPResponseMaskCommand.ToUpper()).ToList(); if (messagesSGDP != null) { foreach (SmartLinkMessageDataModelSmartLinkMessageEntity model in messagesSGDP) { _smartLinkPublisherService.RemoveMessageById(model.Id); } } SmartLinkPublisherAddMessageParams parms = new SmartLinkPublisherAddMessageParams { Command = ctSmartLinkUnSetupCommand, CorrelatedId = "", Data = _sgdpResolverService.GetSGDPSetup().JsonResult }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppUnSetupGravadoNaFila); _smartLinkPublisherService.AddMessage(parms); } public void AddMessageSetup() { SmartLinkPublisherAddMessageParams parms = new SmartLinkPublisherAddMessageParams { Command = ctSmartLinkSetupCommand, CorrelatedId = "", Data = _sgdpResolverService.GetSGDPSetup().JsonResult }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSetupGravadoNaFila); _smartLinkPublisherService.AddMessage(parms); } } } |
...