Exemplo n.º 1
0
        public void TestTriggerFinalized()
        {
            Qtz205TriggerListener triggerListener   = new Qtz205TriggerListener();
            Qtz205ScheListener    schedulerListener = new Qtz205ScheListener();
            NameValueCollection   props             = new NameValueCollection();

            props["quartz.scheduler.idleWaitTime"] = "1500";
            props["quartz.threadPool.threadCount"] = "2";
            IScheduler scheduler = new StdSchedulerFactory(props).GetScheduler();

            scheduler.ListenerManager.AddSchedulerListener(schedulerListener);
            scheduler.ListenerManager.AddTriggerListener(triggerListener);

            IJobDetail job     = JobBuilder.Create <Qtz205Job>().WithIdentity("test").Build();
            ITrigger   trigger = TriggerBuilder.Create()
                                 .WithIdentity("test")
                                 .WithSchedule(SimpleScheduleBuilder.RepeatSecondlyForTotalCount(3))
                                 .Build();

            scheduler.ScheduleJob(job, trigger);
            scheduler.Start();
            Thread.Sleep(5000);

            scheduler.Shutdown(true);

            Assert.AreEqual(2, jobExecutionCount);
            Assert.AreEqual(3, triggerListener.FireCount);
            Assert.AreEqual(1, schedulerListener.TriggerFinalizedCount);
        }
Exemplo n.º 2
0
        internal void Test_can_create_job_and_send_message_based_on_quartz_trigger()
        {
            // Arrange
            var schedule = SimpleScheduleBuilder
                           .RepeatSecondlyForTotalCount(1)
                           .WithMisfireHandlingInstructionFireNow();

            var jobKey  = new JobKey("unit-test-job", "unit-testing");
            var trigger = TriggerBuilder
                          .Create()
                          .WithIdentity(jobKey.Name, jobKey.Group)
                          .WithSchedule(schedule)
                          .Build();

            var createJob = new CreateJob(
                this.receiver,
                new ConnectSession(Guid.NewGuid(), this.clock.TimeNow()),
                jobKey,
                trigger,
                Guid.NewGuid(),
                this.clock.TimeNow());

            // Act
            this.scheduler.Endpoint.Send(createJob);

            // Assert
            // TODO: Write assertions
        }
Exemplo n.º 3
0
        private static ITrigger DefineIntervalTrigger(string identity, DateTime startTime, IntervalType intervalType, int span, int count)
        {
            var trigger = TriggerBuilder.Create()
                          .WithIdentity(identity, Guid.NewGuid().ToString());

            trigger.StartAt(startTime);
            switch (intervalType)
            {
            case IntervalType.Hourly:
                trigger.WithSchedule(count == 0
                        ? SimpleScheduleBuilder.RepeatHourlyForever(span)
                        : SimpleScheduleBuilder.RepeatHourlyForTotalCount(count, span));
                break;

            case IntervalType.Minutely:
                trigger.WithSchedule(count == 0
                        ? SimpleScheduleBuilder.RepeatMinutelyForever(span)
                        : SimpleScheduleBuilder.RepeatMinutelyForTotalCount(count, span));
                break;

            case IntervalType.Secondly:
                trigger.WithSchedule(count == 0
                        ? SimpleScheduleBuilder.RepeatSecondlyForever(span)
                        : SimpleScheduleBuilder.RepeatSecondlyForTotalCount(count, span));
                break;

            case IntervalType.None:
                break;

            default:
                break;
            }
            return(trigger.Build());
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            //Create the scheduler factory
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

            //Ask the scheduler factory for a scheduler
            IScheduler scheduler = schedulerFactory.GetScheduler();

            //Start the scheduler so that it can start executing jobs
            scheduler.Start();

            // Create a job of Type WriteToConsoleJob
            IJobDetail job = JobBuilder.Create(typeof(WriteToConsoleJob)).WithIdentity("MyJob", "MyJobGroup").Build();

            //Schedule this job to execute every second, a maximum of 10 times
            ITrigger trigger = TriggerBuilder.Create().WithSchedule(SimpleScheduleBuilder.RepeatSecondlyForTotalCount(10)).StartNow().WithIdentity("MyJobTrigger", "MyJobTriggerGroup").Build();

            scheduler.ScheduleJob(job, trigger);

            //Wait for a key press. If we don't wait the program exits and the scheduler gets destroyed
            Console.ReadKey();

            //A nice way to stop the scheduler, waiting for jobs that are running to finish
            scheduler.Shutdown(true);
        }
        public static void ConfigureBackgroundJobs(IContainer container, ServiceConfigurator <ITopshelfService> service)
        {
            var jobRegistrations =
                container.ComponentRegistry.Registrations.Where(
                    x =>
                    x.Metadata.ContainsKey(JobConfigurationMetaDataItemNames.ComponentType) &&
                    x.Metadata[JobConfigurationMetaDataItemNames.ComponentType].ToString().Equals(typeof(IMessageDispatcherJob).Name, StringComparison.CurrentCultureIgnoreCase)
                    );

            foreach (var jobRegistration in jobRegistrations)
            {
                var componentRegistration = jobRegistration;

                if ((componentRegistration.Services.First() as KeyedService) == null)
                {
                    continue;
                }

                var jobName = ((KeyedService)componentRegistration.Services.First()).ServiceKey.ToString();
                var jobRepeatFrequencyInSeconds = componentRegistration.Metadata.ContainsKey(JobConfigurationMetaDataItemNames.RepeatFrequencyInSeconds)
                    ? int.Parse(componentRegistration.Metadata[JobConfigurationMetaDataItemNames.RepeatFrequencyInSeconds].ToString())
                    : int.Parse(JobConfigurationMetaDataItemValues.RepeatFrequencyInSeconds);
                var jobRepeatCount = componentRegistration.Metadata.ContainsKey(JobConfigurationMetaDataItemNames.RepeatCount)
                    ? int.Parse(componentRegistration.Metadata[JobConfigurationMetaDataItemNames.RepeatCount].ToString())
                    : int.Parse(JobConfigurationMetaDataItemValues.RepeatCount);
                var componentFullName = componentRegistration.Activator.LimitType.FullName;
                var componentName     = componentRegistration.Activator.LimitType.Name;
                var componentType     = componentRegistration.Activator.LimitType.Assembly.GetType(componentFullName);

                service.ScheduleQuartzJob(q =>
                {
                    q.WithJob(() =>
                    {
                        var jobDetail = new JobDetailImpl(jobName, null, componentType);

                        jobDetail.JobDataMap.Put(JobDataMapKeys.Name, jobName);
                        jobDetail.JobDataMap.Put(JobDataMapKeys.ComponentName, componentName);
                        jobDetail.JobDataMap.Put(JobDataMapKeys.ComponentFullName, componentName);

                        foreach (var item in componentRegistration.Metadata)
                        {
                            jobDetail.JobDataMap.Put(item.Key, item.Value);
                        }

                        return(jobDetail);
                    });

                    q.AddTrigger(() => TriggerBuilder.Create()
                                 .WithIdentity(string.Format("{0}-Trigger", jobName), null)
                                 .WithSchedule(jobRepeatCount > 0
                            ? SimpleScheduleBuilder.RepeatSecondlyForTotalCount(jobRepeatCount, jobRepeatFrequencyInSeconds)
                            : SimpleScheduleBuilder.RepeatSecondlyForever(jobRepeatFrequencyInSeconds)
                                               )
                                 .Build());
                });
            }
        }
Exemplo n.º 6
0
        private static ITrigger DefineIntervalTrigger(string _identity, DateTime _startTime, IntervalType _intervalType, int span, int count)
        {
            ITrigger trigger;
            var      _trigger = TriggerBuilder.Create()
                                .WithIdentity(_identity, Guid.NewGuid().ToString());

            _trigger.StartAt(_startTime);
            switch (_intervalType)
            {
            case IntervalType.Hourly:
                if (count == 0)
                {
                    _trigger.WithSchedule(SimpleScheduleBuilder.RepeatHourlyForever(span));
                }
                else
                {
                    _trigger.WithSchedule(SimpleScheduleBuilder.RepeatHourlyForTotalCount(count, span));
                }
                break;

            case IntervalType.Minutely:
                if (count == 0)
                {
                    _trigger.WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever(span));
                }
                else
                {
                    _trigger.WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForTotalCount(count, span));
                }
                break;

            case IntervalType.Secondly:
                if (count == 0)
                {
                    _trigger.WithSchedule(SimpleScheduleBuilder.RepeatSecondlyForever(span));
                }
                else
                {
                    _trigger.WithSchedule(SimpleScheduleBuilder.RepeatSecondlyForTotalCount(count, span));
                }
                break;

            default:
                break;
            }
            trigger = _trigger.Build();
            return(trigger);
        }
Exemplo n.º 7
0
        public void AddAndScheduleGenericJob <T>(string jobName, string jobGroupName, string triggerName, string triggerGroupName, QuickRepeatIntervals quickRepeatInterval, int intervalValue, int?nTimes) where T : class
        {
            IScheduler scheduler = GetScheduler();

            JobDetailImpl jobDetail = new JobDetailImpl(jobName, jobGroupName, typeof(T));

            ITrigger trigger = null;

            switch (quickRepeatInterval)
            {
            case QuickRepeatIntervals.EverySecond:
                trigger = TriggerBuilder.Create()
                          .StartNow()
                          .WithIdentity(triggerName, triggerGroupName)
                          .WithSchedule(nTimes.HasValue ? SimpleScheduleBuilder.RepeatSecondlyForTotalCount(nTimes.Value, intervalValue) : SimpleScheduleBuilder.RepeatSecondlyForever(intervalValue))
                          .Build();
                break;

            case QuickRepeatIntervals.EveryMinute:
                trigger = TriggerBuilder.Create()
                          .StartNow()
                          .WithIdentity(triggerName, triggerGroupName)
                          .WithSchedule(nTimes.HasValue ? SimpleScheduleBuilder.RepeatMinutelyForTotalCount(nTimes.Value, intervalValue) : SimpleScheduleBuilder.RepeatMinutelyForever(intervalValue))
                          .Build();
                break;

            case QuickRepeatIntervals.EveryHour:
                trigger = TriggerBuilder.Create()
                          .StartNow()
                          .WithIdentity(triggerName, triggerGroupName)
                          .WithSchedule(nTimes.HasValue ? SimpleScheduleBuilder.RepeatHourlyForTotalCount(nTimes.Value, intervalValue) : SimpleScheduleBuilder.RepeatHourlyForever(intervalValue))
                          .Build();
                break;
            }

            scheduler.ScheduleJob(jobDetail, trigger);
            //scheduler.Shutdown();
        }