Exemplo n.º 1
0
        public void TestResumeCrontab()
        {
            //开始调度
            TimeSpan           timeSpan           = new TimeSpan(0, 0, 2);
            RecurrenceStrategy recurrenceStrategy = new RecurrenceStrategy(timeSpan);
            ShowTimeCrontab    showTimeCrontab    = new ShowTimeCrontab("Hello World !", recurrenceStrategy);

            Assert.IsTrue(showTimeCrontab.Count == 0);

            ScheduleMediator.Schedule(showTimeCrontab, 0);

            //线程睡眠
            Thread.Sleep(5000);

            Assert.IsTrue(showTimeCrontab.Count == 3);

            //暂停
            ScheduleMediator.Pause(showTimeCrontab.Id);

            //线程睡眠
            Thread.Sleep(5000);

            Assert.IsTrue(showTimeCrontab.Count == 3);

            //恢复
            ScheduleMediator.Resume(showTimeCrontab.Id);

            //线程睡眠
            Thread.Sleep(5000);

            Assert.IsTrue(showTimeCrontab.Count > 3);
        }
Exemplo n.º 2
0
        //方法

        #region # 获取执行策略 —— static ExecutionStrategy GetExecutionStrategy(string strategyType...
        /// <summary>
        /// 获取执行策略
        /// </summary>
        /// <param name="strategyType">策略类型</param>
        /// <param name="strategy">策略</param>
        /// <returns>执行策略</returns>
        public static ExecutionStrategy GetExecutionStrategy(string strategyType, string strategy)
        {
            if (strategyType == nameof(FixedTimeStrategy))
            {
                DateTime          triggerTime       = DateTime.Parse(strategy);
                FixedTimeStrategy fixedTimeStrategy = new FixedTimeStrategy(triggerTime);

                return(fixedTimeStrategy);
            }
            if (strategyType == nameof(RepeatedTimeStrategy))
            {
                TimeSpan             triggerTime          = TimeSpan.Parse(strategy);
                RepeatedTimeStrategy repeatedTimeStrategy = new RepeatedTimeStrategy(triggerTime);

                return(repeatedTimeStrategy);
            }
            if (strategyType == nameof(RecurrenceStrategy))
            {
                TimeSpan           interval           = TimeSpan.Parse(strategy);
                RecurrenceStrategy recurrenceStrategy = new RecurrenceStrategy(interval);

                return(recurrenceStrategy);
            }
            if (strategyType == nameof(CronExpressionStrategy))
            {
                CronExpressionStrategy cronExpressionStrategy = new CronExpressionStrategy(strategy);

                return(cronExpressionStrategy);
            }

            throw new NotSupportedException("无此类型执行策略");
        }
Exemplo n.º 3
0
        public void TestException()
        {
            string log = $"{AppDomain.CurrentDomain.BaseDirectory}\\ScheduleLogs\\ExceptionLogs\\{DateTime.Today:yyyyMMdd}.txt";

            //开始调度
            TimeSpan           timeSpan           = new TimeSpan(0, 0, 2);
            RecurrenceStrategy recurrenceStrategy = new RecurrenceStrategy(timeSpan);
            ShowTimeCrontab    showTimeCrontab    = new ShowTimeCrontab("Exception", recurrenceStrategy);

            ScheduleMediator.Schedule(showTimeCrontab, 0);

            //线程睡眠
            Thread.Sleep(5100);

            Assert.IsTrue(File.Exists(log));
        }
Exemplo n.º 4
0
        public void TestScheduleInspect()
        {
            //开始调度
            TimeSpan           timeSpan           = new TimeSpan(0, 0, 2);
            RecurrenceStrategy recurrenceStrategy = new RecurrenceStrategy(timeSpan);
            ShowTimeCrontab    showTimeCrontab    = new ShowTimeCrontab("Hello World !", recurrenceStrategy);

            Assert.IsTrue(showTimeCrontab.Count == 0);

            ScheduleMediator.Schedule(showTimeCrontab);

            //线程睡眠
            Thread.Sleep(5000);

            Assert.IsTrue(showTimeCrontab.Count == 3);
        }
Exemplo n.º 5
0
        public void TestFindAllCrontabs()
        {
            TimeSpan               timeSpan               = new TimeSpan(0, 0, 2);
            RecurrenceStrategy     recurrenceStrategy     = new RecurrenceStrategy(timeSpan);
            string                 cronExpression         = DateTime.Now.AddSeconds(2).ToCronExpression();
            CronExpressionStrategy cronExpressionStrategy = new CronExpressionStrategy(cronExpression);

            ShowTimeCrontab showTimeCrontab = new ShowTimeCrontab("Hello World !", recurrenceStrategy);
            AlarmCrontab    alarmCrontab    = new AlarmCrontab("Hello World !", cronExpressionStrategy);

            ScheduleMediator.Schedule(showTimeCrontab, 0);
            ScheduleMediator.Schedule(alarmCrontab, 0);

            IList <ICrontab> crontabs = ScheduleMediator.FindAll();

            Assert.IsTrue(crontabs.Count == 2);
        }