Ejemplo n.º 1
0
        public EventProcessor CreateEventProcessor(string topic, string subscription, SubscriptionSettings subscriptionSettings, IEventHandler handler, ITextSerializer serializer, bool instrumentationEnabled = false)
        {
            var retryStrategy = new Incremental(3, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            var retryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(retryStrategy);
            var namespaceManager = settings.GetNamespaceManager();
            var topicSettings = new TopicSettings
            {
                IsEventBus = true,
                DuplicateDetectionHistoryTimeWindow = new TimeSpan(0, 30, 0),
                Path = topic
            };
            retryPolicy.ExecuteAction(() => CreateSubscriptionIfNotExists(namespaceManager, topicSettings, subscriptionSettings));
            retryPolicy.ExecuteAction(() => UpdateRules(namespaceManager, topicSettings, subscriptionSettings, settings.GetMessagingFactory()));

            IMessageReceiver receiver;

            if (subscriptionSettings.RequiresSession)
            {
                var instrumentation = new SessionSubscriptionReceiverInstrumentation(subscription, instrumentationEnabled);
                try
                {
                    receiver = (IMessageReceiver)new SessionSubscriptionReceiver(this.settings, topic, subscription, true, instrumentation);
                }
                catch
                {
                    instrumentation.Dispose();
                    throw;
                }
            }
            else
            {
                var instrumentation = new SubscriptionReceiverInstrumentation(subscription, instrumentationEnabled);
                try
                {
                    receiver = (IMessageReceiver)new SubscriptionReceiver(this.settings, topic, subscription, true, instrumentation);
                }
                catch
                {
                    instrumentation.Dispose();
                    throw;
                }
            }

            EventProcessor processor;
            try
            {
                processor = new EventProcessor(receiver, serializer);
            }
            catch
            {
                using (receiver as IDisposable) { }
                throw;
            }

            try
            {
                processor.Register(handler);

                return processor;
            }
            catch
            {
                processor.Dispose();
                throw;
            }
        }
        //public void RegistarSubscricoes(IEnumerable<Assinatura> assinaturas, string nomeTopico)
        //{
        //    var topico = (from e in this.topicos
        //                  where e.Path.Equals(nomeTopico)
        //                  select e).FirstOrDefault();
        //    if (topico != null)
        //    {
        //        foreach (var assinatura in assinaturas)
        //        {
        //            var subscription = new SubscriptionSettings
        //            {
        //                Name = assinatura.ObterNomeAssinatura(),
        //                RequiresSession = assinatura.RequerSessao,
        //                SqlFilter = assinatura.Filtro
        //            };
        //            topico.Subscriptions.Add(subscription);
        //        }
        //    }
        //}
        //public void RegistarSubscricoes(Assembly assembly)
        //{
        //    foreach (var eventHandlerType in assembly.ExportedTypes.Where(e => typeof(IEventHandler).IsAssignableFrom(e)))
        //    {
        //        var eventHandlerInstance = (BaseEventHandler)Activator.CreateInstance(eventHandlerType, this.TenantName);
        //        var topico = (from e in this.topicos
        //                     where e.Path.Equals(eventHandlerInstance.TopicName)
        //                     select e).FirstOrDefault();
        //        if(topico != null)
        //        {
        //            string filter = BaseEventHandler.MountFilter(eventHandlerType);
        //            var subscription = new SubscriptionSettings
        //            {
        //                Name = eventHandlerType.AssemblyQualifiedName.GetHashCode().ToString(),
        //                RequiresSession = false,
        //                SqlFilter = filter
        //            };
        //            topico.Subscriptions.Add(subscription);
        //        }
        //    }
        //}
        public void RegistrarTopico(string nomeTopico)
        {
            var topico = new TopicSettings
            {
                IsEventBus = true,
                DuplicateDetectionHistoryTimeWindow = new TimeSpan(0, 30, 00)
            };

            topico.Path = nomeTopico;
            topico.Subscriptions.Add(new SubscriptionSettings
             {
                 Name = "Log",
                 RequiresSession = false,
             });

            this.topicos.Add(topico);
        }
Ejemplo n.º 3
0
        private void CreateTopicIfNotExists(NamespaceManager namespaceManager, TopicSettings topic)
        {
            var topicDescription =
                new TopicDescription(topic.Path)
                {
                    RequiresDuplicateDetection = true,
                    DuplicateDetectionHistoryTimeWindow = topic.DuplicateDetectionHistoryTimeWindow,
                };

            try
            {
                namespaceManager.CreateTopic(topicDescription);
            }
            catch (MessagingEntityAlreadyExistsException) { }
        }
Ejemplo n.º 4
0
        private void CreateSubscriptionIfNotExists(NamespaceManager namespaceManager, TopicSettings topic, SubscriptionSettings subscription)
        {
            var subscriptionDescription =
                new SubscriptionDescription(topic.Path, subscription.Name)
                {
                    RequiresSession = subscription.RequiresSession,
                    LockDuration = TimeSpan.FromSeconds(150),
                };

            try
            {
                namespaceManager.CreateSubscription(subscriptionDescription);
            }
            catch (MessagingEntityAlreadyExistsException) { }
        }
Ejemplo n.º 5
0
        private static void UpdateSubscriptionIfExists(NamespaceManager namespaceManager, TopicSettings topic, UpdateSubscriptionIfExists action, MessagingFactory messagingFactory)
        {
            if (string.IsNullOrWhiteSpace(action.Name)) throw new ArgumentException("action");
            if (string.IsNullOrWhiteSpace(action.SqlFilter)) throw new ArgumentException("action");

            UpdateSqlFilter(namespaceManager, action.SqlFilter, action.Name, topic.Path, messagingFactory);
        }
Ejemplo n.º 6
0
        private static void UpdateRules(NamespaceManager namespaceManager, TopicSettings topic, SubscriptionSettings subscription, MessagingFactory messagingFactory)
        {
            string sqlExpression = null;
            if (!string.IsNullOrWhiteSpace(subscription.SqlFilter))
            {
                sqlExpression = subscription.SqlFilter;
            }

            UpdateSqlFilter(namespaceManager, sqlExpression, subscription.Name, topic.Path, messagingFactory);
        }
Ejemplo n.º 7
0
        public void CreateTopicIfNotExists(TopicSettings topic)
        {
            var topicDescription =
                new TopicDescription(topic.Path)
                {
                    RequiresDuplicateDetection = true,
                    DuplicateDetectionHistoryTimeWindow = topic.DuplicateDetectionHistoryTimeWindow,
                };

            var namespaceManager = settings.GetNamespaceManager();
            namespaceManager.CreateTopic(topicDescription);
            CreateSubscriptionIfNotExists(namespaceManager, topic, new SubscriptionSettings
            {
                Name="log",
                RequiresSession=false
            });
        }