Package org.quartz

Class TriggerBuilder<T extends Trigger>


  • public class TriggerBuilder<T extends Trigger>
    extends java.lang.Object
    TriggerBuilder is used to instantiate Triggers.

    The builder will always try to keep itself in a valid state, with reasonable defaults set for calling build() at any point. For instance if you do not invoke withSchedule(..) method, a default schedule of firing once immediately will be used. As another example, if you do not invoked withIdentity(..) a trigger name will be generated for you.

    Quartz provides a builder-style API for constructing scheduling-related entities via a Domain-Specific Language (DSL). The DSL can best be utilized through the usage of static imports of the methods on the classes TriggerBuilder, JobBuilder, DateBuilder, JobKey, TriggerKey and the various ScheduleBuilder implementations.

    Client code can then use the DSL to write code such as this:

             JobDetail job = newJob(MyJob.class)
                 .withIdentity("myJob")
                 .build();
                 
             Trigger trigger = newTrigger() 
                 .withIdentity(triggerKey("myTrigger", "myTriggerGroup"))
                 .withSchedule(simpleSchedule()
                     .withIntervalInHours(1)
                     .repeatForever())
                 .startAt(futureDate(10, MINUTES))
                 .build();
             
             scheduler.scheduleJob(job, trigger);
     
    See Also:
    JobBuilder, ScheduleBuilder, DateBuilder, Trigger
    • Method Detail

      • newTrigger

        public static TriggerBuilder<Trigger> newTrigger()
        Create a new TriggerBuilder with which to define a specification for a Trigger.
        Returns:
        the new TriggerBuilder
      • build

        public T build()
        Produce the Trigger.
        Returns:
        a Trigger that meets the specifications of the builder.
      • withIdentity

        public TriggerBuilder<T> withIdentity​(java.lang.String name)
        Use a TriggerKey with the given name and default group to identify the Trigger.

        If none of the 'withIdentity' methods are set on the TriggerBuilder, then a random, unique TriggerKey will be generated.

        Parameters:
        name - the name element for the Trigger's TriggerKey
        Returns:
        the updated TriggerBuilder
        See Also:
        TriggerKey, Trigger.getKey()
      • withIdentity

        public TriggerBuilder<T> withIdentity​(java.lang.String name,
                                              java.lang.String group)
        Use a TriggerKey with the given name and group to identify the Trigger.

        If none of the 'withIdentity' methods are set on the TriggerBuilder, then a random, unique TriggerKey will be generated.

        Parameters:
        name - the name element for the Trigger's TriggerKey
        group - the group element for the Trigger's TriggerKey
        Returns:
        the updated TriggerBuilder
        See Also:
        TriggerKey, Trigger.getKey()
      • withIdentity

        public TriggerBuilder<T> withIdentity​(TriggerKey triggerKey)
        Use the given TriggerKey to identify the Trigger.

        If none of the 'withIdentity' methods are set on the TriggerBuilder, then a random, unique TriggerKey will be generated.

        Parameters:
        triggerKey - the TriggerKey for the Trigger to be built
        Returns:
        the updated TriggerBuilder
        See Also:
        TriggerKey, Trigger.getKey()
      • withDescription

        public TriggerBuilder<T> withDescription​(java.lang.String triggerDescription)
        Set the given (human-meaningful) description of the Trigger.
        Parameters:
        triggerDescription - the description for the Trigger
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.getDescription()
      • withPriority

        public TriggerBuilder<T> withPriority​(int triggerPriority)
        Set the Trigger's priority. When more than one Trigger have the same fire time, the scheduler will fire the one with the highest priority first.
        Parameters:
        triggerPriority - the priority for the Trigger
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.DEFAULT_PRIORITY, Trigger.getPriority()
      • modifiedByCalendar

        public TriggerBuilder<T> modifiedByCalendar​(java.lang.String calName)
        Set the name of the Calendar that should be applied to this Trigger's schedule.
        Parameters:
        calName - the name of the Calendar to reference.
        Returns:
        the updated TriggerBuilder
        See Also:
        Calendar, Trigger.getCalendarName()
      • startAt

        public TriggerBuilder<T> startAt​(java.util.Date triggerStartTime)
        Set the time the Trigger should start at - the trigger may or may not fire at this time - depending upon the schedule configured for the Trigger. However the Trigger will NOT fire before this time, regardless of the Trigger's schedule.
        Parameters:
        triggerStartTime - the start time for the Trigger.
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.getStartTime(), DateBuilder
      • startNow

        public TriggerBuilder<T> startNow()
        Set the time the Trigger should start at to the current moment - the trigger may or may not fire at this time - depending upon the schedule configured for the Trigger.
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.getStartTime()
      • endAt

        public TriggerBuilder<T> endAt​(java.util.Date triggerEndTime)
        Set the time at which the Trigger will no longer fire - even if it's schedule has remaining repeats.
        Parameters:
        triggerEndTime - the end time for the Trigger. If null, the end time is indefinite.
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.getEndTime(), DateBuilder
      • forJob

        public TriggerBuilder<T> forJob​(JobKey keyOfJobToFire)
        Set the identity of the Job which should be fired by the produced Trigger.
        Parameters:
        keyOfJobToFire - the identity of the Job to fire.
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.getJobKey()
      • forJob

        public TriggerBuilder<T> forJob​(java.lang.String jobName)
        Set the identity of the Job which should be fired by the produced Trigger - a JobKey will be produced with the given name and default group.
        Parameters:
        jobName - the name of the job (in default group) to fire.
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.getJobKey()
      • forJob

        public TriggerBuilder<T> forJob​(java.lang.String jobName,
                                        java.lang.String jobGroup)
        Set the identity of the Job which should be fired by the produced Trigger - a JobKey will be produced with the given name and group.
        Parameters:
        jobName - the name of the job to fire.
        jobGroup - the group of the job to fire.
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.getJobKey()
      • forJob

        public TriggerBuilder<T> forJob​(JobDetail jobDetail)
        Set the identity of the Job which should be fired by the produced Trigger, by extracting the JobKey from the given job.
        Parameters:
        jobDetail - the Job to fire.
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.getJobKey()
      • usingJobData

        public TriggerBuilder<T> usingJobData​(JobDataMap newJobDataMap)
        Set the Trigger's JobDataMap, adding any values to it that were already set on this TriggerBuilder using any of the other 'usingJobData' methods.
        Returns:
        the updated TriggerBuilder
        See Also:
        Trigger.getJobDataMap()