User: schaefera
  Date: 02/01/17 13:57:27

  Modified:    src/main/org/jboss/util Scheduler.java SchedulerMBean.java
  Log:
  Ajusted JSR-77 to the new JBoss JMX naming and the Scheduler now can be restarted 
and will keep the schedule and allows an easier date input for Initial Start Date
  
  Revision  Changes    Path
  1.15      +122 -134  jboss/src/main/org/jboss/util/Scheduler.java
  
  Index: Scheduler.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/util/Scheduler.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Scheduler.java    2002/01/09 21:49:11     1.14
  +++ Scheduler.java    2002/01/17 21:57:27     1.15
  @@ -7,6 +7,8 @@
   package org.jboss.util;
   
   import java.lang.reflect.Constructor;
  +import java.text.DateFormat;
  +import java.text.SimpleDateFormat;
   import java.security.InvalidParameterException;
   import java.util.ArrayList;
   import java.util.Collection;
  @@ -31,7 +33,7 @@
   import javax.naming.NameNotFoundException;
   import javax.naming.Reference;
   import javax.naming.StringRefAddr;
  -
  +import org.jboss.logging.Logger;
   import org.jboss.naming.NonSerializableFactory;
   import org.jboss.system.ServiceMBeanSupport;
   
  @@ -64,6 +66,20 @@
   * <li>MBean is not bind/unbind to JNDI server anymore</li>
   * </ul>
   * </p>
  +* <p><b>20020117 Andy:</b>
  +* <ul>
  +* <li>Change the behaviour when the Start Date is in the past. Now the
  +*     Scheduler will behave as the Schedule is never stopped and find
  +*     the next available time to start with respect to the settings.
  +*     Therefore you can restart JBoss without adjust your Schedule
  +*     every time. BUT you will still loose the calls during the Schedule
  +*     was donw.</li>
  +* <li>Added parsing capabilities to setInitialStartDate. Now NOW: current time,
  +*     and a string in a format the SimpleDataFormat understand in your environment
  +*     (US: m/d/yy h:m a) but of course the time in ms since 1/1/1970.</li>
  +* <li>Some fixes like the stopping a Schedule even if it already stopped etc.</li>
  +* </ul>
  +* </p>
   **/
   public class Scheduler
      extends ServiceMBeanSupport
  @@ -81,7 +97,8 @@
      // Members
      // -------------------------------------------------------------------------
   
  -   private String mName;
  +   /** Class logger. */
  +   private static final Logger log = Logger.getLogger( Scheduler.class );
   
      private long mActualSchedulePeriod;
      private long mRemainingRepetitions = 0;
  @@ -100,47 +117,36 @@
      private String[] mSchedulableArgumentList = new String[ 0 ];
      private String mSchedulableArgumentTypes;
      private Class[] mSchedulableArgumentTypeList = new Class[ 0 ];
  +   private DateFormat mDateFormatter;
      private Date mStartDate;
  +   private String mStartDateString;
      private long mSchedulePeriod;
      private long mInitialRepetitions;
   
      // -------------------------------------------------------------------------
      // Constructors
      // -------------------------------------------------------------------------
  -
  +   
      /**
       * Default (no-args) Constructor
       **/
      public Scheduler()
      {
  -      mName = null;
  -   }
  -
  -   /**
  -    * Constructor with the necessary attributes to be set
  -    *
  -    * @param pName Name of the MBean
  -    **/
  -   public Scheduler( String pName )
  -   {
  -      mName = pName;
      }
  -
  +   
      /**
       * Constructor with the necessary attributes to be set
       *
       * @param pName Name of the MBean
       **/
      public Scheduler(
  -      String pName,
         String pSchedulableClass,
         String pInitArguments,
         String pInitTypes,
  -      long pInitialStartDate,
  +      String pInitialStartDate,
         long pSchedulePeriod,
         long pNumberOfRepetitions
      ) {
  -      mName = pName;
         setStartAtStartup( true );
         setSchedulableClass( pSchedulableClass );
         setSchedulableArguments( pInitArguments );
  @@ -160,16 +166,19 @@
            try {
               // Check the given attributes if correct
               if( mSchedulableClass == null ) {
  +               log.debug( "Schedulable Class is not set" );
                  throw new InvalidParameterException(
                     "Schedulable Class must be set"
                  );
               }
               if( mSchedulableArgumentList.length != 
mSchedulableArgumentTypeList.length ) {
  +               log.debug( "Schedulable Class Arguments and Types do not match in 
length" );
                  throw new InvalidParameterException(
                     "Schedulable Class Arguments and Types do not match in length"
                  );
               }
               if( mSchedulePeriod <= 0 ) {
  +               log.debug( "Schedule Period is less than 0 (ms)" );
                  throw new InvalidParameterException(
                     "Schedule Period must be set and greater than 0 (ms)"
                  );
  @@ -209,6 +218,7 @@
                  }
               }
               catch( Exception e ) {
  +               log.error( "Could not load or create constructor argument", e );
                  throw new InvalidParameterException( "Could not load or create a 
constructor argument" );
               }
               try {
  @@ -218,81 +228,58 @@
                  mSchedulable = (Schedulable) lSchedulableConstructor.newInstance( 
lArgumentList );
               }
               catch( Exception e ) {
  +               log.error( "Could not find the constructor or create Schedulable 
instance", e );
                  throw new InvalidParameterException( "Could not find the constructor 
or create the Schedulable Instance" );
               }
   
               mRemainingRepetitions = mInitialRepetitions;
               mActualSchedulePeriod = mSchedulePeriod;
               // Register the Schedule at the Timer
  -            if( mStartDate == null || mStartDate.getTime() < new Date().getTime() ) 
{
  -               mStartDate = new Date( new Date().getTime() + 1000 );
  -               // Start Schedule now
  -               System.out.println( "Start regular Schedule with period: " + 
mActualSchedulePeriod );
  +            // Check if initial start date is in the past
  +            if( mStartDate.getTime() < new Date().getTime() ) {
  +               // If then first check if a repetition is in the future
  +               long lNow = new Date().getTime() + 100;
  +               long lSkipRepeats = ( ( lNow - mStartDate.getTime() ) / 
mActualSchedulePeriod ) + 1;
                  if( mRemainingRepetitions > 0 ) {
  -                  System.out.println( "Start Schedule wtih " + 
mRemainingRepetitions + " reps." );
  -                  mActualSchedule = ( (Integer) getServer().invoke(
  -                     mTimer,
  -                     "addNotification",
  -                     new Object[] {
  -                        "Schedule",
  -                        "Scheduler Notification",
  -                        null,    // User Object
  -                        new Date( new Date().getTime() + 1000 ),
  -                        new Long( mActualSchedulePeriod ),
  -                        new Long( mRemainingRepetitions )
  -                     },
  -                     new String[] {
  -                        String.class.getName(),
  -                        String.class.getName(),
  -                        Object.class.getName(),
  -                        Date.class.getName(),
  -                        Long.TYPE.getName(),
  -                        Long.TYPE.getName()
  -                     }
  -                  ) ).intValue();
  -               }
  -               else {
  -                  System.out.println( "Start Schedule with unlimited reps." );
  -                  mActualSchedule = ( (Integer) getServer().invoke(
  -                     mTimer,
  -                     "addNotification",
  -                     new Object[] {
  -                        "Schedule",
  -                        "Scheduler Notification",
  -                        null,    // User Object
  -                        new Date( new Date().getTime() + 1000 ),
  -                        new Long( mActualSchedulePeriod )
  -                     },
  -                     new String[] {
  -                        String.class.getName(),
  -                        String.class.getName(),
  -                        Object.class.getName(),
  -                        Date.class.getName(),
  -                        Long.TYPE.getName()
  -                     }
  -                  ) ).intValue();
  -               }
  -            }
  -            else {
  -               // Add an initial call
  -               mActualSchedule = ( (Integer) getServer().invoke(
  -                  mTimer,
  -                  "addNotification",
  -                  new Object[] {
  -                     "Schedule",
  -                     "Scheduler Notification",
  -                     null,       // User Object
  -                     mStartDate
  -                  },
  -                  new String[] {
  -                     String.class.getName(),
  -                     String.class.getName(),
  -                     Object.class.getName(),
  -                     Date.class.getName(),
  +                  // If not infinit loop
  +                  if( lSkipRepeats >= mRemainingRepetitions ) {
  +                     // No repetition left -> exit
  +                     log.info( "No repetitions left because start date is in the 
past and could " +
  +                        "not be reached by Initial Repetitions * Schedule Period" );
  +                     return;
  +                  } else {
  +                     // Reduce the missed hits
  +                     mRemainingRepetitions -= lSkipRepeats;
                     }
  -               ) ).intValue();
  +               }
  +               mStartDate = new Date( mStartDate.getTime() + ( lSkipRepeats * 
mActualSchedulePeriod ) );
  +               log.debug( "New Start Date is: " + mStartDate );
               }
  -            // Register the notificaiton listener at the MBeanServer
  +            log.debug( "Schedule initial call to: " + mStartDate + ", remaining 
repetitions: " + mRemainingRepetitions );
  +            // Add an initial call
  +            mActualSchedule = ( (Integer) getServer().invoke(
  +               mTimer,
  +               "addNotification",
  +               new Object[] {
  +                  "Schedule",
  +                  "Scheduler Notification",
  +                  null,       // User Object
  +                  mStartDate,
  +                  new Long( mActualSchedulePeriod ),
  +                  mRemainingRepetitions < 0 ?
  +                     new Long( 0 ) :
  +                     new Long( mRemainingRepetitions )
  +               },
  +               new String[] {
  +                  String.class.getName(),
  +                  String.class.getName(),
  +                  Object.class.getName(),
  +                  Date.class.getName(),
  +                  Long.TYPE.getName(),
  +                  Long.TYPE.getName()
  +               }
  +            ) ).intValue();
  +            // Register the notification listener at the MBeanServer
               getServer().addNotificationListener(
                  mTimer,
                  new Listener( mSchedulable ),
  @@ -313,9 +300,15 @@
         boolean pDoItNow
      ) {
         try {
  +         if( mActualSchedule < 0 ) {
  +            mScheduleIsStarted = false;
  +            mWaitForNextCallToStop = false;
  +            return;
  +         }
            if( pDoItNow ) {
               // Remove notification listener now
               mWaitForNextCallToStop = false;
  +            log.debug( "stopSchedule(), schedule id: " + mActualSchedule );
               getServer().invoke(
                  mTimer,
                  "removeNotification",
  @@ -326,6 +319,7 @@
                     "java.lang.Integer" ,
                  }
               );
  +            log.debug( "stopSchedule(), removed schedule id: " + mActualSchedule );
               mActualSchedule = -1;
               mScheduleIsStarted = false;
            }
  @@ -480,12 +474,36 @@
         mIsRestartPending = true;
      }
      
  -   public long getInitialStartDate() {
  -      return mStartDate.getTime();
  +   public String getInitialStartDate() {
  +      return mStartDateString;
      }
      
  -   public void setInitialStartDate( long pStartDate ) {
  -      mStartDate = new Date( ( pStartDate > 0 ? pStartDate : 0 ) );
  +   public void setInitialStartDate( String pStartDate ) {
  +      mStartDateString = pStartDate == null ? "" : pStartDate.trim();
  +      if( mStartDateString.equals( "" ) ) {
  +         mStartDate = new Date( 0 );
  +      } else
  +      if( mStartDateString.equals( "NOW" ) ) {
  +         mStartDate = new Date( new Date().getTime() + 1000 );
  +      } else {
  +         try {
  +            long lDate = new Long( pStartDate ).longValue();
  +            mStartDate = new Date( lDate );
  +         }
  +         catch( Exception e ) {
  +            try {
  +               if( mDateFormatter == null ) {
  +                  mDateFormatter = new SimpleDateFormat();
  +               }
  +               mStartDate = mDateFormatter.parse( mStartDateString );
  +            }
  +            catch( Exception e2 ) {
  +               log.error( "Could not parse given date string: " + mStartDateString, 
e2 );
  +               throw new InvalidParameterException( "Schedulable Date is not of 
correct format" );
  +            }
  +         }
  +      }
  +      log.debug( "Initial Start Date is set to: " + mStartDate );
      }
   
      public long getInitialRepetitions() {
  @@ -556,10 +574,11 @@
            }
         }
         catch( Exception e ) {
  -         e.printStackTrace();
  +         log.error( "Could not start Timer Service", e );
         }
   
         if( mStartOnStart ) {
  +         log.debug( "Start Scheduler on start up time" );
            startSchedule();
         }
      }
  @@ -587,59 +606,25 @@
            Notification pNotification,
            Object pHandback
         ) {
  -         System.out.println( "Listener.handleNotification(), notification: " + 
pNotification );
  +         log.debug( "Listener.handleNotification(), notification: " + pNotification 
);
            try {
               // If schedule is started invoke the schedule method on the Schedulable 
instance
  +            log.debug( "Scheduler is started: " + isStarted() );
  +            Date lTimeStamp = new Date( pNotification.getTimeStamp() );
               if( isStarted() ) {
                  if( getRemainingRepetitions() > 0 || getRemainingRepetitions() < 0 ) 
{
  -                  mDelegate.perform(
  -                     new Date(),
  -                     getRemainingRepetitions()
  -                  );
                     if( mRemainingRepetitions > 0 ) {
                        mRemainingRepetitions--;
                     }
  +                  mDelegate.perform(
  +                     lTimeStamp,
  +                     getRemainingRepetitions()
  +                  );
  +                  log.debug( "Remaining Repititions: " + getRemainingRepetitions() +
  +                     ", wait for next call to stop: " + mWaitForNextCallToStop );
                     if( getRemainingRepetitions() == 0 || mWaitForNextCallToStop ) {
                        stopSchedule( true );
                     }
  -                  else {
  -                     if( "InitialCall".equals( pNotification.getType() ) ) {
  -                        // When Initial Call then setup the regular schedule
  -                        // By first removing the initial one and then adding the
  -                        // regular one.
  -                        getServer().invoke(
  -                           mTimer,
  -                           "removeNotification",
  -                           new Object[] {
  -                              new Integer( mActualSchedule )
  -                           },
  -                           new String[] {
  -                              "java.lang.Integer",
  -                           }
  -                        );
  -                        // Add regular schedule
  -                        mActualSchedule = ( (Integer) getServer().invoke(
  -                           mTimer,
  -                           "addNotification",
  -                           new Object[] {
  -                              "Schedule",
  -                              "Scheduler Notification",
  -                              null,    // User Object
  -                              new Date( new Date().getTime() + 1000 ),
  -                              new Long( mActualSchedulePeriod ),
  -                              new Long( getRemainingRepetitions() )
  -                           },
  -                           new String[] {
  -                              String.class.getName(),
  -                              String.class.getName(),
  -                              Object.class.getName(),
  -                              Date.class.getName(),
  -                              Long.TYPE.getName(),
  -                              Long.TYPE.getName()
  -                           }
  -                        ) ).intValue();
  -                     }
  -                  }
                  }
               }
               else {
  @@ -697,7 +682,10 @@
         implements Schedulable
      {
   
  -      private String mName;
  +      /** Class logger. */
  +      private static final Logger log = Logger.getLogger( 
Scheduler.SchedulableExample.class );
  +      
  +     private String mName;
         private int mValue;
   
         public SchedulableExample(
  @@ -715,7 +703,7 @@
            Date pTimeOfCall,
            long pRemainingRepetitions
         ) {
  -         System.out.println( "Schedulable Examples is called at: " + pTimeOfCall +
  +         log.info( "Schedulable Examples is called at: " + pTimeOfCall +
               ", remaining repetitions: " + pRemainingRepetitions +
               ", test, name: " + mName + ", value: " + mValue );
         }
  
  
  
  1.8       +25 -10    jboss/src/main/org/jboss/util/SchedulerMBean.java
  
  Index: SchedulerMBean.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/util/SchedulerMBean.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SchedulerMBean.java       2002/01/03 04:01:01     1.7
  +++ SchedulerMBean.java       2002/01/17 21:57:27     1.8
  @@ -135,21 +135,36 @@
      public void setSchedulePeriod( long pPeriod );
      
      /**
  -   * @return Date (and time) of the first scheduled call. Value is in milliseconds 
since 1/1/1970.
  -   *         If date is in the past when the schedule is added to the Timer then 
the schedule will
  -   *         started immediately
  +   * @return Date (and time) of the first scheduled. For value see {@link 
#setInitialStartDate
  +   *         setInitialStartDate()} method.
      **/
  -   public long getInitialStartDate();
  +   public String getInitialStartDate();
      
      /**
  -   * Sets the first scheduled call. If in the past when the scheduler gets setup it 
will start
  -   * immediately.
  +   * Sets the first scheduled call. If the date is in the past the scheduler tries 
to find the
  +   * next available start date.
      *
  -   * @param pStartDate Date in milliseconds since 1/1/1970 for the first initial 
call. Anything
  -   *                   less than zero means 1/1/1970. Any date less than now means 
the schedule
  -   *                   will be started immediately.
  +   * @param pStartDate Date when the initial call is scheduled. It can be either:
  +   *                   <ul>
  +   *                      <li>
  +   *                         NOW: date will be the current date (new Date()) plus 1 
seconds
  +   *                      </li><li>
  +   *                         Date as String able to be parsed by SimpleDateFormat 
with default format
  +   *                      </li><li>
  +   *                         Milliseconds since 1/1/1970
  +   *                      </li>
  +   *                   </ul>
  +   *                   If the date is in the past the Scheduler
  +   *                   will search a start date in the future with respect to the 
initial repe-
  +   *                   titions and the period between calls. This means that when 
you restart
  +   *                   the MBean (restarting JBoss etc.) it will start at the next 
scheduled
  +   *                   time. When no start date is available in the future the 
Scheduler will
  +   *                   not start.<br>
  +   *                   Example: if you start your Schedulable everyday at Noon and 
you restart
  +   *                   your JBoss server then it will start at the next Noon (the 
same if started
  +   *                   before Noon or the next day if start after Noon).
      **/
  -   public void setInitialStartDate( long pStartDate );
  +   public void setInitialStartDate( String pStartDate );
      
      /**
       * @return Number of scheduled calls initially. If -1 then there is not limit.
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to