Ejemplo n.º 1
0
        /// <summary>
        /// Returns a list of Dates that are the next fire times of a
        /// <see cref="Trigger" />.
        /// The input trigger will be cloned before any work is done, so you need
        /// not worry about its state being altered by this method.
        /// </summary>
        /// <param name="trigg">The trigger upon which to do the work</param>
        /// <param name="cal">The calendar to apply to the trigger's schedule</param>
        /// <param name="numTimes">The number of next fire times to produce</param>
        /// <returns>List of java.util.Date objects</returns>
        public static IList ComputeFireTimes(Trigger trigg, ICalendar cal, int numTimes)
        {
            ArrayList lst = new ArrayList();

            Trigger t = (Trigger)trigg.Clone();

            if (t.GetNextFireTimeUtc() == null || !t.GetNextFireTimeUtc().HasValue)
            {
                t.ComputeFirstFireTimeUtc(cal);
            }

            for (int i = 0; i < numTimes; i++)
            {
                NullableDateTime d = t.GetNextFireTimeUtc();
                if (d.HasValue)
                {
                    lst.Add(d);
                    t.Triggered(cal);
                }
                else
                {
                    break;
                }
            }

            return(ArrayList.ReadOnly(new ArrayList(lst)));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a list of Dates that are the next fire times of a  <see cref="Trigger" />
        /// that fall within the given date range. The input trigger will be cloned
        /// before any work is done, so you need not worry about its state being
        /// altered by this method.
        /// <p>
        /// NOTE: if this is a trigger that has previously fired within the given
        /// date range, then firings which have already occured will not be listed
        /// in the output List.
        /// </p>
        /// </summary>
        /// <param name="trigg">The trigger upon which to do the work</param>
        /// <param name="cal">The calendar to apply to the trigger's schedule</param>
        /// <param name="from">The starting date at which to find fire times</param>
        /// <param name="to">The ending date at which to stop finding fire times</param>
        /// <returns>List of java.util.Date objects</returns>
        public static IList ComputeFireTimesBetween(Trigger trigg, ICalendar cal, DateTime from, DateTime to)
        {
            ArrayList lst = new ArrayList();

            Trigger t = (Trigger)trigg.Clone();

            if (t.GetNextFireTimeUtc() == null || !t.GetNextFireTimeUtc().HasValue)
            {
                t.StartTimeUtc = from;
                t.EndTimeUtc   = to;
                t.ComputeFirstFireTimeUtc(cal);
            }

            // TODO: this method could be more efficient by using logic specific
            //        to the type of trigger ...
            while (true)
            {
                NullableDateTime d = t.GetNextFireTimeUtc();
                if (d.HasValue)
                {
                    if (d.Value < from)
                    {
                        t.Triggered(cal);
                        continue;
                    }
                    if (d.Value > to)
                    {
                        break;
                    }
                    lst.Add(d);
                    t.Triggered(cal);
                }
                else
                {
                    break;
                }
            }
            return(ArrayList.ReadOnly(new ArrayList(lst)));
        }
        /// <summary>
        /// Schedule the given <see cref="Trigger" /> with the
        /// <see cref="IJob" /> identified by the <see cref="Trigger" />'s settings.
        /// </summary>
        public virtual DateTime ScheduleJob(SchedulingContext ctxt, Trigger trigger)
        {
            ValidateState();

            if (trigger == null)
            {
                throw new SchedulerException("Trigger cannot be null",
                        SchedulerException.ErrorClientError);
            }

            trigger.Validate();

            ICalendar cal = null;
            if (trigger.CalendarName != null)
            {
                cal = resources.JobStore.RetrieveCalendar(ctxt, trigger.CalendarName);
                if (cal == null)
                {
                    throw new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Calendar not found: {0}", trigger.CalendarName),
                                                 SchedulerException.ErrorPersistenceCalendarDoesNotExist);
                }
            }

            NullableDateTime ft = trigger.ComputeFirstFireTimeUtc(cal);

            if (!ft.HasValue)
            {
                throw new SchedulerException("Based on configured schedule, the given trigger will never fire.",
                                             SchedulerException.ErrorClientError);
            }

            resources.JobStore.StoreTrigger(ctxt, trigger, false);
            NotifySchedulerThread(trigger.GetNextFireTimeUtc());
            NotifySchedulerListenersScheduled(trigger);

            return ft.Value;
        }
        /// <summary>
        /// Remove (delete) the <see cref="Trigger" /> with the
        /// given name, and store the new given one - which must be associated
        /// with the same job.
        /// </summary>
        /// <param name="ctxt">The scheduling context.</param>
        /// <param name="triggerName">The name of the <see cref="Trigger" /> to be removed.</param>
        /// <param name="groupName">The group name of the <see cref="Trigger" /> to be removed.</param>
        /// <param name="newTrigger">The new <see cref="Trigger" /> to be stored.</param>
        /// <returns>
        /// 	<see langword="null" /> if a <see cref="Trigger" /> with the given
        /// name and group was not found and removed from the store, otherwise
        /// the first fire time of the newly scheduled trigger.
        /// </returns>
        public virtual NullableDateTime RescheduleJob(SchedulingContext ctxt, string triggerName, string groupName, Trigger newTrigger)
        {
            ValidateState();

            if (groupName == null)
            {
                groupName = SchedulerConstants.DefaultGroup;
            }

            newTrigger.Validate();

            ICalendar cal = null;
            if (newTrigger.CalendarName != null)
            {
                cal = resources.JobStore.RetrieveCalendar(ctxt, newTrigger.CalendarName);
            }

            NullableDateTime ft = newTrigger.ComputeFirstFireTimeUtc(cal);

            if (!ft.HasValue)
            {
                throw new SchedulerException("Based on configured schedule, the given trigger will never fire.",
                                             SchedulerException.ErrorClientError);
            }

            if (resources.JobStore.ReplaceTrigger(ctxt, triggerName, groupName, newTrigger))
            {
                NotifySchedulerThread(newTrigger.GetNextFireTimeUtc());
                NotifySchedulerListenersUnscheduled(triggerName, groupName);
                NotifySchedulerListenersScheduled(newTrigger);
            }
            else
            {
                return null;
            }

            return ft;
        }
        /// <summary> 
        /// Add the <see cref="IJob" /> identified by the given
        /// <see cref="JobDetail" /> to the Scheduler, and
        /// associate the given <see cref="Trigger" /> with it.
        /// <p>
        /// If the given Trigger does not reference any <see cref="IJob" />, then it
        /// will be set to reference the Job passed with it into this method.
        /// </p>
        /// </summary>
        public virtual DateTime ScheduleJob(SchedulingContext ctxt, JobDetail jobDetail, Trigger trigger)
        {
            ValidateState();


            if (jobDetail == null)
            {
                throw new SchedulerException("JobDetail cannot be null",
                        SchedulerException.ErrorClientError);
            }

            if (trigger == null)
            {
                throw new SchedulerException("Trigger cannot be null",
                        SchedulerException.ErrorClientError);
            }

            jobDetail.Validate();

            if (trigger.JobName == null)
            {
                trigger.JobName = jobDetail.Name;
                trigger.JobGroup = jobDetail.Group;
            }
            else if (trigger.JobName != null && !trigger.JobName.Equals(jobDetail.Name))
            {
                throw new SchedulerException("Trigger does not reference given job!", SchedulerException.ErrorClientError);
            }
            else if (trigger.JobGroup != null && !trigger.JobGroup.Equals(jobDetail.Group))
            {
                throw new SchedulerException("Trigger does not reference given job!", SchedulerException.ErrorClientError);
            }

            trigger.Validate();

            ICalendar cal = null;
            if (trigger.CalendarName != null)
            {
                cal = resources.JobStore.RetrieveCalendar(ctxt, trigger.CalendarName);
                if (cal == null)
                {
                    throw new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Calendar not found: {0}", trigger.CalendarName),
                                                 SchedulerException.ErrorPersistenceCalendarDoesNotExist);
                }
            }

            NullableDateTime ft = trigger.ComputeFirstFireTimeUtc(cal);

            if (!ft.HasValue)
            {
                throw new SchedulerException("Based on configured schedule, the given trigger will never fire.",
                                             SchedulerException.ErrorClientError);
            }

            resources.JobStore.StoreJobAndTrigger(ctxt, jobDetail, trigger);
            NotifySchedulerThread(trigger.GetNextFireTimeUtc());
            NotifySchedulerListenersScheduled(trigger);

            return ft.Value;
        }