psdi.server

Class CronTaskManager

  • java.lang.Object
    • psdi.server.CronTaskManager
  • All Implemented Interfaces:
    FixedLoggerNames, FixedLoggers


    public class CronTaskManager
    extends java.lang.Object
    implements FixedLoggers
    CronService is a manager for CronTasks. It first loads the classes necessary to run the CronTasks and then periodically calls the cronAction() method of each CronTask that is registered with it. The interval between invocations is configured in the MXServer.properties file. For example:
    mxe.cronTask.psdi.test.CTTest=10s
    would cause the CronService to load the psdi.test.CTTest class and configure it to run every 10 seconds. Intervals are configured as an integer and a unit designator. Valid values for the unit designator are s,m,h,d which correspond to seconds, minutes, hours and days. The default unit is minutes. CronTasks can implement methods that serialize their current state, store it to the disk in their shutdown() method and then load it from the disk in their init() method. Please see the source code for the psdi.test.CTTest for an example of how to store() and load() serialized state to/from the disk.
    See Also:
    CronTask, Service, psdi.test.CCTest
    • Field Detail

      • cronThreads

        protected TenantLevelObj cronThreads
        Vector for list of CronThreads
      • tloStpExecutor

        protected java.util.concurrent.ScheduledExecutorService tloStpExecutor
    • Constructor Detail

      • CronTaskManager

        public CronTaskManager()
      • CronTaskManager

        public CronTaskManager(MXServer mxServer)
    • Method Detail

      • configFromProperties

        public void configFromProperties()
      • configure

        public void configure(java.util.Properties configData)
      • config

        public void config(java.util.Properties configData)
                    throws java.rmi.RemoteException,
                           MXException
        Configures the set of crontask instances to run. Each has its setInterval() called with the interval configured in crontaskinstance record. Only active crontask instances explicitly or implicitly included in mxe.crontask.dorun and not listed in mxe.crontask.donotrun of maximo.properties will be processed.
        Throws:
        MXException
        java.rmi.RemoteException
      • resumeCrontasks

        public void resumeCrontasks()
                             throws MXException
        This method should and can only be called the moment when admin mode is off. This method should have been called before the monitor thread pick up those crontasks which have been reloaded after the admin mode in on. Both methods are synchronized.
        Throws:
        MXException
      • isReadyForAdminMode

        public boolean isReadyForAdminMode()
                                    throws MXException
        Whether the crontask manager is ready for admin mode. If all the crontasks except BBCron is suspended, then admin mode can be turned on.
        Returns:
        Throws:
        MXException
      • activeCronThreadsCount

        public int activeCronThreadsCount()
      • getPeakThreadCount

        public int getPeakThreadCount()
      • resetPeakThreadCount

        public void resetPeakThreadCount()
      • activeCronThreads

        public java.util.Enumeration activeCronThreads()
      • loadCrontask

        public void loadCrontask(java.lang.String taskName,
                                 java.lang.String instanceName,
                                 java.lang.String className)
        Instantiate the crontask object, start the crontask thread and put them into the hash tables. The CrontaskInstance reference won't get set to the crontask object. It will be set only when the thread is started.
        Parameters:
        taskName -
        instanceName -
        className -
      • initialzeCronTaskInfo

        public boolean initialzeCronTaskInfo(java.lang.String taskName,
                                             java.lang.String instanceName)
        Initialize the crontask information, get the schedule information and caculate the first sleeping time till the next run. If the crontask has aready started before or it is run by another server, the next sleeping time will be based on the last run time. This method has to be called after the crontask thread is started which the crontask instance has been set to the crontask.
      • insertFirstRowIntoTask

        public void insertFirstRowIntoTask(java.lang.String task)
        Insert the first row into the taskscheduler table if row is not already present.
      • updateLastEnd

        public int updateLastEnd(CronTask cron,
                                 java.lang.String taskName,
                                 java.lang.String instanceName)
        Update the taskscheduler table with the information for the Server which started the task
      • updateTaskInfoByAnotherServer

        public int updateTaskInfoByAnotherServer(CronTask cron,
                                                 java.lang.String taskName,
                                                 java.lang.String instanceName)
        Update the taskscheduler table by another server(server which started the cron task no longer exists)
      • getLastTaskInfo

        public java.util.Date[] getLastTaskInfo(CronTask cron,
                                                java.lang.String taskName,
                                                java.lang.String instanceName)
        Get the information for the task for last time run. Depending on this we calculate next time run, makes sure only one server runs the task and fail over It returns an array of Date in the order of Date: server time stamp, Date: last end time, null if it is still running
      • getLastRunTime

        public java.util.Date getLastRunTime(CronTask cron,
                                             java.lang.String taskName,
                                             java.lang.String instanceName)
        Get the last run time for a task. This method is needed because some servers runs faster than the server which runs the task. So they need to wait for sometime
      • init

        public void init()
                  throws java.lang.Exception
        Starts a thread for each task currently configured. Note: the start() method of the CronThread, first calls the init, then goes into an infinite loop where it waits the specified interval and then invokes the CronTask's cronAction() method.
        Throws:
        java.lang.Exception
      • destroy

        public void destroy()
        Calls CronTaskManager.shutdown(). Why? Because all Services have a destroy() method which indicate that they should free any resources they're using. CronTasks have a shutdown(), which CronTaskManager aggregates into it's own shutdown().
        Throws:
        MXException
        java.rmi.RemoteException
      • shutdown

        public void shutdown()
                      throws MXException,
                             java.rmi.RemoteException
        Calls Thread.stop() on each thread associated with a CronTask. This ends the inifinite loop the thread was in, next it creates a new thread for each CronTask and uses it to call the CronTask's shutdown() method. This gives each CronTask has an independent thread to run its shutdown() on, and therefore no CronTask can prevent another CronTask from shutting down.
        Throws:
        MXException
        java.rmi.RemoteException
      • isShutdown

        public boolean isShutdown()
        Returns a logical AND from calling isShutdown() on each CronTask.
      • add

        public void add(CronTask newTask,
                        java.lang.String taskName,
                        java.lang.String instanceName)
                 throws MXSystemException
        Programmatically add a CronTask. If the task is already registered, an MXSystemException is thrown!
        Throws:
        MXSystemException
      • remove

        public void remove(CronTask task,
                           java.lang.String taskName,
                           java.lang.String instanceName)
        Programmatically remove a CronTask. Stop it's thread from running too. Note: the CronTasks's shutdown() is not called. The crontask object is not destroyed.
      • get

        public CronTask get(java.lang.String name,
                            java.lang.String instanceName)
        Get a CronTask by looking up its name.
      • isSleeping

        public boolean isSleeping(java.lang.String taskName,
                                  java.lang.String instanceName)
        Identify whether a ConThread is sleeping.
        Parameters:
        taskName - CronTask Name
        instanceName - Instance Name
        Returns:
        True if thread exists and is sleeping, False if thread exists and is not sleeping, True if thread does not exist.
      • restart

        public void restart()
                     throws java.rmi.RemoteException,
                            MXException
        Restarts this service. Calls this.config(mxServer.getConfig()) and then calls this.init().
        Throws:
        MXException
        java.rmi.RemoteException
      • handleAutoRemoval

        public void handleAutoRemoval(CronTask cronTask)
        If it is autoremoval, remove the crontask instance parameter as well to trigger any action that is associated with remove the crontask defined by the crontask itself. All the database related changes will be in one transaction. A error log will be generated if anything is wrong in the process and the transaction rolls back. The instance will be marked to inactive if not be able to be removed due to error.
        Parameters:
        cronTask -
      • getInstanceMbo

        public CrontaskInstanceRemote getInstanceMbo(java.lang.String taskName,
                                                     java.lang.String instanceName)
                                              throws MXException,
                                                     java.rmi.RemoteException
        Convenient method to get the standalong crontask instance mbo remote.
        Parameters:
        taskName -
        instanceName -
        Returns:
        Throws:
        MXException
        java.rmi.RemoteException
      • deactivate

        public void deactivate(java.lang.String taskName,
                               java.lang.String instName)
        Handles the request when the crontask is deactivated.
        Parameters:
        taskName -
        instName -
      • deactivate

        public void deactivate(java.lang.String key)
      • runTenantsChanged

        public void runTenantsChanged()
                               throws java.rmi.RemoteException,
                                      MXException
        Throws:
        java.rmi.RemoteException
        MXException
      • logSql

        public void logSql(java.lang.String sql,
                           java.sql.Connection con)
                    throws java.rmi.RemoteException,
                           MXException
        Throws:
        java.rmi.RemoteException
        MXException
      • setLastKPICronTaskRun

        public void setLastKPICronTaskRun()
      • getLastKPICronTaskRun

        public java.util.Date getLastKPICronTaskRun()