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
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 Diagrama de classes contendo um publicador de mensagem de exemplo:
...
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 { public class GlbSGDPPublisherMessageService : IGlbSGDPPublisherMessageService { /// <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 ctSGDPAudience = "SUA_SIGLA_DE_AUDIENCESGDP"; // <-- SEU privateCÓDIGO constDE stringAUDIENCE ctSGDPUpdateTenantMetadataEndPointDEVE = "/api/v1/link/send/SGDPUpdateTenantMetadata/SGDP"; private const string ctSGDPDataResponseEndPoint = "/api/v1/link/send/SGDPDataResponse/SGDP";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 = "SGDPResponseDataCommandSGDPDataResponse"; private const string ctSGDPMaskResponseEndPointctSGDPMaskCommand = "/api/v1/link/send/SGDPMaskResponse/SGDPSGDPMaskCommand"; private const string ctSGDPMaskCommandctSGDPResponseMaskCommand = "SGDPMaskCommandSGDPMaskResponse"; private const string ctSGDPResponseMaskCommandctSGDPLogsCommand = "SGDPResponseMaskCommandSGDPLogsCommand"; private const string ctSGDPLogResponseEndPointctSGDPResponseLogCommand = "/api/v1/link/send/SGDPLogsResponse/SGDP";SGDPLogsResponse"; private const string ctSGDPLogsCommandctSmartLinkSetupCommand = "SGDPLogsCommandsetup"; private const string ctSGDPResponseLogCommandctSmartLinkUnSetupCommand = "SGDPResponseLogsCommandunSetup"; /// <summary> /// Constructor /// </summary> private const string/// ctSmartLinkSetupCommand<param name= "setupsmartLinkPublisherService";></param> private/// const string ctSmartLinkUnSetupCommand = "unSetup"; public GlbSGDPPublisherMessageService(IRMSSmartLinkPublisherService smartLinkPublisherService, IGlbSGDPResolverService sgdpResolverService, IRMSLogger logService) { _smartLinkPublisherService = smartLinkPublisherService; <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 ctSGDPUpdateTenantMetadataEndPoint = "/api/v1/link/send/SGDPUpdateTenantMetadata/SGDP"; private const string ctSGDPDataResponseEndPoint = "/api/v1/link/send/SGDPDataResponse/SGDP"; private const string ctSGDPUpdateTenantMetadata = "SGDPUpdateTenantMetadata"; private const string ctSGDPDataCommand = "SGDPDataCommand"; private const string ctSGDPResponseDataCommand = "SGDPDataResponse"; private const string ctSGDPMaskResponseEndPoint = "/api/v1/link/send/SGDPMaskResponse/SGDP"; private const string ctSGDPMaskCommand = "SGDPMaskCommand"; private const string ctSGDPResponseMaskCommand = "SGDPMaskResponse"; private const string ctSGDPLogResponseEndPoint = "/api/v1/link/send/SGDPLogsResponse/SGDP"; private const string ctSGDPLogsCommand = "SGDPLogsCommand"; private const string ctSGDPResponseLogCommand = "SGDPLogsResponse"; private const string ctSmartLinkSetupCommand = "setup"; private const string ctSmartLinkUnSetupCommand = "unSetup"; 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, RouterMessage = ctSGDPDataResponseEndPoint }; _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, RouterMessage = ctSGDPMaskResponseEndPoint }; _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, RouterMessage = ctSGDPLogResponseEndPoint }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseLogCommandGravadoNaFila); _smartLinkPublisherService.AddMessage(parPublisher); } /// <summary> /// Adiciona uma mensagem para envio de um comando de sgldpdateapplicationmetada. /// </summary> public void AddMessageUpdateTenantMetadata(AddMessageUpdateTenantMetadataParms parms) { try { if(string.IsNullOrEmpty(parms.TenantId) _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, Audience = ctSGDPAudience }; ;SmartLinkPublisherAddMessageParams parPublisher.Data = parms.Message; parPublisher.RouterMessage = ctSGDPDataResponseEndPoint; _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, RouterMessage = ctSGDPMaskResponseEndPoint, 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, RouterMessage = ctSGDPLogResponseEndPoint, Audience = ctSGDPAudience }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseLogCommandGravadoNaFila); _smartLinkPublisherService.AddMessage(parPublisher); } /// <summary> /// Adiciona uma mensagem para envio de um comando de sgldpdateapplicationmetada. /// </summary> public void AddMessageUpdateTenantMetadata(AddMessageUpdateTenantMetadataParms parms) { if (CanAddTenantMetadataMessage()) { try { List<string> jsons = GetUpdateTenandMetadata(parms.TenantId); foreach (string json in jsons) { SmartLinkPublisherAddMessageParams smartLinkParms = new SmartLinkPublisherAddMessageParams(); smartLinkParms.Command = ctSGDPUpdateTenantMetadata; smartLinkParms.CorrelatedId = ""; smartLinkParms.Data = json; smartLinkParms.RouterMessage = ctSGDPUpdateTenantMetadataEndPoint; smartLinkParms.Audience = ctSGDPAudience; _logServic e.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPTenantMetadaGravadoNaFila); _smartLinkPublisherService.AddMessage(smartLinkParms); } } catch (Exception ex) { _logService.NotifyLogWarning(ex, Properties.Resources.sconTotvsAppErroAoEnviarSGDPUpdateTenantMetadatasSGDPTenantMetadataInvalidSGDPUpdateTenantMetadata); } return; } List<string> jsons } = GetUpdateTenandMetadata(parms.TenantId); private boolforeach CanAddTenantMetadataMessage(string json in jsons) { SmartLinkPublisherAddMessageParams smartLinkParms = new SmartLinkPublisherAddMessageParams //Se 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, y.TypeEventRouterMessage == ctSGDPDataCommandctSGDPUpdateTenantMetadataEndPoint || }; y.TypeEvent == ctSGDPMaskCommand || _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPTenantMetadaGravadoNaFila); y.TypeEvent == ctSGDPResponseDataCommand ||_smartLinkPublisherService.AddMessage(smartLinkParms); } y.TypeEvent == ctSGDPResponseMaskCommand).ToList(); } if (messageUpstream?.Count > 0)//Se existir alguma não inclua na fila...catch (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 = "", Audience = ctSGDPAudience, Data = _sgdpResolverService.GetSGDPSetup().JsonResult }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppUnSetupGravadoNaFila); _smartLinkPublisherService.AddMessage(parms); } public void AddMessageSetup() { SmartLinkPublisherAddMessageParams parms = new SmartLinkPublisherAddMessageParams { Command = ctSmartLinkSetupCommand, CorrelatedId = "", Audience = ctSGDPAudience, Data = _sgdpResolverService.GetSGDPSetup().JsonResult }; _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSetupGravadoNaFila); _smartLinkPublisherService.AddMessage(parms); } } } |
...