示例#1
0
        private static void TestSequentialProcessingTaskViaActorProxy()
        {
            try
            {
                // Sets device name used in the ActorId constructor
                const string workerId = "worker01";

                // Creates actor proxy
                var proxy = ActorProxy.Create <IWorkerActor>(new ActorId(workerId), workerActorServiceUri);
                Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] ActorProxy for the [{workerId}] actor created.");

                // Subscribes to worker actor events
                var eventHandler = new WorkerActorEventHandler();
                proxy.SubscribeAsync <IWorkerActorEvents>(eventHandler).GetAwaiter().GetResult();
                Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Subscribed to the events raised by the [{workerId}] actor.");

                // Enqueues N messages. Note: the sequential message processing task emulates K steps of H seconds each to process each message.
                // However, since it runs on a separate task not awaited by the actor ProcessMessageAsync method,
                // the method itself returns immediately without waiting the the task completion.
                // This allows the actor to continue to enqueue requests, while processing messages on a separate task.
                var messageList = CreateMessageList();

                foreach (var message in messageList)
                {
                    proxy.StartSequentialProcessingAsync(message).Wait();
                    Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Message [{JsonSerializerHelper.Serialize(message)}] sent.");
                }

                while (proxy.IsSequentialProcessingRunningAsync().Result)
                {
                    Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Waiting for the sequential message processing task completion...");
                    Task.Delay(TimeSpan.FromSeconds(1)).Wait();
                }
                Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Sequential message processing task completed.");

                // Retrieves statistics
                var statistics = proxy.GetProcessingStatisticsAsync().Result;
                if (statistics == null)
                {
                    return;
                }

                // Unsubscribes from worker actor events
                proxy.UnsubscribeAsync <IWorkerActorEvents>(eventHandler).GetAwaiter().GetResult();
                Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Unsubscribed from the events raised by the [{workerId}] actor.");

                // Prints statistics
                PrintStatistics(statistics);
            }
            catch (Exception ex)
            {
                PrintException(ex);
            }
        }
示例#2
0
        private static void TestParallelMessageProcessingViaActorProxy()
        {
            try
            {
                // Sets device name used in the ActorId constructor
                const string workerId = "worker01";

                // Creates actor proxy
                var proxy = ActorProxy.Create <IWorkerActor>(new ActorId(workerId), workerActorServiceUri);
                Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] ActorProxy for the [{workerId}] actor created.");

                // Subscribes to worker actor events
                var eventHandler = new WorkerActorEventHandler();
                proxy.SubscribeAsync <IWorkerActorEvents>(eventHandler).GetAwaiter().GetResult();
                Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Subscribed to the events raised by the [{workerId}] actor.");

                // Creates N messages
                var messageList = CreateMessageList();

                var taskList = new List <Task>();
                Func <string, Task> waitHandler = async messageId =>
                {
                    try
                    {
                        while (await proxy.IsParallelProcessingRunningAsync(messageId))
                        {
                            Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Waiting for [{messageId}] parallel processing task completion...");
                            await Task.Delay(TimeSpan.FromSeconds(1));
                        }
                        Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] [{messageId}] Parallel message processing task completed.");
                    }
                    catch (Exception ex)
                    {
                        PrintException(ex);
                    }
                };

                // Start parallel processing
                foreach (var message in messageList)
                {
                    if (!proxy.StartParallelProcessingAsync(message).Result)
                    {
                        continue;
                    }
                    taskList.Add(waitHandler(message.MessageId));
                    Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Message [{JsonSerializerHelper.Serialize(message)}] sent.");
                }

                // Wait for message processing completion
                Task.WaitAll(taskList.ToArray());

                Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Parallel message processing tasks completed.");

                // Retrieves statistics
                var statistics = proxy.GetProcessingStatisticsAsync().Result;
                if (statistics == null)
                {
                    return;
                }

                // Unsubscribes from worker actor events
                proxy.UnsubscribeAsync <IWorkerActorEvents>(eventHandler).GetAwaiter().GetResult();
                Console.WriteLine($" - [{DateTime.Now.ToLocalTime()}] Unsubscribed from the events raised by the [{workerId}] actor.");

                // Prints statistics
                PrintStatistics(statistics);
            }
            catch (Exception ex)
            {
                PrintException(ex);
            }
        }