public abstract class Task extends Object implements Runnable
poll()
method to allow users to stop, suspend or
view progress and should post all progress messages to the message log by
calling postMessage(String, int)
.Modifier and Type | Field and Description |
---|---|
protected int |
_culledMessageCount
Number of messages removed from the message log.
|
protected String |
_description
Description of this task
|
protected long |
_expirationTime
System time at which Task should terminate if not finished
|
protected long |
_finishTime
System time when finished
|
protected Exception |
_lastException
Most recently thrown Exception
|
protected int |
_maxMessageLogSize
Maximum number of messages to hold in the message log before automaticlly
culling.
|
protected ArrayList<String> |
_messageLog
Collection of messages posted by the task.
|
protected int |
_messageLogVerbosity
Degree of verbosity
|
protected PrintWriter |
_messageWriter
An optional PrintWriter - if not null,
postMessage(String, int)
writes messages to it. |
protected String |
_owner
Identifies the initiator or owne of this task
|
protected Persistit |
_persistit
The Persistit instance this Task runs against.
|
protected long |
_startTime
System time when started
|
protected int |
_state
State of the task.
|
protected AtomicBoolean |
_stop
By default, the
stop() method sets this flag, and the
poll() method throws a TaskEndedException to stop a
running task. |
protected AtomicBoolean |
_suspend
When set, the
poll() method waits. |
protected long |
_suspendedDuration
Total time during which this Task was suspended
|
protected long |
_taskId
Task ID for this task
|
protected Thread |
_thread
The Thread created to run this task.
|
static int |
DEFAULT_MAX_MESSAGE_LOG_SIZE
Default maximum number of messages held in the message log
|
static int |
LOG_NORMAL
Indicates normal level of message logging
|
static int |
LOG_VERBOSE
Indicates a more verbose level of message logging.
|
static int |
STATE_DONE
Status value indicates this
Task finished normally. |
static int |
STATE_ENDED
Status value indicates this
Task was stopped by user
intervention. |
static int |
STATE_EXPIRED
Status value indicates this
Task 's maximum elapsed time
expired. |
static int |
STATE_FAILED
Status value indicates this
Task failed with an Exception. |
static String[] |
STATE_NAMES |
static int |
STATE_NOT_STARTED
Status value indicates this
Task has been set up but not yet
started. |
static int |
STATE_RUNNING
Status value indicates this
Task is running. |
static int |
STATE_SUSPENDED
Status value indicates this
Task is suspended. |
Modifier | Constructor and Description |
---|---|
protected |
Task() |
protected |
Task(Persistit persistit) |
Modifier and Type | Method and Description |
---|---|
protected void |
appendMessage(String fragment,
int level)
Appends a String to the final message of the log.
|
void |
cullMessages(int to)
Remove all the messages up to, but not including the specified index.
|
protected void |
endMessage(int level) |
int |
getMessageLogSize()
Returns the number of messages available in the message log.
|
int |
getMessageLogVerbosity()
Returns the current verbosity level
|
String[] |
getMessages(int from)
Get all the messages, starting from a specified index.
|
PrintWriter |
getMessageWriter()
Returns the current
PrintWriter , or null if
there is none. |
long |
getRemainingTime()
Indicates how much time remains (in milliseconds) until this
Task 's maximum time has expired. |
abstract String |
getStatus()
Returns a short String message describing the current state of this
Task . |
String |
getStatusDetail()
Returns a String message describing the current state of this
Task , possibly in greater detail than getStatus() . |
boolean |
isImmediate() |
protected long |
now()
Abbreviation for
System.currentTimeMillis() . |
protected Tree[] |
parseTreeList(String specification)
Parses a String that represents a list of Volume/Tree pairs.
|
protected void |
poll()
Tests whether this
Task should stop or suspend. |
void |
populateTaskStatus(Management.TaskStatus ts,
boolean details,
boolean clearMessages)
Copy status of this
Task to a Management.TaskStatus . |
protected void |
postMessage(String message,
int level)
Posts a message (typically to denote progress, error or other interim
information) to the message log.
|
void |
resume()
Request this task to resume.
|
void |
run()
Implementation of
Runnable . |
protected abstract void |
runTask()
Called by a newly created
Thread to perform the task. |
void |
setMaximumTime(long maxTime)
Set the maximum amount of wall-clock time this
Task will be
permitted to run. |
void |
setMessageLogVerbosity(int verbosity)
Set verbosity level for selecting posted messages.
|
void |
setMessageWriter(PrintWriter pw)
Sets a
PrintWriter to receive posted messages. |
void |
setPersistit(Persistit persistit)
Set the Persistit instance to be accessed by this Task.
|
void |
setup(long taskId,
String description,
String owner,
long maxTime,
int verbosity)
Sets up a
Task . |
void |
start()
Start this Task
|
void |
stop()
Request this task to stop.
|
void |
suspend()
Request this task to suspend.
|
public static final int STATE_NOT_STARTED
Task
has been set up but not yet
started.public static final int STATE_RUNNING
Task
is running.public static final int STATE_SUSPENDED
Task
is suspended.public static final int STATE_DONE
Task
finished normally.public static final int STATE_FAILED
Task
failed with an Exception.public static final int STATE_ENDED
Task
was stopped by user
intervention.public static final int STATE_EXPIRED
Task
's maximum elapsed time
expired.public static final String[] STATE_NAMES
public static final int LOG_NORMAL
public static final int LOG_VERBOSE
public static final int DEFAULT_MAX_MESSAGE_LOG_SIZE
protected Persistit _persistit
protected long _taskId
protected String _description
protected String _owner
protected long _startTime
protected long _finishTime
protected long _expirationTime
protected long _suspendedDuration
protected AtomicBoolean _stop
stop()
method sets this flag, and the
poll()
method throws a TaskEndedException
to stop a
running task.protected AtomicBoolean _suspend
poll()
method waits.protected Exception _lastException
protected int _state
protected int _maxMessageLogSize
protected int _culledMessageCount
protected int _messageLogVerbosity
protected final ArrayList<String> _messageLog
protected PrintWriter _messageWriter
postMessage(String, int)
writes messages to it.protected Thread _thread
protected Task()
protected Task(Persistit persistit)
public void setPersistit(Persistit persistit)
persistit
- protected abstract void runTask() throws Exception
Thread
to perform the task.Exception
protected void poll()
Task
should stop or suspend. The concrete
task implementation should call poll
frequently. The default
mechanism for stopping a task does not use the deprecated
Thread.stop()
method because it is unsafe. Instead, the task
should call poll().protected Tree[] parseTreeList(String specification) throws PersistitException
specification
- The list of Volume/Tree pairs, specified as a StringPersistitException
protected long now()
System.currentTimeMillis()
.public void setup(long taskId, String description, String owner, long maxTime, int verbosity) throws Exception
Task
.taskId
- unique identifier for this task invocationdescription
- Description of this taskowner
- Hostname and/or usernamemaxTime
- Maximum wall-clock duration, in milliseconds, that Task will
be allowed to runverbosity
- Level at which messages posted by the running task will be
retained in the message log.Exception
public void start()
IllegalStateException
- If this task has already been startedpublic void stop()
_thread.stop()
if necessary.public void suspend()
_thread.suspend()
if necessary.public void resume()
_thread.resume()
if necessary.public void setMaximumTime(long maxTime)
Task
will be
permitted to run. If the Task
is suspended, the amount of
time spend in the suspended state is not counted toward this maximum.maxTime
- The time, in millisecondspublic void setMessageLogVerbosity(int verbosity)
LOG_NORMAL
and LOG_VERBOSE
.verbosity
- Verbositypublic int getMessageLogVerbosity()
public void setMessageWriter(PrintWriter pw)
PrintWriter
to receive posted messages.pw
- The PrintWriter
public PrintWriter getMessageWriter()
PrintWriter
, or null
if
there is none.PrintWriter
public abstract String getStatus()
Task
. It should convey a measurement of progress to the
end-user.Task
's current statepublic String getStatusDetail()
Task
, possibly in greater detail than getStatus()
.
The default implementation returns the same description as
getStatus
.Task
's current state.public long getRemainingTime()
Task
's maximum time has expired.protected void postMessage(String message, int level)
message
- The messagelevel
- Indicates the verbosity level. The message is posted only if
the level of the message is below the current verbosity
threshhold set through setMessageLogVerbosity(int)
.protected void endMessage(int level)
protected void appendMessage(String fragment, int level)
fragment
- The message fragment to appendlevel
- Indicates the verbosity level. The message is posted only if
the level of the message is below the current verbosity
threshold set through setMessageLogVerbosity(int)
.public int getMessageLogSize()
public String[] getMessages(int from)
from
- The indexpublic void cullMessages(int to)
to
- Index of first message not to remove.public void populateTaskStatus(Management.TaskStatus ts, boolean details, boolean clearMessages)
Task
to a Management.TaskStatus
.ts
- The TaskStatus
details
- true
to include messages and status detailclearMessages
- true
to cull the messages being returnedpublic boolean isImmediate()
Copyright © 2025 Open Identity Platform Community. All rights reserved.