checkAndVerifyWork(work, execContext);
if (workListener != null)
{
WorkEvent event = new WorkEvent(this, WorkEvent.WORK_ACCEPTED, work, null);
workListener.workAccepted(event);
}
if (execContext == null)
{
execContext = new ExecutionContext();
}
final CountDownLatch startedLatch = new CountDownLatch(1);
wrapper = new WorkWrapper(this, work, execContext, workListener, startedLatch, null,
System.currentTimeMillis());
setup(wrapper, workListener);
BlockingExecutor executor = getExecutor(work);
if (startTimeout == WorkManager.INDEFINITE)
{
executor.executeBlocking(wrapper);
}
else
{
executor.executeBlocking(wrapper, startTimeout, TimeUnit.MILLISECONDS);
}
startedLatch.await();
return System.currentTimeMillis() - started;
}
catch (ExecutionTimedOutException etoe)
{
exception = new WorkRejectedException(etoe);
exception.setErrorCode(WorkRejectedException.START_TIMED_OUT);
}
catch (RejectedExecutionException ree)
{
exception = new WorkRejectedException(ree);
}
catch (WorkCompletedException wce)
{
if (wrapper != null)
wrapper.setWorkException(wce);
}
catch (WorkException we)
{
exception = we;
}
catch (InterruptedException ie)
{
Thread.currentThread().interrupt();
exception = new WorkRejectedException(bundle.interruptedWhileRequestingPermit());
}
finally
{
if (exception != null)
{
if (workListener != null)
{
WorkEvent event = new WorkEvent(this, WorkEvent.WORK_REJECTED, work, exception);
workListener.workRejected(event);
}
if (trace)
log.tracef("Exception %s for %s", exception, this);