/// <summary>
        /// Unregisters a Service or Actor <paramref name="reference"/> as subscriber for messages of type <paramref name="messageTypeName"/>
        /// </summary>
        /// <param name="reference"></param>
        /// <param name="messageTypeName"></param>
        /// <returns></returns>
        private async Task UnregisterSubscriberAsync(ReferenceWrapper reference, string messageTypeName)
        {
            await WaitForInitializeAsync(CancellationToken.None);

            var myDictionary = await TimeoutRetryHelper.Execute((token, state) => StateManager.GetOrAddAsync <IReliableDictionary <string, BrokerServiceState> >(messageTypeName));

            var queueName           = CreateQueueName(reference, messageTypeName);
            var deadLetterQueueName = CreateDeadLetterQueueName(reference, messageTypeName);

            await TimeoutRetryHelper.ExecuteInTransaction(StateManager, async (tx, token, state) =>
            {
                var subscribers = await myDictionary.TryGetValueAsync(tx, Subscribers, LockMode.Update);
                if (subscribers.HasValue)
                {
                    var newState = BrokerServiceState.RemoveSubscriber(subscribers.Value, reference);
                    await myDictionary.SetAsync(tx, Subscribers, newState);
                }


                await StateManager.RemoveAsync(tx, queueName);
                await StateManager.RemoveAsync(tx, deadLetterQueueName);

                ServiceEventSourceMessage($"Unregistered subscriber: {reference.Name}");
                _queues.TryRemove(queueName, out reference);
            });
        }
        /// <summary>
        /// Registers a Service or Actor <paramref name="reference"/> as subscriber for messages of type <paramref name="messageTypeName"/>
        /// </summary>
        /// <param name="reference"></param>
        /// <param name="messageTypeName"></param>
        /// <returns></returns>
        private async Task RegisterSubscriberAsync(ReferenceWrapper reference, string messageTypeName)
        {
            await WaitForInitializeAsync(CancellationToken.None);

            var myDictionary = await TimeoutRetryHelper.Execute((token, state) => StateManager.GetOrAddAsync <IReliableDictionary <string, BrokerServiceState> >(messageTypeName));

            await TimeoutRetryHelper.ExecuteInTransaction(StateManager, async (tx, token, state) =>
            {
                var queueName           = CreateQueueName(reference, messageTypeName);
                var deadLetterQueueName = CreateDeadLetterQueueName(reference, messageTypeName);

                Func <string, BrokerServiceState> addValueFactory = key =>
                {
                    var newState   = new BrokerServiceState(messageTypeName);
                    var subscriber = new Reference(reference, queueName, deadLetterQueueName);
                    newState       = BrokerServiceState.AddSubscriber(newState, subscriber);
                    return(newState);
                };

                Func <string, BrokerServiceState, BrokerServiceState> updateValueFactory = (key, current) =>
                {
                    var subscriber = new Reference(reference, queueName, deadLetterQueueName);
                    var newState   = BrokerServiceState.AddSubscriber(current, subscriber);
                    return(newState);
                };

                await myDictionary.AddOrUpdateAsync(tx, Subscribers, addValueFactory, updateValueFactory);

                await StateManager.GetOrAddAsync <IReliableQueue <MessageWrapper> >(tx, queueName);
                await StateManager.GetOrAddAsync <IReliableQueue <MessageWrapper> >(tx, deadLetterQueueName);

                _queues.AddOrUpdate(queueName, reference, (key, old) => reference);
                ServiceEventSourceMessage($"Registered subscriber: {reference.Name}");
            }, cancellationToken : CancellationToken.None);
        }
Ejemplo n.º 3
0
        public async Task AddOrUpdate(string messageTypeName, ReferenceWrapper reference)
        {
            await Initialize(CancellationToken.None);

            var myDictionary = await GetStateFor(messageTypeName);

            await StateManager.ExecuteInTransaction(async (tx, token, state) =>
            {
                var queueName = CreateQueueName(reference, messageTypeName);

                BrokerServiceState AddValueFactory(string key)
                {
                    var newState   = new BrokerServiceState(messageTypeName);
                    var subscriber = new Reference(reference, queueName);
                    newState       = BrokerServiceState.AddSubscriber(newState, subscriber);
                    return(newState);
                }

                BrokerServiceState UpdateValueFactory(string key, BrokerServiceState current)
                {
                    var subscriber = new Reference(reference, queueName);
                    var newState   = BrokerServiceState.AddSubscriber(current, subscriber);
                    return(newState);
                }

                await myDictionary.AddOrUpdateAsync(tx, _storeKey, AddValueFactory, UpdateValueFactory);

                await CreateQueue(tx, queueName);

                _queues.AddOrUpdate(queueName, reference, (key, old) => reference);
            }, cancellationToken : CancellationToken.None);
        }
Ejemplo n.º 4
0
        public Task AddOrUpdate(string messageTypeName, ReferenceWrapper reference)
        {
            var myDictionary = _state.GetOrAdd(messageTypeName, s => new ConcurrentDictionary <string, BrokerServiceState>());

            var queueName = CreateQueueName(messageTypeName, reference);

            BrokerServiceState AddValueFactory(string key)
            {
                var newState   = new BrokerServiceState(messageTypeName);
                var subscriber = new Reference(reference, queueName);

                newState = BrokerServiceState.AddSubscriber(newState, subscriber);
                return(newState);
            }

            BrokerServiceState UpdateValueFactory(string key, BrokerServiceState current)
            {
                var subscriber = new Reference(reference, queueName);
                var newState   = BrokerServiceState.AddSubscriber(current, subscriber);

                return(newState);
            }

            myDictionary.AddOrUpdate(_storeKey, AddValueFactory, UpdateValueFactory);

            _queues.AddOrUpdate(queueName, reference, (key, old) => reference);

            return(Task.CompletedTask);
        }
Ejemplo n.º 5
0
        public Task Remove(string messageTypeName, ReferenceWrapper reference)
        {
            var myDictionary = _state.GetOrAdd(messageTypeName, s => new ConcurrentDictionary <string, BrokerServiceState>());

            var queueName = CreateQueueName(messageTypeName, reference);

            if (myDictionary.TryGetValue(_storeKey, out BrokerServiceState subscribers))
            {
                var newState = BrokerServiceState.RemoveSubscriber(subscribers, reference);
                myDictionary.AddOrUpdate(_storeKey, newState, (s, state) => newState);
            }

            _queues.TryRemove(queueName, out reference);

            return(Task.CompletedTask);
        }
Ejemplo n.º 6
0
        public async Task Remove(string messageTypeName, ReferenceWrapper reference)
        {
            await Initialize(CancellationToken.None);

            var myDictionary = await GetStateFor(messageTypeName);

            var queueName = CreateQueueName(reference, messageTypeName);

            await StateManager.ExecuteInTransaction(async (tx, token, state) =>
            {
                var subscribers = await myDictionary.TryGetValueAsync(tx, _storeKey, LockMode.Update);
                if (subscribers.HasValue)
                {
                    var newState = BrokerServiceState.RemoveSubscriber(subscribers.Value, reference);
                    await myDictionary.SetAsync(tx, _storeKey, newState);
                }

                await StateManager.RemoveAsync(tx, queueName);

                _queues.TryRemove(queueName, out reference);
            });
        }