Beispiel #1
0
        public Task Publish <TNotification>(TNotification notification, PublishStrategy strategy, CancellationToken cancellationToken)
        {
            if (!PublishStrategies.TryGetValue(strategy, out var mediator))
            {
                throw new ArgumentException($"Unknown strategy: {strategy}");
            }

            return(mediator.Publish(notification as INotification, cancellationToken));
        }
        public Task HandleAsync(
            TNotification notification,
            PublishStrategy <TNotification> publishStrategy,
            ServiceProvider serviceProvider,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var services = serviceProvider
                           .GetServices <INotificationHandler <TNotification> >()
                           ?? Enumerable.Empty <INotificationHandler <TNotification> >();

            var handlers = services.Select(service => new NotificationHandler <TNotification>(
                                               (serviceNotification, serviceCancellationToken) => service.HandleAsync(
                                                   serviceNotification,
                                                   serviceCancellationToken
                                                   )
                                               ));

            return(publishStrategy(notification, handlers, cancellationToken));
        }
Beispiel #3
0
        public void Publish <T>(string exchange, PublishStrategy strategy, string routingKey, T data) where T : class
        {
            var factory = new ConnectionFactory()
            {
                HostName = settings.Connection.RabbitMQHostname,
                UserName = settings.Connection.RabbitMQUsername,
                Password = settings.Connection.RabbitMQPassword
            };

            using var connection = factory.CreateConnection();
            using var channel    = connection.CreateModel();
            if (strategy == PublishStrategy.RoundRobin)
            {
                channel.ExchangeDeclare(exchange, "x-consistent-hash", true, false);
            }
            else if (strategy == PublishStrategy.Fanout)
            {
                channel.ExchangeDeclare(exchange, "fanout", true, false);
            }
            else
            {
                throw new NotImplementedException();
            }

            var key = routingKey;

            if (strategy == PublishStrategy.RoundRobin)
            {
                key = $"{key}.{Guid.NewGuid()}";
            }

            var serialisedData = JsonConvert.SerializeObject(data, new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
            var body = Encoding.UTF8.GetBytes(serialisedData);

            channel.BasicPublish(exchange: exchange, routingKey: key, basicProperties: null, body: body);
        }
Beispiel #4
0
 public Task Publish <TNotification>(TNotification notification, PublishStrategy strategy)
 {
     return(Publish(notification, strategy, default(CancellationToken)));
 }
 public void SetUp_ObjectUnderTest()
 {
     _factoryMock     = new Mock <ICommandFactory <PublishStrategy> >();
     _objectUnderTest = new PublishStrategy(_factoryMock.Object);
 }
Beispiel #6
0
 public void Publish <T>(string exchange, PublishStrategy strategy, T data) where T : class
 {
     Publish <T>(exchange, strategy, $"{exchange}.publish", data);
 }
 public void Publish <T>(string exchange, PublishStrategy strategy, T data) where T : class
 {
 }