Exemplo n.º 1
0
        public override void Run()
        {
            Trace.TraceInformation("WorkerRole is running");

            Article article1 = new Article("123");
            Article article2 = new Article("456");
            Article article3 = new Article("789");

            article1.Identifier = "123";
            article2.Identifier = "456";
            article3.Identifier = "789";

            article1.Value = "Jagode";
            article2.Value = "Hleb";
            article3.Value = "Mleko";

            try
            {
                lock (lockObject)
                {
                    _repo.AddArticle(article1);
                    _repo.AddArticle(article2);
                    _repo.AddArticle(article3);
                }
            }
            catch (Exception e) { }

            NotifyOthersServer nos = new NotifyOthersServer();

            nos.Open();

            CloudQueue queue = QueueHelper.GetQueueReference("identifiers");

            while (true)
            {
                CloudQueueMessage message;

                lock (lockObject)
                {
                    message = queue.GetMessage();
                }

                if (message != null)
                {
                    Article article = _repo.GetArticle(message.AsString);

                    if (article != null)
                    {
                        Trace.TraceInformation("Artikal sa identifikatorom {0} je tipa: {1}.", article.Identifier, article.Value);
                        lock (lockObject)
                        {
                            queue.DeleteMessage(message);
                        }

                        List <EndpointAddress> internalEndpoints = RoleEnvironment.Roles[RoleEnvironment.CurrentRoleInstance.Role.Name]
                                                                   .Instances.Where(instance => instance.Id != RoleEnvironment.CurrentRoleInstance.Id)
                                                                   .Select(process => new EndpointAddress(
                                                                               String.Format("net.tcp://{0}/{1}", process.InstanceEndpoints[internalEndpointName].IPEndpoint.ToString(),
                                                                                             internalEndpointName))).ToList();

                        int brotherInstances = internalEndpoints.Count;

                        Task [] tasks = new Task [brotherInstances];
                        for (int i = 0; i < brotherInstances; i++)
                        {
                            int  index  = i;
                            Task notify = new Task(() =>
                            {
                                INotifyOthers proxy = new ChannelFactory <INotifyOthers>(binding, internalEndpoints[index]).CreateChannel();
                                proxy.Notify(article.Value);
                            });

                            notify.Start();
                            tasks[index] = notify;
                        }
                        Task.WaitAll(tasks);
                    }
                    else
                    {
                        Trace.TraceInformation("Identifikator {0} je neispravan!", message.AsString);
                        lock (lockObject)
                        {
                            queue.DeleteMessage(message);
                        }
                    }
                }

                Thread.Sleep(5000);
            }
        }