Class AbstractJob

java.lang.Object
com.northconcepts.datapipeline.job.AbstractJob
All Implemented Interfaces:
IJob, Runnable
Direct Known Subclasses:
RunnableJobAdapter

public abstract class AbstractJob extends Object implements IJob
Used to run, pause, and cancel Runnables.
  • Field Details

  • Constructor Details

    • AbstractJob

      public AbstractJob()
  • Method Details

    • getCurrent

      public static AbstractJob getCurrent()
      Returns the AbstractJob currently running in this thread or null if there is none.
    • getAll

      public static List<AbstractJob> getAll()
      Returns all the AbstractJobs currently running in the JVM/classloader, not including Jobs.
    • getParent

      public AbstractJob getParent()
      Returns the thread-local job that ran this one while this job is running.
      Specified by:
      getParent in interface IJob
    • getChild

      public AbstractJob getChild()
      Returns the thread-local child job of this one while this job is running.
      Specified by:
      getChild in interface IJob
    • getUuid

      public UUID getUuid()
      Returns the universally unique identifier (UUID) for this job.
      Specified by:
      getUuid in interface IJob
    • getName

      public String getName()
      Returns the name assigned to this job or its default name if one was not assigned.
      Specified by:
      getName in interface IJob
    • setName

      public AbstractJob setName(String name)
      Assigns a new name to this job.
      Specified by:
      setName in interface IJob
    • getCreatedOn

      public Date getCreatedOn()
      Returns the time this job instance was created.
      Specified by:
      getCreatedOn in interface IJob
    • getStartedOn

      public Date getStartedOn()
      Returns the time this job instance started running.
      Specified by:
      getStartedOn in interface IJob
    • getCancelledOn

      public Date getCancelledOn()
      Returns the time this job instance was cancelled.
      Specified by:
      getCancelledOn in interface IJob
    • isCancelled

      public boolean isCancelled()
      Returns true if this job was cancelled.
      Specified by:
      isCancelled in interface IJob
    • isCancelled

      public boolean isCancelled(Throwable exception)
      Returns true if this exception is due to this job being cancelled.
    • getPausedOn

      public Date getPausedOn()
      Returns the time this job was paused or null if this job is not currently paused.
      Specified by:
      getPausedOn in interface IJob
    • isPaused

      public boolean isPaused()
      Returns true if this job is currently paused.
      Specified by:
      isPaused in interface IJob
    • getFinishedOn

      public Date getFinishedOn()
      Returns the time this job finished running.
      Specified by:
      getFinishedOn in interface IJob
    • isFinished

      public boolean isFinished()
      Returns true if this job has finished running.
      Specified by:
      isFinished in interface IJob
    • getException

      public DataException getException()
      Returns the last exception thrown while running.
      Specified by:
      getException in interface IJob
    • isFailed

      public boolean isFailed()
      Returns true if this job has completed unsuccessfully.
      Specified by:
      isFailed in interface IJob
    • getRunThread

      public Thread getRunThread()
      Returns the thread currently executing this job or null if the job is not running.
      Specified by:
      getRunThread in interface IJob
    • isRunning

      public boolean isRunning()
      Returns true if this job is currently running.
      Specified by:
      isRunning in interface IJob
    • getRunningTime

      public long getRunningTime()
      Returns the number of milliseconds this job was (or has been) running for or zero (0) if it hasn't yet started running.
      Specified by:
      getRunningTime in interface IJob
    • getRunningTimeAsString

      public String getRunningTimeAsString()
      Returns the time this job was (or has been) running in long form as a human readable string (2 Years, 1 Second, 12 Millisecond) or null if it hasn't yet started running.
      Specified by:
      getRunningTimeAsString in interface IJob
    • getRunningTimeAsString

      public String getRunningTimeAsString(boolean shortForm)
      Returns the time this job was (or has been) running as a human readable string in long form (2 Years, 1 Second, 12 Millisecond) or short form (2y 1s 12ms) or null if it hasn't yet started running.
      Specified by:
      getRunningTimeAsString in interface IJob
    • pause

      public AbstractJob pause()
      Description copied from interface: IJob
      Asks this job to pause at its next pause check (between records for a Job) until IJob.resume() is called.
      Specified by:
      pause in interface IJob
    • resume

      public AbstractJob resume()
      Description copied from interface: IJob
      Lets a paused job continue; does nothing if it is not paused.
      Specified by:
      resume in interface IJob
    • cancel

      public AbstractJob cancel()
      Description copied from interface: IJob
      Interrupts the run thread and cancels the child job; does nothing unless this job is running and not yet cancelled.
      Specified by:
      cancel in interface IJob
    • onStart

      protected void onStart()
      Called on the run thread when this job starts, before runImpl().
    • onFinish

      protected void onFinish()
      Called on the run thread after onSuccess() or onFailure(), even if they throw.
    • onPause

      protected void onPause()
      Called on the run thread when it pauses in handlePause().
    • onResume

      protected void onResume()
      Called on the run thread when it resumes in handlePause().
    • onSuccess

      protected void onSuccess()
      Called on the run thread after runImpl() completes without throwing.
    • onFailure

      protected void onFailure()
      Called on the run thread after runImpl() throws; see getException().
    • setLogWriter

      public AbstractJob setLogWriter(DataWriter logWriter)
    • getLogWriter

      public DataWriter getLogWriter()
    • isAutoCloseLogWriter

      public boolean isAutoCloseLogWriter()
    • setAutoCloseLogWriter

      public AbstractJob setAutoCloseLogWriter(boolean autoCloseLogWriter)
    • log

      public AbstractJob log()
    • run

      public void run()
      Executes this job in the current thread. This method blocks until the transfer either completes successfully or fails for any reason.
      Specified by:
      run in interface IJob
      Specified by:
      run in interface Runnable
    • runImpl

      protected abstract void runImpl() throws Throwable
      Performs this job's work on the run thread; call handlePause() and handleCancel() regularly so pause and cancel requests take effect.
      Throws:
      Throwable
    • runAsync

      public AbstractJob runAsync()
      Executes this job in a new thread. This method does not block.
      Specified by:
      runAsync in interface IJob
    • waitUntilStarted

      public AbstractJob waitUntilStarted() throws InterruptedException
      Waits for this job to start.
      Specified by:
      waitUntilStarted in interface IJob
      Returns:
      Throws:
      InterruptedException
    • waitUntilStarted

      public AbstractJob waitUntilStarted(long timeout) throws InterruptedException
      Waits up to timeout milliseconds for this job to start or throws InterruptedException.
      Specified by:
      waitUntilStarted in interface IJob
      Returns:
      Throws:
      InterruptedException
    • waitUntilFinished

      public AbstractJob waitUntilFinished() throws InterruptedException
      Waits for this job to complete.
      Specified by:
      waitUntilFinished in interface IJob
      Returns:
      Throws:
      InterruptedException
    • waitUntilFinished

      public AbstractJob waitUntilFinished(long timeout) throws InterruptedException
      Waits up to timeout milliseconds for this job to complete or throws InterruptedException.
      Specified by:
      waitUntilFinished in interface IJob
      Returns:
      Throws:
      InterruptedException
    • handlePause

      protected void handlePause() throws InterruptedException
      Blocks while this job is paused, calling onPause() before and onResume() after the wait.
      Throws:
      InterruptedException
    • handleCancel

      protected void handleCancel() throws InterruptedException
      Clears the thread's interrupted flag and throws InterruptedException if this job or its parent was cancelled.
      Throws:
      InterruptedException
    • clone

      protected Object clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • toDebugString

      public String toDebugString()