如何在azure worker角色中同时开始处理任务

时间:2014-01-24 12:26:56

标签: c# multithreading azure azure-worker-roles

我开发了工作角色应用程序来处理不同的任务。例如Task1,task2有100条记录,我已经将主题存储在队列中,我想同时开始处理并跨越多个实例加载。

将来会有更多的任务和任务内的记录将会增加。 那么我怎样才能改进以下方法来有效地处理记录呢?

目前我按顺序完成了代码

  private void ProcessTaskQueues()
        {

            var currentInterval1 = 0;
            var maxInterval1 = 15;
            var currentInterval2 = 0;
            var maxInterval2 = 15;
            string queueName1 = RoleEnvironment.GetConfigurationSettingValue("Task1Queue");
            CloudQueue queue1 = storageAccount.CreateCloudQueueClient().GetQueueReference(queueName1);
            queue1.CreateIfNotExists();
            string queueName2 = RoleEnvironment.GetConfigurationSettingValue("Task2Queue");
            CloudQueue queue2 = storageAccount.CreateCloudQueueClient().GetQueueReference(queueName2);
            queue2.CreateIfNotExists();

            while (true)
            {
                try
                {
                    TaskPerformer tp = new TaskPerformer();
                    // Task 1 
                    Trace.WriteLine(string.Format("[{0}] - [TASK1] Fetch Message queue", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    var cloudQueueMessage1 = queue1.GetMessage();
                    if (cloudQueueMessage1 != null)
                    {
                        currentInterval1 = 0;
                        if (cloudQueueMessage1.DequeueCount <= 1)
                        {
                            var item = cloudQueueMessage1.FromMessage<Task1Item>();
                            tp.ExecuteTask1(item);
                            Trace.WriteLine(string.Format("[{0}] - [TASK1] Message Executed for ID : {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), item.MLPID));
                            queue2.DeleteMessage(cloudQueueMessage1);
                        }
                    }
                    else
                    {
                        if (currentInterval1 < maxInterval1)
                        {
                            currentInterval1++;
                            Trace.WriteLine(string.Format("[{0}] - Waiting for {1} seconds", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), currentInterval1));
                        }
                        Thread.Sleep(TimeSpan.FromSeconds(currentInterval1));
                    }

                    // Task 2 
                    Trace.WriteLine(string.Format("[{0}] - [TASK2] Fetch Message queue", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    var cloudQueueMessage2 = queue2.GetMessage();
                    if (cloudQueueMessage2 != null)
                    {
                        currentInterval2 = 0;
                        if (cloudQueueMessage2.DequeueCount <= 1)
                        {

                            var dns = cloudQueueMessage2.FromMessage<DNS>();
                            tp.ExecuteTask2(dns);
                            Trace.WriteLine(string.Format("[{0}] - [TASK2] Message Executed for ID : {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), dns.ID));
                            queue2.DeleteMessage(cloudQueueMessage2);
                        }
                    }
                    else
                    {
                        if (currentInterval2 < maxInterval2)
                        {
                            currentInterval2++;
                            Trace.WriteLine(string.Format("[{0}] - Waiting for {1} seconds", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), currentInterval2));
                        }
                        Thread.Sleep(TimeSpan.FromSeconds(currentInterval2));
                    }
                }
                catch (Exception)
                { }
            }
        }

1 个答案:

答案 0 :(得分:0)

从这个问题的评论中,它引导我搜索备用解决方案,只有一个队列用于多种类型的数据项,我得到了解决方案

How to use one object to store multiple type of data