Eclipse all jobs listener
I'm trying to develop a plugin that will do some work when 开发者_运维技巧any kind of eclipse job is scheduled.
Basically I want to be able to know when a job is in the progress dialog and monitor it.
Is there a listener that can be set up to catch the begging and end of such jobs?
When you think about it, the ProgressView
does just that: whenever a Job starts, it displays it in its view.
Note, as mentioned in this thread, the ProgressView
is tightly coupled with the eclipse Jobs API (i.e., it may not monitor any plain vanilla thread)
(From the article On the Job: The Eclipse Jobs API)
So may be you can start by looking at the code in org.eclipse.ui.internal.progress.ProgressView
, which needs a ProgressViewerContentProvider
, based on a ProgressContentProvider
(implementing a IProgressUpdateCollector
)
The all thing seems to be based upon the ProgressViewUpdater
singleton, which create one UI thread in charge of monitoring those Jobs:
/**
* Create the update job that handles the updatesInfo.
*/
private void createUpdateJob() {
updateJob = new WorkbenchJob(ProgressMessages.ProgressContentProvider_UpdateProgressJob) {
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
*/
public IStatus runInUIThread(IProgressMonitor monitor) {
//Abort the job if there isn't anything
if (collectors.length == 0) {
return Status.CANCEL_STATUS;
}
if (currentInfo.updateAll) {
synchronized (updateLock) {
currentInfo.reset();
}
for (int i = 0; i < collectors.length; i++) {
collectors[i].refresh();
}
} else {
//Lock while getting local copies of the caches.
Object[] updateItems;
Object[] additionItems;
Object[] deletionItems;
synchronized (updateLock) {
currentInfo.processForUpdate();
updateItems = currentInfo.refreshes.toArray();
additionItems = currentInfo.additions.toArray();
deletionItems = currentInfo.deletions.toArray();
currentInfo.reset();
}
for (int v = 0; v < collectors.length; v++) {
IProgressUpdateCollector collector = collectors[v];
if (updateItems.length > 0) {
collector.refresh(updateItems);
}
if (additionItems.length > 0) {
collector.add(additionItems);
}
if (deletionItems.length > 0) {
collector.remove(deletionItems);
}
}
}
return Status.OK_STATUS;
}
};
updateJob.setSystem(true);
updateJob.setPriority(Job.DECORATE);
updateJob.setProperty(ProgressManagerUtil.INFRASTRUCTURE_PROPERTY, new Object());
}
You can listen to the JobChangeListener
in the IJobManager
for the beginning and ending of jobs.
IJobManager manager = Job.getJobManager();
manager.addJobChangeListener(myJobChangeListener);
IJobChangeListener
receives the following events.
public void aboutToRun(IJobChangeEvent event);
public void awake(IJobChangeEvent event);
public void done(IJobChangeEvent event);
public void running(IJobChangeEvent event);
public void scheduled(IJobChangeEvent event);
public void sleeping(IJobChangeEvent event);
I haven't found a way to track the progress of a running job, however. I'd be curious if anyone has.
精彩评论