[jboss-cvs] jmx/src/main/javax/management/timer ...

Dimitris Andreadis dimitris.andreadis at jboss.com
Thu Jul 13 09:22:00 EDT 2006


  User: dimitris
  Date: 06/07/13 09:22:00

  Modified:    src/main/javax/management/timer  Tag: Branch_3_2 Timer.java
  Log:
  JBAS-3281, implement the fixed-rate API methods. Add support for fixed-delay execution, make it default (it was fixed-rate before)
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.10.4.3  +386 -362  jmx/src/main/javax/management/timer/Attic/Timer.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Timer.java
  ===================================================================
  RCS file: /cvsroot/jboss/jmx/src/main/javax/management/timer/Attic/Timer.java,v
  retrieving revision 1.10.4.2
  retrieving revision 1.10.4.3
  diff -u -b -r1.10.4.2 -r1.10.4.3
  --- Timer.java	1 Dec 2005 19:49:20 -0000	1.10.4.2
  +++ Timer.java	13 Jul 2006 13:22:00 -0000	1.10.4.3
  @@ -21,8 +21,6 @@
    */
   package javax.management.timer;
   
  -import java.io.Serializable;
  -
   import java.util.Date;
   import java.util.HashMap;
   import java.util.Iterator;
  @@ -34,6 +32,7 @@
   import javax.management.NotificationBroadcasterSupport;
   import javax.management.ObjectName;
   
  +import org.jboss.logging.Logger;
   import org.jboss.mx.util.RunnableScheduler;
   import org.jboss.mx.util.SchedulableRunnable;
   
  @@ -41,91 +40,63 @@
    * The timer service.
    *
    * @author <a href="mailto:Adrian.Brock at HappeningTimes.com">Adrian Brock</a>
  - * @version $Revision: 1.10.4.2 $
  + * @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
  + * @version $Revision: 1.10.4.3 $
    */
  -public class Timer
  -  extends NotificationBroadcasterSupport
  -  implements TimerMBean, MBeanRegistration, Serializable
  +public class Timer extends NotificationBroadcasterSupport
  +   implements TimerMBean, MBeanRegistration
   {
  +   // logging support
  +   private static Logger log = Logger.getLogger(Timer.class);
  +
     // Constants -----------------------------------------------------
   
  -  /**
  -   * The number of milliseconds in one second.
  -   */
  +   /** The number of milliseconds in one second. */
     public static final long ONE_SECOND = 1000;
   
  -  /**
  -   * The number of milliseconds in one minute.
  -   */
  +   /** The number of milliseconds in one minute. */
     public static final long ONE_MINUTE = ONE_SECOND * 60;
   
  -  /**
  -   * The number of milliseconds in one hour.
  -   */
  +   /** The number of milliseconds in one hour. */
     public static final long ONE_HOUR = ONE_MINUTE * 60;
   
  -  /**
  -   * The number of milliseconds in one day.
  -   */
  +   /** The number of milliseconds in one day. */
     public static final long ONE_DAY = ONE_HOUR * 24;
   
  -  /**
  -   * The number of milliseconds in one week.
  -   */
  +   /** The number of milliseconds in one week. */
     public static final long ONE_WEEK = ONE_DAY * 7;
   
  -  /**
  -   * Don't send notifications at initial start up.
  -   */
  +   /** Don't send notifications at initial start up. */
     private static final int SEND_NO = 0;
   
  -  /**
  -   * Send all past notifications at initial start up.
  -   */
  +   /** Send all past notifications at initial start up. */
     private static final int SEND_START = 1;
   
  -  /**
  -   * Normal operation sending
  -   */
  +   /** Normal operation sending */
     private static final int SEND_NORMAL = 2;
   
     // Attributes ----------------------------------------------------
   
  -  /**
  -   * The next notification id.
  -   */
  +   /** The next notification id. */
     int nextId = 0;
   
  -  /**
  -   * The next notification sequence number.
  -   */
  +   /** The next notification sequence number. */
     long sequenceNumber = 0;
   
  -  /**
  -   * The send past events attribute.
  -   */
  +   /** The send past events attribute. */
     boolean sendPastNotifications = false;
   
  -  /**
  -   * Whether the service is active.
  -   */
  +   /** Whether the service is active. */
     boolean active = false;
   
  -  /**
  -   * Our object name.
  -   */
  +   /** Our object name. */
     ObjectName objectName;
   
  -  /**
  -   * The registered notifications.
  -   */
  +   /** The registered notifications. */
     HashMap notifications = new HashMap();
   
  -  /**
  -   * The scheduler.
  -   * Not serializable.
  -   */
  -  private transient RunnableScheduler scheduler = new RunnableScheduler();
  +   /** The scheduler */
  +   private RunnableScheduler scheduler = new RunnableScheduler();
   
     // Static --------------------------------------------------------
   
  @@ -135,22 +106,46 @@
   
     // TimerMBean implementation -------------------------------------
   
  -  public Integer addNotification(String type, String message, Object userData,
  -                                 Date date)
  +   public Integer addNotification(String type, String message, Object userData, Date date)
       throws IllegalArgumentException
     {
       return addNotification(type, message, userData, date, 0);
     }
   
  -  public Integer addNotification(String type, String message, Object userData,
  -                                 Date date, long period)
  +   public Integer addNotification(String type, String message, Object userData, Date date, long period)
       throws IllegalArgumentException
     {
       return addNotification(type, message, userData, date, period, 0);
     }
   
  -  public Integer addNotification(String type, String message,
  -                Object userData, Date date, long period, long occurences)
  +   public Integer addNotification(String type, String message, Object userData, Date date, long period, long occurences)
  +      throws IllegalArgumentException
  +   {
  +      return addNotification(type, message, userData, date, period, occurences, false);
  +   }
  +
  +   /**
  +    * Creates a new timer notification with the specified type, message and userData and inserts it into the list of notifications with a given date, period and number of occurences.
  +    * <p/>
  +    * If the timer notification to be inserted has a date that is before the current date, the method behaves as if the specified date were the current date.
  +    * For once-off notifications, the notification is delivered immediately.
  +    * For periodic notifications, the first notification is delivered immediately and the subsequent ones are spaced as specified by the period parameter.
  +    * <p/>
  +    * Note that once the timer notification has been added into the list of notifications, its associated date, period and number of occurences cannot be updated.
  +    * <p/>
  +    * In the case of a periodic notification, the value of parameter fixedRate is used to specify the execution scheme, as specified in Timer.
  +    *
  +    * @param type         The timer notification type.
  +    * @param message      The timer notification detailed message.
  +    * @param userData     The timer notification user data object.
  +    * @param date         The date when the notification occurs.
  +    * @param period       The period of the timer notification (in milliseconds).
  +    * @param nbOccurences The total number the timer notification will be emitted.
  +    * @param fixedRate    If true and if the notification is periodic, the notification is scheduled with a fixed-rate execution scheme. If false and if the notification is periodic, the notification is scheduled with a fixed-delay execution scheme. Ignored if the notification is not periodic.
  +    * @return The identifier of the new created timer notification.
  +    * @throws IllegalArgumentException The period or the number of occurences is negative
  +    */
  +   public Integer addNotification(String type, String message, Object userData, Date date, long period, long nbOccurences, boolean fixedRate)
       throws IllegalArgumentException
     {
        // Generate the next id.
  @@ -160,8 +155,7 @@
   
       // Validate and create the registration.
       RegisteredNotification rn =
  -      new RegisteredNotification(id, type, message, userData, date, period,
  -                                 occurences);
  +         new RegisteredNotification(id, type, message, userData, date, period, nbOccurences, fixedRate);
   
       // Add the registration.
       synchronized(notifications)
  @@ -209,6 +203,23 @@
       return new Long(rn.occurences);
     }
   
  +   /**
  +    * Gets a copy of the flag indicating whether a peridic notification is executed at fixed-delay or at fixed-rate.
  +    *
  +    * @param id The timer notification identifier.
  +    * @return A copy of the flag indicating whether a peridic notification is executed at fixed-delay or at fixed-rate.
  +    */
  +   public Boolean getFixedRate(Integer id)
  +   {
  +      // Make sure there is a registration
  +      RegisteredNotification rn = (RegisteredNotification) notifications.get(id);
  +      if (rn == null)
  +         return null;
  +
  +      // Return a copy of the fixedRate
  +      return new Boolean(rn.fixedRate);
  +   }
  +
     public Vector getNotificationIDs(String type)
     {
       Vector result = new Vector();
  @@ -311,6 +322,9 @@
     public void removeNotification(Integer id)
       throws InstanceNotFoundException
     {
  +
  +     log.debug("removeNotification: " + objectName + ",id=" + id);
  +
       // Check if there is a notification.
       synchronized(notifications)
       {
  @@ -330,6 +344,8 @@
     {
       boolean found = false;
   
  +     log.debug("removeNotifications: " + objectName + ",type=" + type);
  +
       // Loop through the notifications removing the passed type.
       synchronized(notifications)
       {
  @@ -354,6 +370,7 @@
   
     public void setSendPastNotifications(boolean value)
     {
  +      log.debug("setSendPastNotifications: " + objectName + ",value=" + value);
       sendPastNotifications = value;
     }
   
  @@ -364,9 +381,11 @@
         return;
       active = true;
   
  +      log.debug("start: " + objectName + " at " + new Date());
  +
       // Perform the initial sends, for past notifications send missed events
       // otherwise ignore them
  -    synchronized(notifications)
  +      synchronized (notifications)
       {
          Iterator iterator = notifications.values().iterator();
          while (iterator.hasNext())
  @@ -391,6 +410,8 @@
       if (active == false)
         return;
   
  +     log.debug("stop: " + objectName + ",now=" + new Date());
  +
       // Stop the threads
       active = false;
       scheduler.stop();
  @@ -450,14 +471,17 @@
         if (rn.sendType != SEND_NO)
         {
           long seq = 0;
  -        synchronized(this)
  +           synchronized (this)
           {
              seq = ++sequenceNumber;
           }
  -        sendNotification(new TimerNotification(rn.type, objectName, seq,
  -                         rn.nextDate, rn.message, rn.id, rn.userData));
  -      }
   
  +           log.debug("sendNotification: " + rn);
  +           TimerNotification tn = new TimerNotification(rn.type, objectName,
  +              seq, rn.nextDate, rn.message, rn.id);
  +           tn.setUserData(rn.userData);
  +           sendNotification(tn);
  +        }
         // Calculate the next date.
         // Except for when we are sending past notifications at start up,
         // it cannot be in the future.
  @@ -466,13 +490,14 @@
           // If no next run, remove it sets the next date to zero.
           if (rn.calcNextDate() == false)
           {
  -          synchronized(notifications)
  +              synchronized (notifications)
             {
  +                 log.debug("remove: " + rn);
                notifications.remove(rn.id);
             }
           }
         }
  -      while (isActive() == true && rn.sendType != SEND_START && rn.nextDate != 0
  +        while (isActive() && rn.sendType != SEND_START && rn.nextDate != 0
                && rn.occurences == 0 && rn.nextDate < System.currentTimeMillis());
       }
   
  @@ -485,54 +510,38 @@
     /**
      * A registered notification. These run as separate threads.
      */
  -  private class RegisteredNotification
  -    extends SchedulableRunnable
  +   private class RegisteredNotification extends SchedulableRunnable
     {
       // Attributes ----------------------------------------------------
   
  -    /**
  -     * The notification id.
  -     */
  +      /** The notification id. */
       public Integer id;
   
  -    /**
  -     * The notification type.
  -     */
  +      /** The notification type. */
       public String type;
   
  -    /**
  -     * The message.
  -     */
  +      /** The message. */
       public String message;
   
  -    /**
  -     * The user data.
  -     */
  +      /** The user data. */
       public Object userData;
   
  -    /**
  -     * The start date.
  -     */
  +      /** The start date. */
       public long startDate;
   
  -    /**
  -     * The period.
  -     */
  +      /** The period. */
       public long period;
   
  -    /**
  -     * The maximum number of occurences.
  -     */
  +      /** The maximum number of occurences. */
       public long occurences;
   
  -    /**
  -     * The send type, no send, past notifications or normal
  -     */
  +      /** The flag to indicate fixedRate notifications, or fixedDelay (default) */
  +      public boolean fixedRate;
  +      
  +      /** The send type, no send, past notifications or normal */
       public int sendType = SEND_NORMAL;
   
  -    /**
  -     * The next run date
  -     */
  +      /** The next run date */
       public long nextDate = 0;
   
       // Constructors --------------------------------------------------
  @@ -544,19 +553,22 @@
        * @param type the notification type.
        * @param message the notification's message string.
        * @param userData the notification's user data.
  -     * @param date the date/time the notification will occur.
  +       * @param startDate the date/time the notification will occur.
        * @param period the repeat period in milli-seconds. Passing zero means
        *        no repeat.
        * @param occurences the maximum number of repeats. When the period is not
        *        zero and this parameter is zero, it will repeat indefinitely.
  -     * @return the notification id for this notification.
  +       * @param fixedRate If true and if the notification is periodic, the notification
  +       *        is scheduled with a fixed-rate execution scheme. If false and if the notification
  +       *        is periodic, the notification is scheduled with a fixed-delay execution scheme.
  +       *        Ignored if the notification is not periodic.
  +       *
        * @exception IllegalArgumentException when the date is before the current
        *        date, the period is negative or the number of repeats is
        *        negative.
        */
  -    public RegisteredNotification(Integer id, String type, String message,
  -                                  Object userData, Date startDate,
  -                                  long period, long occurences)
  +      public RegisteredNotification(Integer id, String type, String message, Object userData,
  +            Date startDate, long period, long occurences, boolean fixedRate)
         throws IllegalArgumentException
       {
         // Basic validation
  @@ -567,24 +579,26 @@
         if (occurences < 0)
           throw new IllegalArgumentException("Negative Occurences");
   
  +         this.startDate = startDate.getTime();
  +         if (startDate.getTime() < System.currentTimeMillis())
  +         {
  +            log.debug("startDate [" + startDate + "] in the past, set to now");
  +            this.startDate = System.currentTimeMillis();
  +         }
  +
         // Remember the values
         this.id = id;
         this.type = type;
         this.message = message;
         this.userData = userData;
  -      this.startDate = startDate.getTime();
         this.period = period;
         this.occurences = occurences;
  +         this.fixedRate = fixedRate;
   
  -      // Can we get a next date in the future
  -      nextDate = this.startDate;
  -      while (nextDate < System.currentTimeMillis())
  -      {
  -        // If we have no more occurences its an error
  -        if (calcNextDate() == false)
  -          throw new IllegalArgumentException("Requested notification(s) " +
  -                                             "in the past.");
  -      }
  +         this.nextDate = this.startDate;
  +
  +         String msgStr = "new " + this.toString();
  +         log.debug(msgStr);
       }
   
       // Public --------------------------------------------------------
  @@ -612,7 +626,10 @@
         }
   
         // Calculate the next occurence
  +         if (fixedRate)
         nextDate += period;
  +         else // fixed delay
  +            nextDate = System.currentTimeMillis() + period;
   
         return true;
       }
  @@ -627,5 +644,12 @@
           // Send any notifications
           sendNotifications(this);
       }
  +
  +      public String toString()
  +      {
  +         return " RegisteredNotification: [timer=" + objectName + ",id=" + id + ",startDate=" + new Date(startDate) +
  +                ",period=" + period + ",occurences=" + occurences + ",fixedRate=" + fixedRate +
  +                ",nextDate=" + new Date(nextDate) + "]";
  +      }
     }
   }
  
  
  



More information about the jboss-cvs-commits mailing list