User: schaefera
  Date: 01/08/14 10:24:10

  Modified:    src/main/org/jboss/util Scheduler.java
  Log:
  Added a patch from Cameron (camtabor) fixing some problems within the
  Scheduler.
  
  Revision  Changes    Path
  1.7       +109 -63   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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Scheduler.java    2001/08/03 17:15:57     1.6
  +++ Scheduler.java    2001/08/14 17:24:10     1.7
  @@ -1,5 +1,5 @@
   /*
  - * JBoss, the OpenSource J2EE webOS
  + * JBoss, the OpenSource EJB server
    *
    * Distributable under LGPL license.
    * See terms of license at gnu.org.
  @@ -20,6 +20,8 @@
   import javax.management.MalformedObjectNameException;
   import javax.management.MBeanServer;
   import javax.management.Notification;
  +import javax.management.timer.TimerNotification;
  +import javax.management.NotificationFilter;
   import javax.management.NotificationListener;
   import javax.management.ObjectName;
   import javax.naming.Context;
  @@ -35,11 +37,22 @@
   import org.jboss.util.ServiceMBeanSupport;
   
   /**
  - * Scheduler Instance to allow clients to run this as a
  - * scheduling service for any Schedulable instances.
  - *
  - * @author <a href="mailto:[EMAIL PROTECTED]";>Andreas Schaefer</a>
  - **/
  +* Scheduler Instance to allow clients to run this as a
  +* scheduling service for any Schedulable instances.
  +*
  +* @author <a href="mailto:[EMAIL PROTECTED]";>Andreas Schaefer</a>
  +* @author Cameron (camtabor)
  +*
  +* <p><b>Revisions:</b></p>
  +* <p><b>20010814 Cameron:</b>
  +* <ul>
  +* <li>Checks if the TimerMBean is already loaded</li>
  +* <li>Created a SchedulerNotificationFilter so that each Scheduler only
  +*     get its notifications</li>
  +* <li>Stop was broken because removeNotification( Integer ) was broken</li>
  +* </ul>
  +* </p>
  +**/
   public class Scheduler
      extends ServiceMBeanSupport
      implements SchedulerMBean
  @@ -47,23 +60,23 @@
   
      // -------------------------------------------------------------------------
      // Constants
  -   // -------------------------------------------------------------------------  
  +   // -------------------------------------------------------------------------
   
      public static String JNDI_NAME = "scheduler:domain";
      public static String JMX_NAME = "scheduler";
   
      // -------------------------------------------------------------------------
      // Members
  -   // -------------------------------------------------------------------------  
  +   // -------------------------------------------------------------------------
   
      private String mName;
  -   
  +
      private long mActualSchedulePeriod;
      private long mRemainingRepetitions = 0;
      private int mActualSchedule = -1;
      private ObjectName mTimer;
      private Schedulable mSchedulable;
  -   
  +
      private boolean mScheduleIsStarted = false;
      private boolean mWaitForNextCallToStop = false;
      private boolean mStartOnStart = false;
  @@ -81,7 +94,7 @@
   
      // -------------------------------------------------------------------------
      // Constructors
  -   // -------------------------------------------------------------------------  
  +   // -------------------------------------------------------------------------
   
      /**
       * Default (no-args) Constructor
  @@ -127,8 +140,8 @@
   
      // -------------------------------------------------------------------------
      // SchedulerMBean Methods
  -   // -------------------------------------------------------------------------  
  -   
  +   // -------------------------------------------------------------------------
  +
      public void startSchedule() {
         // Check if not already started
         if( !isStarted() ) {
  @@ -195,15 +208,7 @@
               catch( Exception e ) {
                  throw new InvalidParameterException( "Could not find the constructor 
or create the Schedulable Instance" );
               }
  -            // Register the notificaiton listener at the MBeanServer
  -            getServer().addNotificationListener(
  -               mTimer, 
  -               new Listener( mSchedulable ),
  -               // No filter
  -               null,
  -               // No object handback necessary
  -               null
  -            );
  +
               mRemainingRepetitions = mInitialRepetitions;
               mActualSchedulePeriod = mSchedulePeriod;
               // Register the Schedule at the Timer
  @@ -273,6 +278,14 @@
                     }
                  ) ).intValue();
               }
  +            // Register the notificaiton listener at the MBeanServer
  +            getServer().addNotificationListener(
  +               mTimer,
  +               new Listener( mSchedulable ),
  +               new SchedulerNotificationFilter(new Integer(mActualSchedule)),
  +               // No object handback necessary
  +               null
  +            );
               mScheduleIsStarted = true;
               mIsRestartPending = false;
            }
  @@ -281,7 +294,7 @@
            }
         }
      }
  -   
  +
      public void stopSchedule(
         boolean pDoItNow
      ) {
  @@ -296,7 +309,7 @@
                     new Integer( mActualSchedule )
                  },
                  new String[] {
  -                  Integer.TYPE.getName(),
  +                  "java.lang.Integer" ,
                  }
               );
               mActualSchedule = -1;
  @@ -310,19 +323,19 @@
            e.printStackTrace();
         }
      }
  -   
  +
      public void restartSchedule() {
         stopSchedule( true );
         startSchedule();
      }
  -   
  +
      public String getSchedulableClass() {
         if( mSchedulableClass == null ) {
            return null;
         }
         return mSchedulableClass.getName();
      }
  -   
  +
      public void setSchedulableClass( String pSchedulableClass )
         throws InvalidParameterException
      {
  @@ -354,11 +367,11 @@
         }
         mIsRestartPending = true;
      }
  -   
  +
      public String getSchedulableArguments() {
         return mSchedulableArguments;
      }
  -   
  +
      public void setSchedulableArguments( String pArgumentList ) {
         if( pArgumentList == null || pArgumentList.equals( "" ) ) {
            mSchedulableArgumentList = new String[ 0 ];
  @@ -380,11 +393,11 @@
         mSchedulableArguments = pArgumentList;
         mIsRestartPending = true;
      }
  -   
  +
      public String getSchedulableArgumentTypes() {
         return mSchedulableArgumentTypes;
      }
  -   
  +
      public void setSchedulableArgumentTypes( String pTypeList )
         throws InvalidParameterException
      {
  @@ -440,11 +453,11 @@
         mSchedulableArgumentTypes = pTypeList;
         mIsRestartPending = true;
      }
  -   
  +
      public long getSchedulePeriod() {
         return mSchedulePeriod;
      }
  -   
  +
      public void setSchedulePeriod( long pPeriod ) {
         if( pPeriod <= 0 ) {
            throw new InvalidParameterException( "Schedulable Period may be not less 
or equals than 0" );
  @@ -452,11 +465,11 @@
         mSchedulePeriod = pPeriod;
         mIsRestartPending = true;
      }
  -   
  +
      public long getInitialRepetitions() {
         return mInitialRepetitions;
      }
  -   
  +
      public void setInitialRepetitions( long pNumberOfCalls ) {
         if( pNumberOfCalls <= 0 ) {
            pNumberOfCalls = -1;
  @@ -468,7 +481,7 @@
      public long getRemainingRepetitions() {
         return mRemainingRepetitions;
      }
  -   
  +
      public boolean isStarted() {
         return mScheduleIsStarted;
      }
  @@ -479,7 +492,7 @@
   
      // -------------------------------------------------------------------------
      // Methods
  -   // -------------------------------------------------------------------------  
  +   // -------------------------------------------------------------------------
   
      public ObjectName getObjectName(
         MBeanServer pServer,
  @@ -489,7 +502,7 @@
      {
         return pName;
      }
  -   
  +
      public String getJNDIName() {
         if( mName != null ) {
            return JMX_NAME + ":" + mName;
  @@ -498,35 +511,38 @@
            return JMX_NAME;
         }
      }
  -   
  +
      public String getName() {
         return "JBoss Scheduler MBean";
      }
  -   
  +
      // -------------------------------------------------------------------------
      // ServiceMBean - Methods
  -   // -------------------------------------------------------------------------  
  +   // -------------------------------------------------------------------------
   
      protected void initService()
           throws Exception
      {
      }
  -   
  +
      protected void startService()
           throws Exception
      {
         bind( this );
         try {
  -         // Create Timer MBean
  -         mTimer = new ObjectName( "DefaultDomain", "service", "Timer" );
  -         getServer().createMBean( "javax.management.timer.Timer", mTimer );
  -         // Now start the Timer
  -         getServer().invoke(
  -            mTimer,
  -            "start",
  -            new Object[] {},
  -            new String[] {}
  -         );
  +         // Create Timer MBean if need be
  +
  +           mTimer = new ObjectName( "DefaultDomain", "service", "Timer");
  +           if ( !getServer().isRegistered(mTimer)){
  +             getServer().createMBean( "javax.management.timer.Timer", mTimer );
  +             // Now start the Timer
  +             getServer().invoke(
  +                mTimer,
  +                "start",
  +                new Object[] {},
  +                new String[] {}
  +             );
  +         }
         }
         catch( Exception e ) {
            e.printStackTrace();
  @@ -535,7 +551,7 @@
            startSchedule();
         }
      }
  -   
  +
      protected void stopService() {
         try {
            unbind();
  @@ -601,16 +617,16 @@
      public class Listener
         implements NotificationListener
      {
  -      
  +
         private Schedulable mDelegate;
  -      
  +
         public Listener( Schedulable pDelegate ) {
            mDelegate = pDelegate;
         }
  -      
  +
         public void handleNotification(
            Notification pNotification,
  -         Object pHandback 
  +         Object pHandback
         ) {
            System.out.println( "Listener.handleNotification(), notification: " + 
pNotification );
            try {
  @@ -639,7 +655,7 @@
                                 new Integer( mActualSchedule )
                              },
                              new String[] {
  -                              Integer.TYPE.getName(),
  +                              "java.lang.Integer",
                              }
                           );
                           // Add regular schedule
  @@ -674,7 +690,7 @@
                        new Integer( mActualSchedule )
                     },
                     new String[] {
  -                     Integer.TYPE.getName(),
  +                     "java.lang.Integer",
                     }
                  );
                  mActualSchedule = -1;
  @@ -685,17 +701,17 @@
            }
         }
      }
  -   
  +
      /**
       * A test class for a Schedulable Class
       **/
      public static class SchedulableExample
         implements Schedulable
      {
  -      
  +
         private String mName;
         private int mValue;
  -      
  +
         public SchedulableExample(
            String pName,
            int pValue
  @@ -703,7 +719,7 @@
            mName = pName;
            mValue = pValue;
         }
  -      
  +
         /**
          * Just log the call
          **/
  @@ -716,4 +732,34 @@
               ", test, name: " + mName + ", value: " + mValue );
         }
      }
  +}
  +
  +/**
  + * Filter to ensure that each Scheduler only gets notified when it is supposed to.
  + */
  +class SchedulerNotificationFilter implements javax.management.NotificationFilter{
  +
  +   private Integer mId;
  +
  +   /**
  +    * Create a Filter.
  +    * @param id the Scheduler id
  +    */
  +   public SchedulerNotificationFilter(Integer id){
  +     mId = id;
  +   }
  +
  +   /**
  +    * Determine if the notification should be sent to this Scheduler
  +    */
  +   public boolean isNotificationEnabled(Notification notification){
  +     if (notification instanceof javax.management.timer.TimerNotification){
  +       TimerNotification timerNotification = (TimerNotification) notification;
  +       if (timerNotification.getNotificationID().equals(mId)){
  +         return true;
  +       }
  +     }
  +     return false;
  +   }
  +
   }
  
  
  

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

Reply via email to