public void Run()
        {
            //初始化容器
            DelayPlanManager.Init();
            Debug.WriteLine($"延时队列初始化完成时间:{DateTime.Now}");

            //构造消费者
            System.Threading.Thread consumer = new System.Threading.Thread(() =>
            {
                while (true)
                {
                    DelayPlanManager.Read();
                    System.Threading.Thread.Sleep(1000);
                }
            });
            consumer.IsBackground = true;
            consumer.Start();

            var t = DateTime.Now.AddSeconds(30);
            Func <DelayQueueSlot, Task> callback = (result) =>
            {
                var np = result as NotifyPlan;
                //模拟业务
                Debug.WriteLine($"[{DateTime.Now}]ID:{np.Key},地址:{np.NotifyUrl},延迟秒数:{np.TimeSpan}");

                Assert.Equal(DateTime.Now, t);

                return(Task.CompletedTask);
            };

            Debug.WriteLine($"预期时间{t}");
            DelayPlanManager.Insert(new NotifyPlan
            {
                NotifyUrl = "",
                Key       = Guid.NewGuid().ToString(),
                Callback  = callback
            }, t);


            //模拟生产端写入任务
            Task[] tasks = new Task[10];
            for (int i = 0; i < 10; i++)
            {
                tasks[i] = new Task(() =>
                {
                    for (int k = 0; k < 200; k++)
                    {
                        int rndNum = new Random().Next(20, 500);
                        DelayPlanManager.Insert(new NotifyPlan
                        {
                            NotifyUrl = "",
                            Key       = Guid.NewGuid().ToString(),
                            Callback  = callback
                        }, DateTime.Now.AddSeconds(rndNum));
                    }
                }, TaskCreationOptions.LongRunning);
                tasks[i].Start();
            }
            Task.WaitAll(tasks);
        }
示例#2
0
        public IActionResult Remove([FromQuery] Guid sid)
        {
            bool success = DelayPlanManager.Remove(sid.ToString());

            if (success)
            {
                return(Ok());
            }
            return(BadRequest());
        }
示例#3
0
        public async Task <IActionResult> Insert([FromQuery] Guid sid)
        {
            bool success = await DelayPlanManager.InsertById(sid);

            if (success)
            {
                return(Ok());
            }
            return(BadRequest());
        }