com.cosylab.jcosyne.server
Class Executor

java.lang.Object
  extended byjavax.management.NotificationBroadcasterSupport
      extended bycom.cosylab.jcosyne.server.RemoteTask
          extended bycom.cosylab.jcosyne.server.Executor
All Implemented Interfaces:
java.util.EventListener, ExecutorMBean, javax.management.MBeanRegistration, javax.management.NotificationBroadcaster, javax.management.NotificationEmitter, javax.management.NotificationListener, RemoteTaskMBean, java.lang.Runnable

public class Executor
extends RemoteTask
implements ExecutorMBean, javax.management.NotificationListener

Executor is one of the predefined MBeans provided by JCosyne framework, which keeps track of other executing remote tasks. Executor launches new remote tasks in separate threads, suspends and resumes them and returns data about currently active tasks. Because the executor cannot forcefully stop or suspend or resume other tasks (since this is deprecated in Java Thread API), executor sets Suspended or Running flags on remote tasks. It is the duty of remote tasks to periodically check these flags (by, by default, calling their RemoteTask::checkState() method periodically) and respond. The executor will, however, listen to remote task notifications that infom it when the remote task took appropriate action.

Author:
Gasper Tkacik (gasper.tkacik@cosylab.com)

Field Summary
 
Fields inherited from class com.cosylab.jcosyne.server.RemoteTask
log, name, running, sequence, SERVER_DELEGATE, serverRef, suspended
 
Constructor Summary
Executor()
           
 
Method Summary
 javax.management.ObjectName[] getRunningRemoteTasks()
          Returns a list of all currently running remote tasks by name.
 long getSpeedMetrics()
          Returns the estimate of the processor speed on which the server is executing.
 void handleNotification(javax.management.Notification not, java.lang.Object handback)
          Listens to execution notifications from other remote tasks.
protected  void internalInitialize()
          Registers to the server as server listener, catches notifications whereby a remote task is removed from the server.
 boolean isReadyToRun()
          Always returns true.
 boolean isRunningRemoteTask(javax.management.ObjectName name)
          Returns true if the remote task passed as parameter is currently executing (in running state).
 void postDeregister()
          Overrides the parent postDeregister to do specific cleanup.
 void preDeregister()
          Deregisters as server listener and deregisters from all active tasks.
 void resumeRemoteTask(javax.management.ObjectName name, boolean block)
          Resumes the remote task if it has been suspended.
 void run()
          Performs some canonical computation and measures the attribute SpeedMetrics.
 boolean startRemoteTask(int localRepositoryID)
          Starts remote task that has its state stored in a local repository.
 boolean startRemoteTask(javax.management.ObjectName name)
          Starts a remote task that exists in this server.
 boolean stopRemoteTask(javax.management.ObjectName name)
          Stops the currently executing remote task.
 void suspendRemoteTask(javax.management.ObjectName name, boolean block)
          Suspends remote task if it is running.
 
Methods inherited from class com.cosylab.jcosyne.server.RemoteTask
checkState, copyStateFrom, copyStateFrom, executeScript, getInputResource, getLastScriptResult, getName, getOutputResource, getPostExecutionScript, getPostInitializactionScript, getProgress, getSequence, getState, initialize, isAborting, isFinished, isInitialized, isRunning, isSuspended, loadMatrix, loadMatrix, notifyAttributeChange, notifyExecutionStarts, notifyExecutionStops, postRegister, preRegister, saveMatrix, setFinished, setPostExecutionScript, setPostInitializationScript, setProgress, setRunning, setSuspended
 
Methods inherited from class javax.management.NotificationBroadcasterSupport
addNotificationListener, getNotificationInfo, handleNotification, removeNotificationListener, removeNotificationListener, sendNotification
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.cosylab.jcosyne.server.RemoteTaskMBean
copyStateFrom, copyStateFrom, equals, executeScript, getLastScriptResult, getName, getPostExecutionScript, getPostInitializactionScript, getProgress, getState, hashCode, initialize, isFinished, isInitialized, isRunning, isSuspended, setPostExecutionScript, setPostInitializationScript, setRunning, setSuspended
 

Constructor Detail

Executor

public Executor()
Method Detail

getSpeedMetrics

public long getSpeedMetrics()
Returns the estimate of the processor speed on which the server is executing. The estimate is simply the time in milliseconds to finish certain hardcoded calculation.

Specified by:
getSpeedMetrics in interface ExecutorMBean
Returns:
time in milliseconds for the execution of some canonical calculation

startRemoteTask

public boolean startRemoteTask(javax.management.ObjectName name)
Starts a remote task that exists in this server. The remote task can only be started if it does not run currently, if it returns true when its ReadyToRun attribute is evaluated. If the remote task is not yet initialized, its initialize() is called by this method. After that, a new thread is created for the remote task, the task is added to the active list, thread is started and this executor starts listening to execution events from the remote task. Running attribute of the remote task is set to true

Specified by:
startRemoteTask in interface ExecutorMBean
Parameters:
name - the name of the collocated remote task that will be started
Returns:
true iff the task was started succesfully

isRunningRemoteTask

public boolean isRunningRemoteTask(javax.management.ObjectName name)
Returns true if the remote task passed as parameter is currently executing (in running state).

Specified by:
isRunningRemoteTask in interface ExecutorMBean
Parameters:
name - the name of the remote task to check
Returns:
true iff the task is in the list of currently active tasks

stopRemoteTask

public boolean stopRemoteTask(javax.management.ObjectName name)
Stops the currently executing remote task. If the task is not running, returns NOP. Otherwise sets the Running attribute on the remote task to false and removes the task from the active list.

Specified by:
stopRemoteTask in interface ExecutorMBean
Parameters:
name - the name of the remote task to stop
Returns:
true if the task was stopped successfully

getRunningRemoteTasks

public javax.management.ObjectName[] getRunningRemoteTasks()
Returns a list of all currently running remote tasks by name.

Specified by:
getRunningRemoteTasks in interface ExecutorMBean
Returns:
a list of running tasks

run

public void run()
Performs some canonical computation and measures the attribute SpeedMetrics. Fires attribute change notification when done.

Specified by:
run in interface java.lang.Runnable

isReadyToRun

public boolean isReadyToRun()
Always returns true.

Specified by:
isReadyToRun in interface RemoteTaskMBean
Returns:
true

handleNotification

public void handleNotification(javax.management.Notification not,
                               java.lang.Object handback)
Listens to execution notifications from other remote tasks. Logs execution start events, removes itself as a listener on execution stop events, deblocks (potentially) blocking threads that wait in synchronous mode for suspend and resume requests to complete.

Specified by:
handleNotification in interface javax.management.NotificationListener
Parameters:
not - notification object of type ExecutionNotification
handback - not used

suspendRemoteTask

public void suspendRemoteTask(javax.management.ObjectName name,
                              boolean block)
Suspends remote task if it is running. Suspended task will stop processing and wait at a point in the run() method that it chooses, until it is resumed. While in suspended mode, it must be possible for any client to look up intermediate results without affecting the computation. Calling suspend on non-running or already suspended tasks has no effect.

Specified by:
suspendRemoteTask in interface ExecutorMBean
Parameters:
name - the name of the remote task to be suspended
block - if true, this method blocks until the remote task confirms that it has been suspended by firing execution event; if false, this method returns immediately, and after the return, the task may not have been suspended already

resumeRemoteTask

public void resumeRemoteTask(javax.management.ObjectName name,
                             boolean block)
Resumes the remote task if it has been suspended. Calling resume on a running non-suspended task has no effect.

Specified by:
resumeRemoteTask in interface ExecutorMBean
Parameters:
name - the name of remote task to suspend
block - if true this method blocks until the remote task notifies (through execution notifications) listeners that it has resumed processing; if false this method may return before the remote task has been resumed

internalInitialize

protected void internalInitialize()
Registers to the server as server listener, catches notifications whereby a remote task is removed from the server.

Specified by:
internalInitialize in class RemoteTask
See Also:
RemoteTask.initialize()

startRemoteTask

public boolean startRemoteTask(int localRepositoryID)
Starts remote task that has its state stored in a local repository. This method will contact the ResultRepository MBean, take its record with localRepositoryID index, and from it instantiate the remote task This is a sort of simple deserialization, done by invoking RemoteTask::copyStateFrom() on the newly instantiated task. Note that the task cannot be instantiated if the same task already exists in the server.

Specified by:
startRemoteTask in interface ExecutorMBean
Parameters:
localRepositoryID - the ID of the remote task invocation (index) entry in the local result repository
Returns:
true if the instantiation was succesfull

postDeregister

public void postDeregister()
Overrides the parent postDeregister to do specific cleanup. Call super post-deregistration method here!.

Specified by:
postDeregister in interface javax.management.MBeanRegistration
Overrides:
postDeregister in class RemoteTask

preDeregister

public void preDeregister()
                   throws java.lang.Exception
Deregisters as server listener and deregisters from all active tasks.

Specified by:
preDeregister in interface javax.management.MBeanRegistration
Overrides:
preDeregister in class RemoteTask
Throws:
java.lang.Exception