Search in sources :

Example 6 with JobExecutor

use of org.activiti.engine.impl.jobexecutor.JobExecutor in project Activiti by Activiti.

the class JobRetryCmd method execute.

public Object execute(CommandContext commandContext) {
    JobEntity job = commandContext.getJobEntityManager().findJobById(jobId);
    if (job == null) {
        return null;
    }
    ActivityImpl activity = getCurrentActivity(commandContext, job);
    ProcessEngineConfiguration processEngineConfig = commandContext.getProcessEngineConfiguration();
    if (activity == null || activity.getFailedJobRetryTimeCycleValue() == null) {
        log.debug("activitiy or FailedJobRetryTimerCycleValue is null in job " + jobId + "'. only decrementing retries.");
        job.setRetries(job.getRetries() - 1);
        job.setLockOwner(null);
        job.setLockExpirationTime(null);
        if (job.getDuedate() == null) {
            // add wait time for failed async job
            job.setDuedate(calculateDueDate(commandContext, processEngineConfig.getAsyncFailedJobWaitTime(), null));
        } else {
            // add default wait time for failed job
            job.setDuedate(calculateDueDate(commandContext, processEngineConfig.getDefaultFailedJobWaitTime(), job.getDuedate()));
        }
    } else {
        String failedJobRetryTimeCycle = activity.getFailedJobRetryTimeCycleValue();
        try {
            DurationHelper durationHelper = new DurationHelper(failedJobRetryTimeCycle, processEngineConfig.getClock());
            job.setLockOwner(null);
            job.setLockExpirationTime(null);
            job.setDuedate(durationHelper.getDateAfter());
            if (job.getExceptionMessage() == null) {
                // is it the first exception 
                log.debug("Applying JobRetryStrategy '" + failedJobRetryTimeCycle + "' the first time for job " + job.getId() + " with " + durationHelper.getTimes() + " retries");
                // then change default retries to the ones configured
                job.setRetries(durationHelper.getTimes());
            } else {
                log.debug("Decrementing retries of JobRetryStrategy '" + failedJobRetryTimeCycle + "' for job " + job.getId());
            }
            job.setRetries(job.getRetries() - 1);
        } catch (Exception e) {
            throw new ActivitiException("failedJobRetryTimeCylcle has wrong format:" + failedJobRetryTimeCycle, exception);
        }
    }
    if (exception != null) {
        job.setExceptionMessage(exception.getMessage());
        job.setExceptionStacktrace(getExceptionStacktrace());
    }
    // Dispatch both an update and a retry-decrement event
    ActivitiEventDispatcher eventDispatcher = commandContext.getEventDispatcher();
    if (eventDispatcher.isEnabled()) {
        eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_UPDATED, job));
        eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.JOB_RETRIES_DECREMENTED, job));
    }
    if (processEngineConfig.isAsyncExecutorEnabled() == false) {
        JobExecutor jobExecutor = processEngineConfig.getJobExecutor();
        JobAddedNotification messageAddedNotification = new JobAddedNotification(jobExecutor);
        TransactionContext transactionContext = commandContext.getTransactionContext();
        transactionContext.addTransactionListener(TransactionState.COMMITTED, messageAddedNotification);
    }
    return null;
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) ActivitiException(org.activiti.engine.ActivitiException) ProcessEngineConfiguration(org.activiti.engine.ProcessEngineConfiguration) ActivityImpl(org.activiti.engine.impl.pvm.process.ActivityImpl) DurationHelper(org.activiti.engine.impl.calendar.DurationHelper) JobExecutor(org.activiti.engine.impl.jobexecutor.JobExecutor) TransactionContext(org.activiti.engine.impl.cfg.TransactionContext) JobAddedNotification(org.activiti.engine.impl.jobexecutor.JobAddedNotification) ActivitiException(org.activiti.engine.ActivitiException) ActivitiEventDispatcher(org.activiti.engine.delegate.event.ActivitiEventDispatcher)

Example 7 with JobExecutor

use of org.activiti.engine.impl.jobexecutor.JobExecutor in project Activiti by Activiti.

the class JobTestHelper method waitForJobExecutorToProcessAllJobs.

public static void waitForJobExecutorToProcessAllJobs(ProcessEngineConfiguration processEngineConfiguration, ManagementService managementService, long maxMillisToWait, long intervalMillis, boolean shutdownExecutorWhenFinished) {
    JobExecutor jobExecutor = null;
    AsyncExecutor asyncExecutor = null;
    if (processEngineConfiguration.isAsyncExecutorEnabled() == false) {
        jobExecutor = processEngineConfiguration.getJobExecutor();
        jobExecutor.start();
    } else {
        asyncExecutor = processEngineConfiguration.getAsyncExecutor();
        asyncExecutor.start();
    }
    try {
        Timer timer = new Timer();
        InteruptTask task = new InteruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        boolean areJobsAvailable = true;
        try {
            while (areJobsAvailable && !task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
                try {
                    areJobsAvailable = areJobsAvailable(managementService);
                } catch (Throwable t) {
                // Ignore, possible that exception occurs due to locking/updating of table on MSSQL when
                // isolation level doesn't allow READ of the table
                }
            }
        } catch (InterruptedException e) {
        // ignore
        } finally {
            timer.cancel();
        }
        if (areJobsAvailable) {
            throw new ActivitiException("time limit of " + maxMillisToWait + " was exceeded");
        }
    } finally {
        if (shutdownExecutorWhenFinished) {
            if (processEngineConfiguration.isAsyncExecutorEnabled() == false) {
                jobExecutor.shutdown();
            } else {
                asyncExecutor.shutdown();
            }
        }
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Timer(java.util.Timer) JobExecutor(org.activiti.engine.impl.jobexecutor.JobExecutor) AsyncExecutor(org.activiti.engine.impl.asyncexecutor.AsyncExecutor)

Example 8 with JobExecutor

use of org.activiti.engine.impl.jobexecutor.JobExecutor in project Activiti by Activiti.

the class JobTestHelper method waitForJobExecutorOnCondition.

public static void waitForJobExecutorOnCondition(ProcessEngineConfiguration processEngineConfiguration, long maxMillisToWait, long intervalMillis, Callable<Boolean> condition) {
    JobExecutor jobExecutor = null;
    AsyncExecutor asyncExecutor = null;
    if (processEngineConfiguration.isAsyncExecutorEnabled() == false) {
        jobExecutor = processEngineConfiguration.getJobExecutor();
        jobExecutor.start();
    } else {
        asyncExecutor = processEngineConfiguration.getAsyncExecutor();
        asyncExecutor.start();
    }
    try {
        Timer timer = new Timer();
        InteruptTask task = new InteruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        boolean conditionIsViolated = true;
        try {
            while (conditionIsViolated) {
                Thread.sleep(intervalMillis);
                conditionIsViolated = !condition.call();
            }
        } catch (InterruptedException e) {
        // ignore
        } catch (Exception e) {
            throw new ActivitiException("Exception while waiting on condition: " + e.getMessage(), e);
        } finally {
            timer.cancel();
        }
        if (conditionIsViolated) {
            throw new ActivitiException("time limit of " + maxMillisToWait + " was exceeded");
        }
    } finally {
        if (processEngineConfiguration.isAsyncExecutorEnabled() == false) {
            jobExecutor.shutdown();
        } else {
            asyncExecutor.shutdown();
        }
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Timer(java.util.Timer) JobExecutor(org.activiti.engine.impl.jobexecutor.JobExecutor) ActivitiException(org.activiti.engine.ActivitiException) AsyncExecutor(org.activiti.engine.impl.asyncexecutor.AsyncExecutor)

Example 9 with JobExecutor

use of org.activiti.engine.impl.jobexecutor.JobExecutor in project Activiti by Activiti.

the class JobTestHelper method executeJobExecutorForTime.

public static void executeJobExecutorForTime(ProcessEngineConfiguration processEngineConfiguration, long maxMillisToWait, long intervalMillis) {
    JobExecutor jobExecutor = null;
    AsyncExecutor asyncExecutor = null;
    if (processEngineConfiguration.isAsyncExecutorEnabled() == false) {
        jobExecutor = processEngineConfiguration.getJobExecutor();
        jobExecutor.start();
    } else {
        asyncExecutor = processEngineConfiguration.getAsyncExecutor();
        asyncExecutor.start();
    }
    try {
        Timer timer = new Timer();
        InteruptTask task = new InteruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        try {
            while (!task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
            }
        } catch (InterruptedException e) {
        // ignore
        } finally {
            timer.cancel();
        }
    } finally {
        if (processEngineConfiguration.isAsyncExecutorEnabled() == false) {
            jobExecutor.shutdown();
        } else {
            asyncExecutor.shutdown();
        }
    }
}
Also used : Timer(java.util.Timer) JobExecutor(org.activiti.engine.impl.jobexecutor.JobExecutor) AsyncExecutor(org.activiti.engine.impl.asyncexecutor.AsyncExecutor)

Example 10 with JobExecutor

use of org.activiti.engine.impl.jobexecutor.JobExecutor in project Activiti by Activiti.

the class BaseSpringRestTestCase method waitForJobExecutorToProcessAllJobs.

public void waitForJobExecutorToProcessAllJobs(long maxMillisToWait, long intervalMillis) {
    JobExecutor jobExecutor = null;
    AsyncExecutor asyncExecutor = null;
    if (processEngineConfiguration.isAsyncExecutorEnabled() == false) {
        jobExecutor = processEngineConfiguration.getJobExecutor();
        jobExecutor.start();
    } else {
        asyncExecutor = processEngineConfiguration.getAsyncExecutor();
        asyncExecutor.start();
    }
    try {
        Timer timer = new Timer();
        InteruptTask task = new InteruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        boolean areJobsAvailable = true;
        try {
            while (areJobsAvailable && !task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
                areJobsAvailable = areJobsAvailable();
            }
        } catch (InterruptedException e) {
        } finally {
            timer.cancel();
        }
        if (areJobsAvailable) {
            throw new ActivitiException("time limit of " + maxMillisToWait + " was exceeded");
        }
    } finally {
        if (processEngineConfiguration.isAsyncExecutorEnabled() == false) {
            jobExecutor.shutdown();
        } else {
            asyncExecutor.shutdown();
        }
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Timer(java.util.Timer) JobExecutor(org.activiti.engine.impl.jobexecutor.JobExecutor) AsyncExecutor(org.activiti.engine.impl.asyncexecutor.AsyncExecutor)

Aggregations

JobExecutor (org.activiti.engine.impl.jobexecutor.JobExecutor)11 Timer (java.util.Timer)9 ActivitiException (org.activiti.engine.ActivitiException)9 AsyncExecutor (org.activiti.engine.impl.asyncexecutor.AsyncExecutor)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 JobAddedNotification (org.activiti.engine.impl.jobexecutor.JobAddedNotification)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 ProcessEngineConfiguration (org.activiti.engine.ProcessEngineConfiguration)1 ActivitiEventDispatcher (org.activiti.engine.delegate.event.ActivitiEventDispatcher)1 DurationHelper (org.activiti.engine.impl.calendar.DurationHelper)1 TransactionContext (org.activiti.engine.impl.cfg.TransactionContext)1 TransactionListener (org.activiti.engine.impl.cfg.TransactionListener)1 AsyncJobAddedNotification (org.activiti.engine.impl.jobexecutor.AsyncJobAddedNotification)1 JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)1 ActivityImpl (org.activiti.engine.impl.pvm.process.ActivityImpl)1