Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.schedule.quartz;
 
 import java.util.Map;
 
 
 import org.quartz.Job;
 
Global scheduler instance.

Author(s):
Philip Helger
 
 public class GlobalQuartzScheduler extends GlobalSingleton
 {
   public static final String GROUP_NAME = "com.helger";
 
   private static final Logger s_aLogger = LoggerFactory.getLogger (GlobalQuartzScheduler.class);
 
   private final Scheduler m_aScheduler;
   private String m_sGroupName = ;
 
   @DevelopersNote ("Used from derived classes")
   public GlobalQuartzScheduler ()
   {
     // main scheduler
      = QuartzSchedulerHelper.getScheduler ();
 
     // Always add the statistics listener
   }
 
   @Nonnull
   public static final GlobalQuartzScheduler getInstance ()
   {
     return getGlobalSingleton (GlobalQuartzScheduler.class);
   }

  

Returns:
The Quartz internal group name to be used. Defaults to GROUP_NAME.
 
   @Nonnull
   @Nonempty
   public String getGroupName ()
   {
     return ;
   }

  
Set the Quartz internal group name.

Parameters:
sGroupName The new group name to be used. May neither be null nor empty.
 
   public void setGroupName (@Nonnull @Nonempty final String sGroupName)
   {
     ValueEnforcer.notEmpty (sGroupName"GroupName");
     = sGroupName;
  }

  
Add a job listener for all jobs.

Parameters:
aJobListener The job listener to be added. May not be null.
  public void addJobListener (@Nonnull final JobListener aJobListener)
  {
    ValueEnforcer.notNull (aJobListener"JobListener");
    try
    {
      .getListenerManager ().addJobListener (aJobListener, EverythingMatcher.allJobs ());
    }
    catch (final SchedulerException ex)
    {
      throw new IllegalStateException ("Failed to add job listener " + aJobListenerex);
    }
  }

  

Returns:
The underlying Quartz scheduler object. Never null.
  protected final Scheduler getScheduler ()
  {
    return ;
  }

  
Modify the JobData map in derived classes. This is e.g. used in pdaf3 to set the correct state data in executed jobs.

Parameters:
aJobDataMap The job data map to modify. Never null.
  protected void modifyJobDataMap (@Nonnull final JobDataMap aJobDataMap)
  {}

  
This method is only for testing purposes.

Parameters:
sJobName Name of the backup job. Needs to be passed in for testing purposes since no two job details with the same name may exist.
aTriggerBuilder The trigger builder instance to schedule the job
aJobClass Class to execute
aJobData Additional parameters. May be null.
  public final void scheduleJob (@Nonnull final String sJobName,
                                 @Nonnull final TriggerBuilder <? extends TriggeraTriggerBuilder,
                                 @Nonnull final Class <? extends JobaJobClass,
                                 @Nullable final Map <String, ? extends ObjectaJobData)
  {
    ValueEnforcer.notNull (sJobName"JobName");
    ValueEnforcer.notNull (aTriggerBuilder"TriggerBuilder");
    ValueEnforcer.notNull (aJobClass"JobClass");
    // what to do
    final JobDetail aJobDetail = JobBuilder.newJob (aJobClass).withIdentity (sJobName).build ();
    // add custom parameters
    final JobDataMap aJobDataMap = aJobDetail.getJobDataMap ();
    if (aJobData != null)
      for (final Map.Entry <String, ? extends ObjectaEntry : aJobData.entrySet ())
        aJobDataMap.put (aEntry.getKey (), aEntry.getValue ());
    // Add parameters via callback as well
    modifyJobDataMap (aJobDataMap);
    try
    {
      // Schedule now
      final Trigger aTrigger = aTriggerBuilder.build ();
      .scheduleJob (aJobDetailaTrigger);
      .info ("Succesfully scheduled job '" + sJobName + "' with trigger " + aTrigger);
    }
    catch (final SchedulerException ex)
    {
      throw LoggedRuntimeException.newException (ex);
    }
  }

  
Schedule a new job that should be executed now and only once.

Parameters:
sJobName Name of the job - must be unique within the whole system!
aJobClass The Job class to be executed.
aJobData Optional job data map.
  public void scheduleJobNowOnce (@Nonnull final String sJobName,
                                  @Nonnull final Class <? extends JobaJobClass,
                                  @Nullable final Map <String, ? extends ObjectaJobData)
  {
    scheduleJob (sJobName,
                 TriggerBuilder.newTrigger ()
                               .startNow ()
                               .withSchedule (SimpleScheduleBuilder.simpleSchedule ()
                                                                   .withIntervalInMinutes (1)
                                                                   .withRepeatCount (0)),
                 aJobClass,
                 aJobData);
  }

  
Shutdown the scheduler and wait for all jobs to complete.

Throws:
org.quartz.SchedulerException If something goes wrong
  public final void shutdown () throws SchedulerException
  {
    try
    {
      // Shutdown but wait for jobs to complete
      .shutdown (true);
      .info ("Successfully shutdown GlobalQuartzScheduler");
    }
    catch (final SchedulerException ex)
    {
      .error ("Failed to shutdown GlobalQuartzScheduler"ex);
      throw ex;
    }
  }
  protected final void onDestroy (@Nonnull final IScope aScopeInDestructionthrows Exception
  {
    shutdown ();
  }
New to GrepCode? Check out our FAQ X