protected void Raise(IntegrationEvent integrationEvent) { integrationEvents.Add(integrationEvent); handlers[integrationEvent.GetType()](integrationEvent); Version++; }
public void Apply(IntegrationEvent integrationEvent) { handlers[integrationEvent.GetType()](integrationEvent); Version++; }
private string GetEventKey(IntegrationEvent @event) { return(@event.GetType().Name); }
public bool PublishEvent(IntegrationEvent @event) { RabbitMqTopicModel topicModel = _integrationEventTopicMapping.GetPublishedTopic(@event.GetType().Name); if (topicModel == null) { return(false); } _queueProcessor.PublishMessageToExchange(topicModel.Exchange, topicModel.RoutingKey, @event); return(true); }
public IEnumerable <SubscriptionInfo> GetHandlersForEvent(IntegrationEvent integrationEvent) { var key = GetEventKey(integrationEvent.GetType()); return(GetHandlersForEvent(key)); }
public void Publish(IntegrationEvent @event, byte priority = 0) { logger.Info("Se inicia la ejecución del método Publish"); logger.Debug("Argumentos del método Publish: event - {}, priority - {}", @event, priority); if (!_persistentConnection.IsConnected) { logger.Trace("No se ha establecido la conexión al Message Broker"); _persistentConnection.TryConnect(); } var policy = RetryPolicy.Handle <BrokerUnreachableException>() .Or <SocketException>() .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => { logger.Warn("No se ha podido establecer la conexión al Message Broker"); logger.Debug("Argumentos del action onRetry: Exception - {}, TimeSpan - {}", ex, time); }); logger.Trace("Se procede a crear el Channel"); using (var channel = _persistentConnection.CreateModel()) { logger.Debug("Valor de la variable channel - {}", channel); logger.Trace("Se obtiene el nombre del evento y se almacena en la variable eventName"); var eventName = @event.GetType() .Name; logger.Debug("Valor de la variable eventName - {}", eventName); logger.Trace("Se declara el Exchange en el Message Broker"); logger.Debug("Argumentos de channel.ExchangeDeclare: exchange - {}, type - {}, durable - {}", BROKER_NAME, "direct", true); channel.ExchangeDeclare(exchange: BROKER_NAME, type: "direct", durable: true); logger.Trace("Se obtiene la instancia del IntegrationEvent"); var message = JsonConvert.SerializeObject(@event); logger.Debug("Valor de la variable message - {}", message); logger.Trace("Se obtiene el payload del IntegrationEvent"); var body = Encoding.UTF8.GetBytes(message); logger.Debug("Valor de la variable body - {}", body); logger.Trace("Se define la política para la publicación del evento"); policy.Execute(() => { logger.Trace("Se inicia la creación de las propiedades del mensaje"); var properties = channel.CreateBasicProperties(); logger.Debug("Valor de la variable properties - {}", properties); logger.Trace("Se establece el DeliveryMode"); properties.DeliveryMode = 2; // persistent logger.Debug("Valor del properties.DeliveryMode - {}", properties.DeliveryMode); logger.Trace("Se establece el Priority"); properties.Priority = priority; logger.Debug("Valor del properties.Priority - {}", properties.Priority); logger.Trace("Se realiza la publicación del mensaje en el Message Broker"); logger.Debug("Argumentos de channel.BasicPublish: exchange - {}, routingKey - {}, mandatory - {}, basicProperties - {}, body - {}", BROKER_NAME, eventName, true, properties, body); channel.BasicPublish(exchange: BROKER_NAME, routingKey: eventName, mandatory: true, basicProperties: properties, body: body); }); } }
public void Publish(IntegrationEvent @event, string exchangeName = null) { foreach (var extender in _eventExtenders) { extender.Extend(@event); } var connectStopwatch = Stopwatch.StartNew(); if (!_persistentConnection.IsConnected) { _persistentConnection.TryConnect(); } connectStopwatch.Stop(); var policy = Policy .Handle <BrokerUnreachableException>() .Or <SocketException>() .Or <TimeoutException>() .WaitAndRetryForever(retryAttempt => { var seconds = Math.Pow(2, retryAttempt); return(TimeSpan.FromSeconds(seconds < _persistentConnection.RetryPolicyMaxSleepDurationSeconds ? seconds : _persistentConnection.RetryPolicyMaxSleepDurationSeconds)); }, (ex, time) => { _logger.LogWarning(ex, "Could not establish connection to RabbitMQ because of `{exception}` in `{time}`, retrying...", ex.Message, time); } ); policy.Execute(() => { var channelStopwatch = Stopwatch.StartNew(); using (var channel = _persistentConnection.CreateChannel()) { channelStopwatch.Stop(); var properties = channel.CreateBasicProperties(); properties.Persistent = true; var eventName = @event.GetType().Name; var message = JsonConvert.SerializeObject(@event); var body = Encoding.UTF8.GetBytes(message); var publishStopwatch = Stopwatch.StartNew(); channel.BasicPublish( exchange: exchangeName, routingKey: eventName, basicProperties: properties, body: body); publishStopwatch.Stop(); var totalElapsedMilliseconds = connectStopwatch.ElapsedMilliseconds + channelStopwatch.ElapsedMilliseconds + publishStopwatch.ElapsedMilliseconds; if (totalElapsedMilliseconds > 100) { _logger.LogWarning( "Slow publishing to exchange: '{exchangeName}' event: '{eventType}' total: '{elapsedMilliseconds}'ms, connect: '{connectMilliseconds}'ms, channel: '{channelMilliseconds}'ms, publish: '{publishMilliseconds}'ms", exchangeName, @event.GetType().Name, totalElapsedMilliseconds, connectStopwatch.ElapsedMilliseconds, channelStopwatch.ElapsedMilliseconds, publishStopwatch.ElapsedMilliseconds); } } }); }