예제 #1
0
 public TaskWorker(
     String owner,
     String author,
     TaskDescriptor descriptor,
     ITaskPerformer performer,
     ApplicationEventPublisher eventPublisher) {
   // initialize variables
   this.descriptor = descriptor;
   this.performer = performer;
   this.eventPublisher = eventPublisher;
   // locks
   final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
   this.writeLock = lock.writeLock();
   this.readLock = lock.readLock();
   this.executionLock = new CountDownLatch(1);
   // fill variables
   this.result = injectId(new TaskResult(), descriptor.getId());
   this.result.setDescriptorId(descriptor.getId());
   this.result.setOwner(owner);
   this.result.setAuthor(author);
   this.result.setParameters(descriptor.getParameters());
   this.result.setRegistrationDate(new Date());
   this.result.setStatus(TaskResultStatus.UNKNOWN);
   // change state
   changeState(new TaskProgress(TaskProgress.PENDING_STEP), TaskExecutionStatus.RUNNING);
 }
예제 #2
0
 @Override
 public TaskResult get(long timeout, TimeUnit unit)
     throws InterruptedException, ExecutionException, TimeoutException {
   if (!executionLock.await(timeout, unit)) {
     throw new TimeoutException(
         String.format(
             "Task '%s[%s]' hasn't completed", performer.retrieveName(), descriptor.getId()));
   }
   return result;
 }
예제 #3
0
 /**
  * Публикует событие изменения прогресса выполнения задачи
  *
  * @param targetProgress обновленный прогресс выполнения
  */
 private void changeState(TaskProgress targetProgress) {
   writeLock.lock();
   try {
     progress = targetProgress;
     doPublishEvent(
         new TaskProgressEvent(this, descriptor.getId(), performer.retrieveName(), targetProgress),
         true);
   } finally {
     writeLock.unlock();
   }
 }
예제 #4
0
 /**
  * Публикует событие изменения статуса выполнения задачи
  *
  * @param targetState обновленный статус выполнения
  */
 private void changeState(TaskExecutionStatus targetState) {
   writeLock.lock();
   try {
     status = targetState;
     doPublishEvent(
         new TaskExecutionEvent(this, descriptor.getId(), performer.retrieveName(), targetState),
         false);
   } finally {
     writeLock.unlock();
   }
 }
예제 #5
0
 @Override
 public void run() {
   // Устанавливаем системные параметры запуска
   startTask();
   // Выполняем задачу
   Map<String, Object> results = null;
   TaskResultStatus resultStatus;
   Throwable cause = null;
   try {
     results = performer.performTask(this, result.getParameters());
     resultStatus = (isCancelled()) ? TaskResultStatus.CANCELED : TaskResultStatus.FINISHED;
   } catch (Throwable th) {
     resultStatus = TaskResultStatus.ERROR;
     cause = th;
     LOG.error(
         String.format(
             "Task '%s[%s]' error: %s",
             descriptor.getId(), performer.retrieveName(), cause.getMessage()),
         cause);
   }
   // Устанавливаем системные параметры завершения
   endTask(results, resultStatus, cause);
 }