Class JavaScriptJobManagerImpl
- java.lang.Object
-
- org.htmlunit.javascript.background.JavaScriptJobManagerImpl
-
- All Implemented Interfaces:
java.io.Serializable,JavaScriptJobManager
class JavaScriptJobManagerImpl extends java.lang.Object implements JavaScriptJobManager
Default implementation of
JavaScriptJobManager.This job manager class is guaranteed not to keep old windows in memory (no window memory leaks).
This job manager is serializable, but any running jobs are transient and are not serialized.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.htmlunit.javascript.background.JavaScriptJobManager
JavaScriptJobManager.JavaScriptJobFilter
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.ArrayList<java.lang.Integer>cancelledJobs_private JavaScriptJobcurrentlyRunningJob_private static org.apache.commons.logging.LogLOGLogging support.private static java.util.concurrent.atomic.AtomicIntegerNEXT_JOB_ID_A counter used to generate the IDs assigned toJavaScriptJobs.private java.util.PriorityQueue<JavaScriptJob>scheduledJobsQ_Queue of jobs that are scheduled to run.private java.lang.ref.WeakReference<WebWindow>window_The window to which this job manager belongs (weakly referenced, so as not to leak memory).
-
Constructor Summary
Constructors Constructor Description JavaScriptJobManagerImpl(WebWindow window)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intaddJob(JavaScriptJob job, Page page)Adds the specified job to this job manager, assigning it an ID.JavaScriptJobgetEarliestJob()Gets the earliest job for this manager.JavaScriptJobgetEarliestJob(JavaScriptJobManager.JavaScriptJobFilter filter)Gets the earliest job for this manager.intgetJobCount()Returns the number of active jobs, including jobs that are currently executing and jobs that are waiting to execute.intgetJobCount(JavaScriptJobManager.JavaScriptJobFilter filter)Returns the number of active jobs, including jobs that are currently executing and jobs that are waiting to execute.private WebWindowgetWindow()Returns the window to which this job manager belongs, ornullif it has been garbage collected.java.lang.StringjobStatusDump(JavaScriptJobManager.JavaScriptJobFilter filter)INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.private voidprintQueue()Utility method to print current queue.private voidreadObject(java.io.ObjectInputStream in)Our own serialization (to handle the weak reference)voidremoveAllJobs()Removes all jobs from the execution queue.voidremoveJob(int id)Removes the specified job from the execution queue.booleanrunSingleJob(JavaScriptJob givenJob)Runs the provided job if it is the right time for it.voidshutdown()Shuts down this job manager and stops all of its jobs.voidstopJob(int id)Stops the specified job and removes it from the execution queue, not even allowing the job to finish if it is currently executing.intwaitForJobs(long timeoutMillis)Blocks until all active jobs have finished executing.intwaitForJobsStartingBefore(long delayMillis)Blocks until all jobs scheduled to start executing before(now + delayMillis)have finished executing.intwaitForJobsStartingBefore(long delayMillis, JavaScriptJobManager.JavaScriptJobFilter filter)Blocks until all jobs scheduled to start executing before(now + delayMillis)have finished executing.
-
-
-
Field Detail
-
window_
private final transient java.lang.ref.WeakReference<WebWindow> window_
The window to which this job manager belongs (weakly referenced, so as not to leak memory).
-
scheduledJobsQ_
private transient java.util.PriorityQueue<JavaScriptJob> scheduledJobsQ_
Queue of jobs that are scheduled to run. This is a priority queue, sorted by closest target execution time.
-
cancelledJobs_
private transient java.util.ArrayList<java.lang.Integer> cancelledJobs_
-
currentlyRunningJob_
private transient JavaScriptJob currentlyRunningJob_
-
NEXT_JOB_ID_
private static final java.util.concurrent.atomic.AtomicInteger NEXT_JOB_ID_
A counter used to generate the IDs assigned toJavaScriptJobs.
-
LOG
private static final org.apache.commons.logging.Log LOG
Logging support.
-
-
Constructor Detail
-
JavaScriptJobManagerImpl
JavaScriptJobManagerImpl(WebWindow window)
Creates a new instance.- Parameters:
window- the window associated with the new job manager
-
-
Method Detail
-
getJobCount
public int getJobCount()
Returns the number of active jobs, including jobs that are currently executing and jobs that are waiting to execute.- Specified by:
getJobCountin interfaceJavaScriptJobManager- Returns:
- the number of active jobs
-
getJobCount
public int getJobCount(JavaScriptJobManager.JavaScriptJobFilter filter)
Returns the number of active jobs, including jobs that are currently executing and jobs that are waiting to execute. Only jobs passing the filter are counted.- Specified by:
getJobCountin interfaceJavaScriptJobManager- Parameters:
filter- the JavaScriptJobFilter- Returns:
- the number of active jobs
-
addJob
public int addJob(JavaScriptJob job, Page page)
Adds the specified job to this job manager, assigning it an ID. If the specified page is not currently loaded in the window which owns this job manager, the operation fails and this method returns0.- Specified by:
addJobin interfaceJavaScriptJobManager- Parameters:
job- the job to add to the job managerpage- the page which is trying to add the job- Returns:
- the ID assigned to the job
-
removeJob
public void removeJob(int id)
Removes the specified job from the execution queue. This doesn't interrupt the job if it is currently running.- Specified by:
removeJobin interfaceJavaScriptJobManager- Parameters:
id- the ID of the job to be removed from the execution queue
-
stopJob
public void stopJob(int id)
Stops the specified job and removes it from the execution queue, not even allowing the job to finish if it is currently executing.- Specified by:
stopJobin interfaceJavaScriptJobManager- Parameters:
id- the ID of the job to be stopped
-
removeAllJobs
public void removeAllJobs()
Removes all jobs from the execution queue. This doesn't interrupt any jobs that may be currently running.- Specified by:
removeAllJobsin interfaceJavaScriptJobManager
-
waitForJobs
public int waitForJobs(long timeoutMillis)
Blocks until all active jobs have finished executing. If a job is scheduled to begin executing after(now + timeoutMillis), this method will wait fortimeoutMillismilliseconds and then returnfalse.- Specified by:
waitForJobsin interfaceJavaScriptJobManager- Parameters:
timeoutMillis- the maximum amount of time to wait (in milliseconds); may be negative, in which case this method returns immediately- Returns:
- the number of background JavaScript jobs still executing or waiting to be executed when this
method returns; will be
0if there are no jobs left to execute
-
waitForJobsStartingBefore
public int waitForJobsStartingBefore(long delayMillis)
Blocks until all jobs scheduled to start executing before(now + delayMillis)have finished executing. If there is no background JavaScript task currently executing, and there is no background JavaScript task scheduled to start executing within the specified time, this method returns immediately -- even if there are tasks scheduled to be executed after(now + delayMillis).- Specified by:
waitForJobsStartingBeforein interfaceJavaScriptJobManager- Parameters:
delayMillis- the delay which determines the background tasks to wait for (in milliseconds); may be negative, as it is relative to the current time- Returns:
- the number of background JavaScript jobs still executing or waiting to be executed when this
method returns; will be
0if there are no jobs left to execute
-
waitForJobsStartingBefore
public int waitForJobsStartingBefore(long delayMillis, JavaScriptJobManager.JavaScriptJobFilter filter)Blocks until all jobs scheduled to start executing before(now + delayMillis)have finished executing. If there is no background JavaScript task currently executing, and there is no background JavaScript task scheduled to start executing within the specified time, this method returns immediately -- even if there are tasks scheduled to be executed after(now + delayMillis).- Specified by:
waitForJobsStartingBeforein interfaceJavaScriptJobManager- Parameters:
delayMillis- the delay which determines the background tasks to wait for (in milliseconds); may be negative, as it is relative to the current timefilter- the JavaScriptJobFilter- Returns:
- the number of background JavaScript jobs still executing or waiting to be executed when this
method returns; will be
0if there are no jobs left to execute
-
shutdown
public void shutdown()
Shuts down this job manager and stops all of its jobs.- Specified by:
shutdownin interfaceJavaScriptJobManager
-
getWindow
private WebWindow getWindow()
Returns the window to which this job manager belongs, ornullif it has been garbage collected.- Returns:
- the window to which this job manager belongs, or
nullif it has been garbage collected
-
printQueue
private void printQueue()
Utility method to print current queue.
-
jobStatusDump
public java.lang.String jobStatusDump(JavaScriptJobManager.JavaScriptJobFilter filter)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Specified by:
jobStatusDumpin interfaceJavaScriptJobManager- Parameters:
filter- the JavaScriptJobFilter- Returns:
- the job status report as string
-
getEarliestJob
public JavaScriptJob getEarliestJob()
Gets the earliest job for this manager.- Specified by:
getEarliestJobin interfaceJavaScriptJobManager- Returns:
nullif none
-
getEarliestJob
public JavaScriptJob getEarliestJob(JavaScriptJobManager.JavaScriptJobFilter filter)
Gets the earliest job for this manager.- Specified by:
getEarliestJobin interfaceJavaScriptJobManager- Parameters:
filter- the JavaScriptJobFilter- Returns:
nullif none
-
runSingleJob
public boolean runSingleJob(JavaScriptJob givenJob)
Runs the provided job if it is the right time for it.- Specified by:
runSingleJobin interfaceJavaScriptJobManager- Parameters:
givenJob- the job to run- Returns:
- returns true if the job was run.
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundExceptionOur own serialization (to handle the weak reference)- Parameters:
in- the stream to read form- Throws:
java.io.IOException- in case of errorjava.lang.ClassNotFoundException- in case of error
-
-