Пример #1
0
            protected override IEnumerator <AsyncStep> GetAsyncSteps()
            {
                this.owner.namespaceManager = this.serviceBusFactory.CreateNamespaceManager();

                for (int partitionId = 0; partitionId < this.owner.partitionCount; partitionId++)
                {
                    // Create topic if not exists.
                    string topicPath = this.owner.GetTopicPath(partitionId);

                    bool topicExist = false;

                    yield return(this.CallAsync(
                                     (thisPtr, t, c, s) => thisPtr.owner.namespaceManager.BeginTopicExists(topicPath, c, s),
                                     (thisPtr, r) => topicExist = thisPtr.owner.namespaceManager.EndTopicExists(r),
                                     ExceptionPolicy.Transfer));

                    if (!topicExist)
                    {
                        TopicDescription topicDescription = new TopicDescription(topicPath);

                        yield return(this.CallAsync(
                                         (thisPtr, t, c, s) => thisPtr.owner.namespaceManager.BeginCreateTopic(topicDescription, c, s),
                                         (thisPtr, r) => topicDescription = thisPtr.owner.namespaceManager.EndCreateTopic(r),
                                         ExceptionPolicy.Transfer));
                    }

                    for (int nodeId = 0; nodeId < this.owner.nodeCount; nodeId++)
                    {
                        // Create subscriptions if not exist
                        string subscriptionName = GetSubscriptionName(nodeId);

                        bool subscriptionExists = false;

                        yield return(this.CallAsync(
                                         (thisPtr, t, c, s) => thisPtr.owner.namespaceManager.BeginSubscriptionExists(topicPath, subscriptionName, c, s),
                                         (thisPtr, r) => subscriptionExists = thisPtr.owner.namespaceManager.EndSubscriptionExists(r),
                                         ExceptionPolicy.Transfer));

                        if (!subscriptionExists)
                        {
                            SubscriptionDescription subscriptionDescription = new SubscriptionDescription(topicPath, subscriptionName)
                            {
                                RequiresSession = false
                            };

                            yield return(this.CallAsync(
                                             (thisPtr, t, c, s) => thisPtr.owner.namespaceManager.BeginCreateSubscription(subscriptionDescription, c, s),
                                             (thisPtr, r) => thisPtr.owner.namespaceManager.EndCreateSubscription(r),
                                             ExceptionPolicy.Transfer));
                        }
                    }
                }

                for (int partitionId = 0; partitionId < this.owner.partitionCount; partitionId++)
                {
                    MessagingFactory factory = null;

                    yield return(this.CallAsync(
                                     (thisPtr, t, c, s) => thisPtr.serviceBusFactory.BeginCreateMessagingFactory(c, s),
                                     (thisPtr, r) => factory = thisPtr.serviceBusFactory.EndCreateMessagingFactory(r),
                                     ExceptionPolicy.Transfer));

                    this.owner.factories.Add(partitionId, factory);

                    string topicPath              = this.owner.GetTopicPath(partitionId);
                    string subscriptionName       = GetSubscriptionName(this.owner.nodeId);
                    string subscriptionEntityPath = SubscriptionClient.FormatSubscriptionPath(topicPath, subscriptionName);

                    MessageSender sender = null;

                    yield return(this.CallAsync(
                                     (thisPtr, t, c, s) => factory.BeginCreateMessageSender(topicPath, c, s),
                                     (thisPtr, r) => sender = factory.EndCreateMessageSender(r),
                                     ExceptionPolicy.Transfer));

                    this.owner.senders.Add(partitionId, sender);

                    MessageReceiver receiver = null;

                    yield return(this.CallAsync(
                                     (thisPtr, t, c, s) => factory.BeginCreateMessageReceiver(subscriptionEntityPath, ReceiveMode.ReceiveAndDelete, c, s),
                                     (thisPtr, r) => receiver = factory.EndCreateMessageReceiver(r),
                                     ExceptionPolicy.Transfer));

                    var pump = new MessagePump(topicPath, receiver, this.owner.inputQueue, this.owner.semaphore);
                    this.owner.pumps.Add(partitionId, pump);

                    pump.Start();
                }

                this.owner.dispatcher = new MessageDispatcher(this.owner.inputQueue, this.owner.onReceivedAsync);
                this.owner.dispatcher.Start();
            }