|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.management.NotificationBroadcasterSupport com.cosylab.jcosyne.server.RemoteTask
Remote Task is the base convenience class from which all remote tasks inherit.
It canonically implements the RemoteTaskMBean
. See documentation in
that interface for design contract behavior. In addition, this implementation contains
many convenience methods that allow quick development of new remote tasks. Features include:
log
. This is Java 1.4
logging. If you submit a log, it will be displayed on the console, but will, as a side effect,
also cause the log notification to be emitted (and caught by log collector, which will
record and forward it).
getSequence()
.
getName()
returns the name of
this remote task in ObjectName
format, where "MBeans:type=" + getClass().getName()
is the design prescription to form the name; serverRef
contains the reference
to the JMX server running this task.
checkState()
from the run()
method periodically to
check and respond to changes in running and suspended states. The method will block until
resumed, or in case of forceful stop, will throw an exception that will break loops in
run()
.
notifyAttributeChange
to schedule such a notification.
notifyExecutionStarts()
and notifyExecutionStops()
. The latter also invokes post-execution script and
finally moves remote task into the Finished
state.
getState()
will scan introspectively throgh all attributes of
this
and assemble attribute lists. Note that the framework will take snapshots
at execution start and execution stop checkpoints automatically, when corresponding
execution notifications are created by notifyExecution...()
methods. Note that
by default getState(true)
will throw UnsupportedOperationException
,
signifying that this class does not know how to capture arbitrary internal data. Subclasses
should override and provide for this functionality. getState(false)
, however,
works by capturing all RW attributes.
getInputResource()
and getOutputResource()
. This methods, on your behalf, contact the result
repository, obtain appropriate filenames and return initialized streams that can be used
immediately. By using these methods, the result repository automatically assmebles the list
of all opened input and output files and will store it in remote task invocation entry.
final void initialize()
.
This method starts the notification dispatcher, calls abstract internalInitialize()
which you implement in your subclass, invokes post-initialization script and affects the state
change.
notifyAttributeChange()
. The
method creates and dispatches events.
checkState()
should be called periodically by subclasses from
their run()
loops. This method will check if the computation needs to be
interrupted (and will throw InterruptedException
if so, which you catch and
handle by gracefully ending the computation); or suspended, in which case the method will
block on your behalf until the process is resumed again through the executor. Call
checkState()
from the point in your run()
loop where
intermediate results make best sense and will not be affected by accessing them through
accessors.
Progress
attribute is a predefined attribute for tracking the progress of
the calculation, and it expresses the completion in the range from 0 to 100. Use
setProgress()
to change the value and dispatch notifications. These can be used
by GUI to track progress of the calculation.
RemoteTaskScript
interface and must be written in BeanScript syntax. Custom
script suspends execution immediately prior to the execution and resumes afterwards. It
stores the result in LastScriptResult
as per design contract.
saveMatrix()
and loadMatrix()
.
RemoteTaskMBean
Field Summary | |
protected java.util.logging.Logger |
log
Logger that remote task can use to submit logs. |
protected javax.management.ObjectName |
name
Contains the name of this remote task, calculated accoring to the prescription of RemoteTaskMBean . |
protected boolean |
running
Running flag. |
protected long |
sequence
Automatic sequence counting for notifications. |
static javax.management.ObjectName |
SERVER_DELEGATE
The name prescribed by JMX specification that denotes the JMX server. |
protected javax.management.MBeanServer |
serverRef
Contains a reference to the collocated JMX server that is hosting this remote task. |
protected boolean |
suspended
Suspended flag. |
Constructor Summary | |
RemoteTask()
Construct a new instance of remote task. |
Method Summary | |
protected void |
checkState()
Checks the running and suspended flags. |
boolean |
copyStateFrom(javax.management.AttributeList state,
javax.management.AttributeList internal)
Throws exception. |
boolean |
copyStateFrom(RemoteTaskInvocation rti)
Throws exception. |
java.lang.Object |
executeScript(java.lang.String script)
Executes a script immediatelly. |
java.io.InputStream |
getInputResource(java.lang.String resourceName)
Get input stream for a named input resource. |
java.lang.Object |
getLastScriptResult()
Return the last script result executed with executeScript() . |
javax.management.ObjectName |
getName()
Returns the name under which this remote task is registered in the server. |
java.io.OutputStream |
getOutputResource(java.lang.String subspec)
Get output stream for an output resource. |
java.lang.String |
getPostExecutionScript()
Returns the post execution script. |
java.lang.String |
getPostInitializactionScript()
Returns the post-initialization script. |
double |
getProgress()
Returns the progress variable. |
protected long |
getSequence()
Returns a new sequence number for the notification, internally increases sequence count. |
javax.management.AttributeList |
getState(boolean internal)
Captures the state of this remote task. |
void |
initialize()
A cannonical implementation of initialize. |
protected abstract void |
internalInitialize()
Override this method to provide specific initialization for your remote algorithm. |
boolean |
isAborting()
Returns true iff the task is currently aborting, that is, is being
forcefully terminated. |
boolean |
isFinished()
Returns the value of the Finished attribute. |
boolean |
isInitialized()
Returns the value of the Initialized attribute. |
boolean |
isRunning()
Returns the value of the Running attribute. |
boolean |
isSuspended()
Returns the value of the Suspended attribute. |
protected double[][] |
loadMatrix(java.io.InputStream is)
A convenience method for loading the matrix from an input stream. |
protected double[][] |
loadMatrix(java.lang.String input)
A convenience method for loading matrix from a input resource. |
protected void |
notifyAttributeChange(java.lang.String name,
java.lang.Object oldValue,
java.lang.Object newValue)
Call this method to inform listeners about the change in the attribute value. |
protected void |
notifyExecutionStarts()
Sends execution start notification to all listeners. |
protected void |
notifyExecutionStops(java.lang.String message,
int success)
Sends stop execution notification to all listeners. |
void |
postDeregister()
Releases resources. |
void |
postRegister(java.lang.Boolean arg0)
NOP. |
void |
preDeregister()
Releases resources and stops the notification dispatcher thread. |
javax.management.ObjectName |
preRegister(javax.management.MBeanServer arg0,
javax.management.ObjectName arg1)
Initializes the server reference and this object name. |
protected void |
saveMatrix(java.lang.String sub,
double[][] data)
A convenience method for saving matrix into the output file. |
protected void |
setFinished(boolean value)
Call this method at the end of computation to switch to finished state. |
void |
setPostExecutionScript(java.lang.String postExecutionScript)
Sets the post execution script. |
void |
setPostInitializationScript(java.lang.String postInitializationScript)
Sets a script that implements RemoteTaskScript interface. |
protected void |
setProgress(double progress)
Call this method from run() to update the variable and send
notifications. |
void |
setRunning(boolean running)
Invoked by the Executor . |
void |
setSuspended(boolean suspended)
This method can only be called by the Executor . |
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 |
equals, hashCode, isReadyToRun |
Methods inherited from interface java.lang.Runnable |
run |
Field Detail |
protected java.util.logging.Logger log
protected long sequence
getSequence()
to get a unique sequence number, do not access this field directly.
protected javax.management.ObjectName name
RemoteTaskMBean
.
protected javax.management.MBeanServer serverRef
protected boolean running
checkState()
periodically
from run()
to detect running state change.
protected boolean suspended
checkState()
periodically
from run()
to detect suspended state change.
public static javax.management.ObjectName SERVER_DELEGATE
Constructor Detail |
public RemoteTask()
Method Detail |
protected long getSequence()
notify...
to send notifications.
public void postDeregister()
postDeregister
in interface javax.management.MBeanRegistration
public void postRegister(java.lang.Boolean arg0)
postRegister
in interface javax.management.MBeanRegistration
arg0
- ignoredpublic void preDeregister() throws java.lang.Exception
preDeregister
in interface javax.management.MBeanRegistration
java.lang.Exception
public javax.management.ObjectName preRegister(javax.management.MBeanServer arg0, javax.management.ObjectName arg1) throws java.lang.Exception
preRegister
in interface javax.management.MBeanRegistration
arg0
- collocated server hosting this remote taskarg1
- the name under which this remote task is registered in the
server
java.lang.Exception
- not thrownpublic void setRunning(boolean running)
Executor
. This method sets the new value of the running
attribute and does state change notification. Wakes up all threads blocking on
this object, if any, so that they can check the state again. Do not call this method
by hand, rather call notifyExecutionStarts()
or notifyExecutionStops()
.
setRunning
in interface RemoteTaskMBean
running
- the new value of Running
attribute.public boolean isRunning()
Running
attribute.
isRunning
in interface RemoteTaskMBean
true
if execution is in progress.RemoteTaskMBean.setRunning(boolean)
protected void notifyExecutionStarts()
Running
attribute to true
on this instance. Call
this method when you start executing run()
protected void notifyExecutionStops(java.lang.String message, int success)
Running
state). Goes into Finished
state
as a side-effect.
message
- custom message string to put into the execution notification.success
- success flag that carried by stop execution notificationpublic javax.management.ObjectName getName()
getName
in interface RemoteTaskMBean
preRegister(javax.management.MBeanServer, javax.management.ObjectName)
public javax.management.AttributeList getState(boolean internal)
RemoteTaskMBean
the state of a remote task is composed of the values
of all RW attributes of the remote task. These (name, value) pairs are encapsulated
into serializable instance of AttributeList
and returned.
getState
in interface RemoteTaskMBean
internal
- if true
throws UnsupportedOperationException;
to support internal state transfer you must override this
method and pack specific, algorithm dependent-data into
the attribute list; if false
this method does
classic RW attribute capture
java.lang.UnsupportedOperationException
- if the internal data
transfer is requestedRemoteTaskMBean.copyStateFrom(AttributeList, AttributeList)
public java.io.InputStream getInputResource(java.lang.String resourceName)
resourceName
- the name of the input resource to open
public java.io.OutputStream getOutputResource(java.lang.String subspec)
subspec
- identifier appended to the file name in case this task requests multiple
output resources; if a single resource is needed, this can be
null
public final void initialize()
internalInitialize()
,
after which it invokes post-initialization script and performs state change (and
notifications) into Initialized
state.
initialize
in interface RemoteTaskMBean
protected abstract void internalInitialize()
initialize()
initialize()
protected void notifyAttributeChange(java.lang.String name, java.lang.Object oldValue, java.lang.Object newValue)
AttributeChangeNotification
out-of-thread.
name
- the name of the attribute that has changed, watch out for
proper capitalization (different than JavaBeans)oldValue
- old value, can be null
newValue
- new value of the attributepublic boolean isInitialized()
Initialized
attribute.
isInitialized
in interface RemoteTaskMBean
true
iff initialize()
has completed
successfullypublic boolean isSuspended()
Suspended
attribute.
isSuspended
in interface RemoteTaskMBean
true
iff the execution of this remote task has been
suspended, because suspendRemoteTask()
on
Executor
has been invoked for this taskExecutor
public void setSuspended(boolean suspended)
Executor
. If you wish to suspend
a task, obtain a reference to the executor and invoke suspendRemoteTask()
on it. Notifies listeners about the state transition. Notifies all threads that they
should check their state. Call checkState()
periodically in
your run()
method to react to suspended state change and block
execution if necessary.
setSuspended
in interface RemoteTaskMBean
suspended
- the new value of the suspended flag, determined by the
Executor
Executor.suspendRemoteTask(javax.management.ObjectName, boolean)
,
checkState()
protected void checkState() throws java.lang.InterruptedException
run()
loop. Subclasses should, in their
run()
methods catch this exception and hande it appropriately by gracefully
terminating the calculation. If suspended flag is true, this method will block, until
the suspended returns to false
. Also sends resumed and suspended execution
notifications as appropriate.
java.lang.InterruptedException
- if the computation has been stopped; catch and handlepublic boolean copyStateFrom(javax.management.AttributeList state, javax.management.AttributeList internal)
copyStateFrom
in interface RemoteTaskMBean
state
- the collection of RW attributesinternal
- arbitrary collection of data that is needed to replicate
internal state in another instance of the same type of remote task
true
iff state transfer has been successful
java.lang.UnsupportedOperationException
- always thrown by this default
implementationpublic boolean copyStateFrom(RemoteTaskInvocation rti)
copyStateFrom
in interface RemoteTaskMBean
rti
- task invocation data from the result repository, use to reconstruct
the state
true
iff state transfer has been successful
java.lang.UnsupportedOperationException
- always thrown by this implementationpublic boolean isFinished()
Finished
attribute.
isFinished
in interface RemoteTaskMBean
true
iff this task is in finished state, after the
execution has stoppedprotected void setFinished(boolean value)
value
- new value for the finished attributepublic double getProgress()
getProgress
in interface RemoteTaskMBean
protected void setProgress(double progress)
run()
to update the variable and send
notifications. This is used by GUI clients to track the progress of the
computation.
progress
- a value from 0 to 100 indicating progresspublic java.lang.String getPostExecutionScript()
getPostExecutionScript
in interface RemoteTaskMBean
null
iff no script is usedpublic void setPostExecutionScript(java.lang.String postExecutionScript)
RemoteTaskScript
interface.
setPostExecutionScript
in interface RemoteTaskMBean
postExecutionScript
- new BeanShell script or null
RemoteTaskScript
public java.lang.String getPostInitializactionScript()
getPostInitializactionScript
in interface RemoteTaskMBean
null
iff no script is usedinitialize()
public void setPostInitializationScript(java.lang.String postInitializationScript)
RemoteTaskScript
interface.
setPostInitializationScript
in interface RemoteTaskMBean
postInitializationScript
- script to invoke after initialization or
null
RemoteTaskScript
protected void saveMatrix(java.lang.String sub, double[][] data)
sub
- subspecification used when requesting the output resource, see
getOutputResource()
data
- the matrix to storegetOutputResource(java.lang.String)
protected double[][] loadMatrix(java.io.InputStream is)
is
- inputstream from which to read the matrix in text form
protected double[][] loadMatrix(java.lang.String input)
input
- the name of the input resource relative to the result repository
input path
public java.lang.Object executeScript(java.lang.String script)
LastScriptResult
and attribute notifications are dispatched.
executeScript
in interface RemoteTaskMBean
script
- the script to execute, must implement RemoteTaskScript
RemoteTaskScript
public java.lang.Object getLastScriptResult()
executeScript()
.
getLastScriptResult
in interface RemoteTaskMBean
null
iff no script has been executedpublic boolean isAborting()
true
iff the task is currently aborting, that is, is being
forcefully terminated. If true, stop executing as quickly as possible, without
saving the results and so on.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |