Example #1
0
        public ISubscription Subscribe(Action <string> messageReceivedAction)
        {
            var tokenSource = new CancellationTokenSource();

            SafeExecute.Sync(() => ThreadPool.QueueUserWorkItem(s => {
                var cancellationToken = (CancellationToken)s;
                while (!cancellationToken.IsCancellationRequested)
                {
                    SafeExecute.SyncCatch(
                        () => PollMessage(messageReceivedAction, cancellationToken),
                        () => Thread.Sleep(_options.WaitDuration));
                }
            }, tokenSource.Token));
            return(new DefaultSubscription(() => tokenSource.Cancel()));
        }
Example #2
0
        private void PollMessage(Action <string> messageReceivedAction, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                SafeExecute.SyncCatch(() =>
                {
                    var link    = GetLink();
                    var message = link.Receive(TimeSpan.FromMilliseconds(_options.PollingInterval));
                    if (message == null)
                    {
                        Thread.Sleep(_options.WaitDuration);
                        return;
                    }

                    link.Accept(message);
                    messageReceivedAction.Invoke((string)message.Body);
                }, () => Thread.Sleep(_options.WaitDuration));
            }
        }
Example #3
0
        public bool Load()
        {
            if (_options.DependencyCacheOptions?.Enabled != true)
            {
                return(false);
            }

            var filePath = string.IsNullOrEmpty(_options.DependencyCacheOptions.FileLocation) ? _options.DependencyCacheOptions.FileName :
                           Path.Combine(_options.DependencyCacheOptions.FileLocation, _options.DependencyCacheOptions.FileName);

            var content = LoadCache(filePath);

            if (string.IsNullOrEmpty(content))
            {
                return(false);
            }

            var model = _serializer.TryDeserializeObject <DependencyCacheModel>(content);

            if (model == null || !VerifyAssembles(model.Assemblies))
            {
                return(false);
            }

            if (model.TypeHandlers?.Any() != true)
            {
                return(false);
            }

            var loadSuccess = SafeExecute.Sync(() => {
                foreach (var item in model.TypeHandlers)
                {
                    var type    = Type.GetType(item.Name);
                    var handler = _serviceProvider.GetService(type) as ITypeHandler;
                    handler.LoadCache(item.Sections);
                }
            });

            return(loadSuccess);
        }
Example #4
0
        public IObservable <T> GetObservableTopic <T>(string topic, QueueHandler handler = null) where T : class
        {
            return(Observable.Create <T>(observer => {
                var tokenSource = handler != null ? handler.TokenSource : new CancellationTokenSource();
                var stoppingToken = tokenSource.Token;

                ThreadPool.QueueUserWorkItem(s => {
                    var token = (CancellationToken)s;

                    var consumer = CreateConsumer(topic);

                    while (!token.IsCancellationRequested)
                    {
                        while (handler != null && handler.Wait)
                        {
                            handler.LastActivityTime = DateTimeOffset.Now;
                            Wait();
                        }

                        var pollingInterval = 500;
                        SafeExecute.Sync(() => pollingInterval = _options.Value.QueuePollingInterval);
                        if (pollingInterval < 500)
                        {
                            pollingInterval = 500;
                        }

                        var messageText = string.Empty;

                        try
                        {
                            messageText = PollMessage(consumer, pollingInterval).RunSync();

                            if (handler != null)
                            {
                                handler.LastActivityTime = DateTimeOffset.Now;
                            }

                            if (string.IsNullOrEmpty(messageText))
                            {
                                Wait();
                                continue;
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // do nothing
                        }
                        catch (Exception e)
                        {
                            StaticLog.Error(e, "Queue consume error: {0}");
                        }

                        try
                        {
                            var messageWrapper = _serializer.DeserializeObject <QueueMessage>(messageText);
                            var item = _serializer.DeserializeObject(messageWrapper.Data, messageWrapper.Type) as T;
                            observer.OnNext(item);
                        }
                        catch (Exception e)
                        {
                            StaticLog.Error($"Deserialise message error: {messageText} - {e.Message}");
                        }
                    }
                }, stoppingToken);

                return () => {
                    tokenSource.Cancel();
                };
            }));
        }