Esempio n. 1
0
  protected void executeJob() {
    try {
      commandExecutor.execute(new ExecuteAsyncJobCmd(job));

    } catch (final ActivitiOptimisticLockingException e) {

      handleFailedJob(e);

      if (log.isDebugEnabled()) {
        log.debug(
            "Optimistic locking exception during job execution. If you have multiple async executors running against the same database, "
                + "this exception means that this thread tried to acquire an exclusive job, which already was changed by another async executor thread."
                + "This is expected behavior in a clustered environment. "
                + "You can ignore this message if you indeed have multiple job executor threads running against the same database. "
                + "Exception message: {}",
            e.getMessage());
      }

    } catch (Throwable exception) {
      handleFailedJob(exception);

      // Finally, Throw the exception to indicate the ExecuteAsyncJobCmd failed
      String message = "Job " + job.getId() + " failed";
      log.error(message, exception);
    }
  }
Esempio n. 2
0
  /**
   * Returns true if lock succeeded, or no lock was needed. Returns false if locking was
   * unsuccessfull.
   */
  protected boolean lockJobIfNeeded() {
    try {
      if (job.isExclusive()) {
        commandExecutor.execute(new LockExclusiveJobCmd(job));
      }

    } catch (Throwable lockException) {
      if (log.isDebugEnabled()) {
        log.debug(
            "Exception during exclusive job acquisition. Retrying job.",
            lockException.getMessage());
      }

      commandExecutor.execute(
          new Command<Void>() {
            public Void execute(CommandContext commandContext) {
              commandContext.getJobEntityManager().retryAsyncJob(job);
              return null;
            }
          });

      return false;
    }

    return true;
  }
Esempio n. 3
0
  public Object execute(CommandContext commandContext) {
    if (jobId == null) {
      throw new ActivitiException("jobId is null");
    }

    if (log.isLoggable(Level.FINE)) {
      log.fine("Executing job " + jobId);
    }
    JobEntity job = commandContext.getJobManager().findJobById(jobId);

    if (job == null) {
      throw new ActivitiException("No job found with id '" + jobId + "'");
    }

    JobExecutorContext jobExecutorContext = Context.getJobExecutorContext();
    if (jobExecutorContext != null) { // if null, then we are not called by the job executor
      jobExecutorContext.setCurrentJob(job);
    }

    try {
      job.execute(commandContext);
    } catch (RuntimeException exception) {
      // When transaction is rolled back, decrement retries
      CommandExecutor commandExecutor =
          Context.getProcessEngineConfiguration().getCommandExecutorTxRequiresNew();

      commandContext
          .getTransactionContext()
          .addTransactionListener(
              TransactionState.ROLLED_BACK,
              new DecrementJobRetriesListener(commandExecutor, jobId, exception));

      // throw the original exception to indicate the ExecuteJobCmd failed
      throw exception;
    } finally {
      if (jobExecutorContext != null) {
        jobExecutorContext.setCurrentJob(null);
      }
    }
    return null;
  }
Esempio n. 4
0
  protected void unlockJobIfNeeded() {
    try {
      if (job.isExclusive()) {
        commandExecutor.execute(new UnlockExclusiveJobCmd(job));
      }

    } catch (ActivitiOptimisticLockingException optimisticLockingException) {
      if (log.isDebugEnabled()) {
        log.debug(
            "Optimistic locking exception while unlocking the job. If you have multiple async executors running against the same database, "
                + "this exception means that this thread tried to acquire an exclusive job, which already was changed by another async executor thread."
                + "This is expected behavior in a clustered environment. "
                + "You can ignore this message if you indeed have multiple job executor acquisition threads running against the same database. "
                + "Exception message: {}",
            optimisticLockingException.getMessage());
      }

    } catch (Throwable t) {
      log.error("Error while unlocking exclusive job " + job.getId(), t);
    }
  }