List of usage examples for org.eclipse.jdt.internal.core.search.processing IJob getJobFamily
public String getJobFamily();
From source file:com.codenvy.ide.ext.java.server.internal.core.search.processing.JobManager.java
License:Open Source License
/** * This API is allowing to run one job in concurrence with background processing. * Indeed since other jobs are performed in background, resource sharing might be * an issue.Therefore, this functionality allows a given job to be run without * colliding with background ones.// ww w .j a v a 2 s . c om * Note: multiple thread might attempt to perform concurrent jobs at the same time, * and should synchronize (it is deliberately left to clients to decide whether * concurrent jobs might interfere or not. In general, multiple read jobs are ok). * * Waiting policy can be: * IJobConstants.ForceImmediateSearch * IJobConstants.CancelIfNotReadyToSearch * IJobConstants.WaitUntilReadyToSearch * */ public boolean performConcurrentJob(IJob searchJob, int waitingPolicy, IProgressMonitor progress) { if (VERBOSE) Util.verbose("STARTING concurrent job - " + searchJob); //$NON-NLS-1$ searchJob.ensureReadyToRun(); boolean status = IJob.FAILED; try { int concurrentJobWork = 100; if (progress != null) progress.beginTask("", concurrentJobWork); //$NON-NLS-1$ if (awaitingJobsCount() > 0) { switch (waitingPolicy) { case IJob.ForceImmediate: if (VERBOSE) Util.verbose("-> NOT READY - forcing immediate - " + searchJob);//$NON-NLS-1$ try { disable(); // pause indexing status = searchJob.execute( progress == null ? null : new SubProgressMonitor(progress, concurrentJobWork)); } finally { enable(); } if (VERBOSE) Util.verbose("FINISHED concurrent job - " + searchJob); //$NON-NLS-1$ return status; case IJob.CancelIfNotReady: if (VERBOSE) Util.verbose("-> NOT READY - cancelling - " + searchJob); //$NON-NLS-1$ if (VERBOSE) Util.verbose("CANCELED concurrent job - " + searchJob); //$NON-NLS-1$ throw new OperationCanceledException(); case IJob.WaitUntilReady: IProgressMonitor subProgress = null; try { int totalWork = 1000; if (progress != null) { subProgress = new SubProgressMonitor(progress, concurrentJobWork * 8 / 10); subProgress.beginTask("", totalWork); //$NON-NLS-1$ concurrentJobWork = concurrentJobWork * 2 / 10; } // use local variable to avoid potential NPE (see bug 20435 NPE when searching java method // and bug 42760 NullPointerException in JobManager when searching) Thread t = this.processingThread; int originalPriority = t == null ? -1 : t.getPriority(); try { if (t != null) t.setPriority(Thread.currentThread().getPriority()); synchronized (this) { this.awaitingClients++; } IJob previousJob = null; int awaitingJobsCount; int lastJobsCount = totalWork; float lastWorked = 0; float totalWorked = 0; while ((awaitingJobsCount = awaitingJobsCount()) > 0) { if ((subProgress != null && subProgress.isCanceled()) || this.processingThread == null) throw new OperationCanceledException(); IJob currentJob = currentJob(); // currentJob can be null when jobs have been added to the queue but job manager is not enabled if (currentJob != null && currentJob != previousJob) { if (VERBOSE) Util.verbose("-> NOT READY - waiting until ready - " + searchJob);//$NON-NLS-1$ if (subProgress != null) { String indexing = Messages.bind(Messages.jobmanager_filesToIndex, currentJob.getJobFamily(), Integer.toString(awaitingJobsCount)); subProgress.subTask(indexing); // ratio of the amount of work relative to the total work float ratio = awaitingJobsCount < totalWork ? 1 : ((float) totalWork) / awaitingJobsCount; if (lastJobsCount > awaitingJobsCount) { totalWorked += (lastJobsCount - awaitingJobsCount) * ratio; } else { // more jobs were added, just increment by the ratio totalWorked += ratio; } if (totalWorked - lastWorked >= 1) { subProgress.worked((int) (totalWorked - lastWorked)); lastWorked = totalWorked; } lastJobsCount = awaitingJobsCount; } previousJob = currentJob; } try { if (VERBOSE) Util.verbose("-> GOING TO SLEEP - " + searchJob);//$NON-NLS-1$ Thread.sleep(50); } catch (InterruptedException e) { // ignore } } } finally { synchronized (this) { this.awaitingClients--; } if (t != null && originalPriority > -1 && t.isAlive()) t.setPriority(originalPriority); } } finally { if (subProgress != null) subProgress.done(); } } } status = searchJob .execute(progress == null ? null : new SubProgressMonitor(progress, concurrentJobWork)); } finally { if (progress != null) progress.done(); if (VERBOSE) Util.verbose("FINISHED concurrent job - " + searchJob); //$NON-NLS-1$ } return status; }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.processing.JobManager.java
License:Open Source License
/** * Infinite loop performing resource indexing *//* w ww . j a v a 2s . c o m*/ public void run() { long idlingStart = -1; activateProcessing(); try { class ProgressJob extends Job { ProgressJob(String name) { super(name); } protected IStatus run(IProgressMonitor monitor) { IJob job = currentJob(); while (!monitor.isCanceled() && job != null) { String taskName = new StringBuffer(Messages.jobmanager_indexing) .append(Messages.bind(Messages.jobmanager_filesToIndex, job.getJobFamily(), Integer.toString(awaitingJobsCount()))) .toString(); monitor.subTask(taskName); setName(taskName); try { Thread.sleep(500); } catch (InterruptedException e) { // ignore } job = currentJob(); } return Status.OK_STATUS; } } this.progressJob = null; while (this.processingThread != null) { try { IJob job; synchronized (this) { // handle shutdown case when notifyAll came before the wait but after the while loop was entered if (this.processingThread == null) continue; // must check for new job inside this sync block to avoid timing hole if ((job = currentJob()) == null) { if (this.progressJob != null) { this.progressJob.cancel(); this.progressJob = null; } if (idlingStart < 0) idlingStart = System.currentTimeMillis(); else notifyIdle(System.currentTimeMillis() - idlingStart); this.wait(); // wait until a new job is posted (or reenabled:38901) } else { idlingStart = -1; } } if (job == null) { notifyIdle(System.currentTimeMillis() - idlingStart); // just woke up, delay before processing any new jobs, allow some time for the active thread to finish Thread.sleep(500); continue; } if (VERBOSE) { Util.verbose(awaitingJobsCount() + " awaiting jobs"); //$NON-NLS-1$ Util.verbose("STARTING background job - " + job); //$NON-NLS-1$ } try { this.executing = true; if (this.progressJob == null) { this.progressJob = new ProgressJob(Messages.bind(Messages.jobmanager_indexing, "", "")); //$NON-NLS-1$ //$NON-NLS-2$ this.progressJob.setPriority(Job.LONG); this.progressJob.setSystem(true); this.progressJob.schedule(); } /*boolean status = */job.execute(null); //if (status == FAILED) request(job); } finally { this.executing = false; if (VERBOSE) Util.verbose("FINISHED background job - " + job); //$NON-NLS-1$ moveToNextJob(); if (this.awaitingClients == 0) Thread.sleep(50); } } catch (InterruptedException e) { // background indexing was interrupted } } } catch (RuntimeException e) { if (this.processingThread != null) { // if not shutting down // log exception Util.log(e, "Background Indexer Crash Recovery"); //$NON-NLS-1$ // keep job manager alive discardJobs(null); this.processingThread = null; reset(); // this will fork a new thread with no waiting jobs, some indexes will be inconsistent } throw e; } catch (Error e) { if (this.processingThread != null && !(e instanceof ThreadDeath)) { // log exception Util.log(e, "Background Indexer Crash Recovery"); //$NON-NLS-1$ // keep job manager alive discardJobs(null); this.processingThread = null; reset(); // this will fork a new thread with no waiting jobs, some indexes will be inconsistent } throw e; } }