com.sun.sgs.impl.kernel
Class ScheduledTaskImpl

java.lang.Object
  extended by com.sun.sgs.impl.kernel.ScheduledTaskImpl
All Implemented Interfaces:
ScheduledTask

final class ScheduledTaskImpl
extends Object
implements ScheduledTask

Package-private implementation of ScheduledTask that is used to maintain state about tasks accepted by TransactionScheduler and passed on its backing SchedulerQueue.

This class implements some of the Future interface so that the associated task can be cancelled or waited on to finish. The resulting value provided by a call to get is null if the task completed successfully, or the Throwable that caused the task to fail permanently in the case of failure. See the documentation on the associated methods for more details on how interruption is handled.

Note: This class is located in the com.sun.sgs.impl.kernel package instead of the com.sun.sgs.impl.kernel.schedule package because it needs to be a package-private implementation for use by the schedulers.


Nested Class Summary
(package private) static class ScheduledTaskImpl.Builder
          We use the builder pattern here to avoid constructor explosion.
 
Field Summary
 
Fields inherited from interface com.sun.sgs.kernel.schedule.ScheduledTask
NON_RECURRING, UNBOUNDED
 
Method Summary
 boolean cancel(boolean block)
          Cancel this ScheduledTask.
(package private)  Throwable get()
          Returns null if the task completed successfully, or the Throwable that caused the task to fail permanently.
 Throwable getLastFailure()
          Returns the Throwable that was the last failure of this task or null if it has never been run or never failed.
 Identity getOwner()
          Returns the owner.
 long getPeriod()
          Returns the period for the task if it's recurring, or NON_RECURRING if this is not a recurring task.
 Priority getPriority()
          Returns the priority.
 RecurringTaskHandle getRecurringTaskHandle()
          Returns the RecurringTaskHandle associated with this task if this task is recurring, or null if this is not recurring.
 long getStartTime()
          Returns the time at which this task is scheduled to start.
 KernelRunnable getTask()
          Returns the task.
(package private)  TaskQueue getTaskQueue()
          Returns the TaskQueue associated with this task or null if no queue was set.
 long getTimeout()
          Returns the transaction timeout.
 int getTryCount()
          Returns the try count (the number of times that this task has been attempted).
(package private)  void incrementTryCount()
          Increments the try count (the number of times that this task has been attempted).
 boolean isCancelled()
          Returns whether this task has been cancelled.
(package private)  boolean isDone()
          Returns whether the task has finished.
 boolean isRecurring()
          Returns whether this is a recurring task.
(package private)  boolean isRunning()
          Returns whether this task is currently running.
(package private)  void resetStartTime()
          Re-sets the starting time to the now.
(package private)  void setDone(Throwable result)
          Sets the state of this task to done, with the result of the task being the provided Throwable which may be null to indicate that the task completed successfully.
(package private)  boolean setInterrupted()
          Similar to calling setRunning(false) except that no waiters are notified of the state change.
(package private)  void setLastFailure(Throwable lastFailure)
          Sets the Throwable that caused the last failure of this task.
 void setPriority(Priority priority)
          Sets the priority for this task.
(package private)  void setRecurringTaskHandle(RecurringTaskHandle handle)
          Sets this task's handle or throws IllegalStateException if the task is not recurring.
(package private)  boolean setRunning(boolean running)
          Sets the state of this task to running, returning false if the task has already been cancelled or has completed, or if the state is not being changed.
(package private)  void setTaskQueue(TaskQueue queue)
          Sets the TaskQueue associated with this task.
 void setTimeout(long timeout)
          Sets the transaction timeout for this task.
 String toString()
          Provides some diagnostic detail about this task.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getTask

public KernelRunnable getTask()
Returns the task.

Specified by:
getTask in interface ScheduledTask
Returns:
the KernelRunnable to run

getOwner

public Identity getOwner()
Returns the owner.

Specified by:
getOwner in interface ScheduledTask
Returns:
the Identity that owns the task

getPriority

public Priority getPriority()
Returns the priority.

Specified by:
getPriority in interface ScheduledTask
Returns:
the Priority

getStartTime

public long getStartTime()
Returns the time at which this task is scheduled to start.

Specified by:
getStartTime in interface ScheduledTask
Returns:
the scheduled run time for the task

getPeriod

public long getPeriod()
Returns the period for the task if it's recurring, or NON_RECURRING if this is not a recurring task.

Specified by:
getPeriod in interface ScheduledTask
Returns:
the period between recurring executions.

getTryCount

public int getTryCount()
Returns the try count (the number of times that this task has been attempted).

Specified by:
getTryCount in interface ScheduledTask
Returns:
the total number of times this task has been retried

getTimeout

public long getTimeout()
Returns the transaction timeout. A Scheduler may or may not choose to use this timeout value.

Specified by:
getTimeout in interface ScheduledTask
Returns:
the transaction timeout to use for this task.

getLastFailure

public Throwable getLastFailure()
Returns the Throwable that was the last failure of this task or null if it has never been run or never failed.

Specified by:
getLastFailure in interface ScheduledTask
Returns:
the Throwable that was the cause of the last failure

setPriority

public void setPriority(Priority priority)
Sets the priority for this task.

Specified by:
setPriority in interface ScheduledTask
Parameters:
priority - the new priority for this task

isRecurring

public boolean isRecurring()
Returns whether this is a recurring task. If this is not a recurring task then getPeriod should always return NON_RECURRING.

Specified by:
isRecurring in interface ScheduledTask
Returns:
true if this task is a recurring task, false otherwise.

getRecurringTaskHandle

public RecurringTaskHandle getRecurringTaskHandle()
Returns the RecurringTaskHandle associated with this task if this task is recurring, or null if this is not recurring.

Specified by:
getRecurringTaskHandle in interface ScheduledTask
Returns:
the associated RecurringTaskHandle or null

isCancelled

public boolean isCancelled()
Returns whether this task has been cancelled.

Specified by:
isCancelled in interface ScheduledTask
Returns:
true if this ScheduledTask has been cancelled, false otherwise

cancel

public boolean cancel(boolean block)
Cancel this ScheduledTask. Note that if the task is already running then calling this method may not have any affect. Also note that this method should never be called with a value of true for the block parameter if the calling thread is also currently running this ScheduledTask.

Specified by:
cancel in interface ScheduledTask
Parameters:
block - if true, this call will block until the task is cancelled or has completed, if false, it will not block
Returns:
true if the task was cancelled by this call, false otherwise

setTimeout

public void setTimeout(long timeout)
Sets the transaction timeout for this task.

Specified by:
setTimeout in interface ScheduledTask
Parameters:
timeout - the new transaction timeout for this task

get

Throwable get()
        throws InterruptedException
Returns null if the task completed successfully, or the Throwable that caused the task to fail permanently. If the task has not yet completed then this will block until the result is known or the caller is interrupted. An InterruptedException is thrown either if the calling thread is interrupted before a result is known or if the task is cancelled meaning that no result is known.

Throws:
InterruptedException

resetStartTime

void resetStartTime()
Re-sets the starting time to the now.


isDone

boolean isDone()
Returns whether the task has finished.


setRunning

boolean setRunning(boolean running)
Sets the state of this task to running, returning false if the task has already been cancelled or has completed, or if the state is not being changed.


setInterrupted

boolean setInterrupted()
Similar to calling setRunning(false) except that no waiters are notified of the state change. This is used when the task's thread is interrupted and we don't know if the task is going to be re-runnable in a new thread or has to be dropped.


isRunning

boolean isRunning()
Returns whether this task is currently running.


setDone

void setDone(Throwable result)
Sets the state of this task to done, with the result of the task being the provided Throwable which may be null to indicate that the task completed successfully. If the task is not currently running or was not previously interrupted then this method has no effect. Otherwise, the result is set and the task is marked as completed.


setRecurringTaskHandle

void setRecurringTaskHandle(RecurringTaskHandle handle)
Sets this task's handle or throws IllegalStateException if the task is not recurring.


incrementTryCount

void incrementTryCount()
Increments the try count (the number of times that this task has been attempted).


setLastFailure

void setLastFailure(Throwable lastFailure)
Sets the Throwable that caused the last failure of this task.

Parameters:
lastFailure - the last failure

setTaskQueue

void setTaskQueue(TaskQueue queue)
Sets the TaskQueue associated with this task. Typically this is used to track dependency, so that when this task is completed, the next task can be fetched from the queue.


getTaskQueue

TaskQueue getTaskQueue()
Returns the TaskQueue associated with this task or null if no queue was set. This is typically the source of dependent tasks where this task was submitted.


toString

public String toString()
Provides some diagnostic detail about this task.

Overrides:
toString in class Object
Returns:
a String representation of the task.

RedDwarf, Version 0.10.1
2010-03-14 10:56:12

Copyright © 2010 The RedDwarf Authors. All rights reserved
Copyright © 2007-2010 Sun Microsystems, Inc. All rights reserved