View Javadoc

    1   package org.apache.turbine.services.schedule;
    2
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *   http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21
    22  import java.util.ArrayList;
    23  import java.util.List;
    24
    25  import org.apache.commons.configuration.Configuration;
    26  import org.apache.commons.lang.StringUtils;
    27  import org.apache.turbine.util.TurbineException;
    28
    29  /**
    30   * Service for a cron like scheduler that uses the
    31   * TurbineResources.properties file instead of the database.
    32   * The methods that operate on jobs ( get,add,update,remove )
    33   * only operate on the queue in memory and changes are not reflected
    34   * to the properties file which was used to initialize the jobs.
    35   * An example is given below.  The job names are the class names that
    36   * extend ScheduledJob.
    37   *
    38   * <PRE>
    39   *
    40   * services.SchedulerService.scheduler.jobs=scheduledJobName,scheduledJobName2
    41   *
    42   * services.SchedulerService.scheduler.job.scheduledJobName.ID=1
    43   * services.SchedulerService.scheduler.job.scheduledJobName.SECOND=-1
    44   * services.SchedulerService.scheduler.job.scheduledJobName.MINUTE=-1
    45   * services.SchedulerService.scheduler.job.scheduledJobName.HOUR=7
    46   * services.SchedulerService.scheduler.job.scheduledJobName.WEEKDAY=-1
    47   * services.SchedulerService.scheduler.job.scheduledJobName.DAY_OF_MONTH=-1
    48   *
    49   * services.SchedulerService.scheduler.job.scheduledJobName2.ID=1
    50   * services.SchedulerService.scheduler.job.scheduledJobName2.SECOND=-1
    51   * services.SchedulerService.scheduler.job.scheduledJobName2.MINUTE=-1
    52   * services.SchedulerService.scheduler.job.scheduledJobName2.HOUR=7
    53   * services.SchedulerService.scheduler.job.scheduledJobName2.WEEKDAY=-1
    54   * services.SchedulerService.scheduler.job.scheduledJobName2.DAY_OF_MONTH=-1
    55   *
    56   * </PRE>
    57   *
    58   * Based on TamboraSchedulerService written by John Thorhauer.
    59   *
    60   * @author <a href="mailto:ekkerbj@netscpae.net">Jeff Brekke</a>
    61   * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
    62   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    63   * @version $Id: TurbineNonPersistentSchedulerService.java 534527 2007-05-02 16:10:59Z tv $
    64   *
    65   * @deprecated Use QuartzSchedulerService instead
    66   */
    67  @Deprecated
    68  public class TurbineNonPersistentSchedulerService extends AbstractSchedulerService
    69  {
    70      /**
    71       * @see org.apache.turbine.services.schedule.AbstractSchedulerService#loadJobs()
    72       */
    73      @Override
    74      protected List<? extends JobEntry> loadJobs() throws TurbineException
    75      {
    76          Configuration conf = getConfiguration();
    77          List<Object> jobProps = conf.getList("scheduler.jobs");
    78          List<JobEntry> jobs = new ArrayList<JobEntry>();
    79
    80          // If there are scheduler.jobs defined then set up a job vector
    81          // for the scheduleQueue
    82          if (!jobProps.isEmpty())
    83          {
    84              for (int i = 0; i < jobProps.size(); i++)
    85              {
    86                  String jobName = (String)jobProps.get(i);
    87                  String jobPrefix = "scheduler.job." + jobName;
    88
    89                  String jobId = conf.getString(jobPrefix + ".ID", null);
    90                  if (StringUtils.isEmpty(jobId))
    91                  {
    92                      throw new TurbineException(
    93                              "There is an error in the TurbineResources.properties file. \n"
    94                              + jobPrefix + ".ID is not found.\n");
    95                  }
    96
    97                  int sec = conf.getInt(jobPrefix + ".SECOND", -1);
    98                  int min = conf.getInt(jobPrefix + ".MINUTE", -1);
    99                  int hr = conf.getInt(jobPrefix + ".HOUR", -1);
    100                 int wkday = conf.getInt(jobPrefix + ".WEEKDAY", -1);
    101                 int dayOfMonth = conf.getInt(jobPrefix + ".DAY_OF_MONTH", -1);
    102
    103                 JobEntry je = newJob(
    104                         sec,
    105                         min,
    106                         hr,
    107                         wkday,
    108                         dayOfMonth,
    109                         jobName);
    110                 je.setJobId(Integer.parseInt(jobId));
    111                 jobs.add(je);
    112             }
    113         }
    114
    115         return jobs;
    116     }
    117
    118     /**
    119      * @see org.apache.turbine.services.schedule.ScheduleService#newJob(int, int, int, int, int, java.lang.String)
    120      */
    121     @Override
    122     public JobEntry newJob(int sec, int min, int hour, int wd, int day_mo, String task) throws TurbineException
    123     {
    124         return new JobEntryNonPersistent(sec, min, hour, wd, day_mo, task);
    125     }
    126
    127     /**
    128      * This method returns the job element from the internal queue.
    129      *
    130      * @param oid The int id for the job.
    131      * @return A JobEntry.
    132      * @throws TurbineException could not retrieve job
    133      */
    134     @Override
    135     public JobEntry getJob(int oid)
    136             throws TurbineException
    137     {
    138         JobEntry je = new JobEntryNonPersistent();
    139         je.setJobId(oid);
    140         return scheduleQueue.getJob(je);
    141     }
    142
    143     /**
    144      * Remove a job from the queue.
    145      *
    146      * @param je A JobEntry with the job to remove.
    147      */
    148     @Override
    149     public void removeJob(JobEntry je)
    150     {
    151         // Remove from the queue.
    152         scheduleQueue.remove(je);
    153         restart();
    154     }
    155
    156     /**
    157      * Add/update a job
    158      *
    159      * @param je A JobEntry with the job to modify
    160      * @throws TurbineException job could not be updated
    161      */
    162     @Override
    163     public void updateJob(JobEntry je)
    164             throws TurbineException
    165     {
    166         try
    167         {
    168             je.calcRunTime();
    169
    170             // Update the queue.
    171             scheduleQueue.modify(je);
    172             restart();
    173         }
    174         catch (Exception e)
    175         {
    176             throw new TurbineException("Problem updating Scheduled Job: " + je.getTask(), e);
    177         }
    178     }
    179 }