Search in sources :

Example 6 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity in project Activiti by Activiti.

the class AbstractAsyncJobExecutor method start.

/** Starts the async executor */
public void start() {
    if (isActive) {
        return;
    }
    log.info("Starting up the default async job executor [{}].", getClass().getName());
    initialize();
    startExecutingAsyncJobs();
    isActive = true;
    while (temporaryJobQueue.isEmpty() == false) {
        JobEntity job = temporaryJobQueue.pop();
        executeAsyncJob(job);
    }
    isActive = true;
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity)

Example 7 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity in project Activiti by Activiti.

the class AcquireAsyncJobsDueRunnable method run.

public synchronized void run() {
    log.info("starting to acquire async jobs due");
    final CommandExecutor commandExecutor = asyncExecutor.getCommandExecutor();
    while (!isInterrupted) {
        try {
            AcquiredJobEntities acquiredJobs = commandExecutor.execute(new AcquireAsyncJobsDueCmd(asyncExecutor));
            boolean allJobsSuccessfullyOffered = true;
            for (JobEntity job : acquiredJobs.getJobs()) {
                boolean jobSuccessFullyOffered = asyncExecutor.executeAsyncJob(job);
                if (!jobSuccessFullyOffered) {
                    allJobsSuccessfullyOffered = false;
                }
            }
            // If all jobs are executed, we check if we got back the amount we expected
            // If not, we will wait, as to not query the database needlessly. 
            // Otherwise, we set the wait time to 0, as to query again immediately.
            millisToWait = asyncExecutor.getDefaultAsyncJobAcquireWaitTimeInMillis();
            int jobsAcquired = acquiredJobs.size();
            if (jobsAcquired >= asyncExecutor.getMaxAsyncJobsDuePerAcquisition()) {
                millisToWait = 0;
            }
            // If the queue was full, we wait too (even if we got enough jobs back), as not overload the queue
            if (millisToWait == 0 && !allJobsSuccessfullyOffered) {
                millisToWait = asyncExecutor.getDefaultQueueSizeFullWaitTimeInMillis();
            }
        } catch (ActivitiOptimisticLockingException optimisticLockingException) {
            if (log.isDebugEnabled()) {
                log.debug("Optimistic locking exception during async job acquisition. If you have multiple async executors running against the same database, " + "this exception means that this thread tried to acquire a due async job, which already was acquired by another async executor acquisition thread." + "This is expected behavior in a clustered environment. " + "You can ignore this message if you indeed have multiple async executor acquisition threads running against the same database. " + "Exception message: {}", optimisticLockingException.getMessage());
            }
        } catch (Throwable e) {
            log.error("exception during async job acquisition: {}", e.getMessage(), e);
            millisToWait = asyncExecutor.getDefaultAsyncJobAcquireWaitTimeInMillis();
        }
        if (millisToWait > 0) {
            try {
                if (log.isDebugEnabled()) {
                    log.debug("async job acquisition thread sleeping for {} millis", millisToWait);
                }
                synchronized (MONITOR) {
                    if (!isInterrupted) {
                        isWaiting.set(true);
                        MONITOR.wait(millisToWait);
                    }
                }
                if (log.isDebugEnabled()) {
                    log.debug("async job acquisition thread woke up");
                }
            } catch (InterruptedException e) {
                if (log.isDebugEnabled()) {
                    log.debug("async job acquisition wait interrupted");
                }
            } finally {
                isWaiting.set(false);
            }
        }
    }
    log.info("stopped async job due acquisition");
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException) AcquireAsyncJobsDueCmd(org.activiti.engine.impl.cmd.AcquireAsyncJobsDueCmd)

Example 8 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity in project Activiti by Activiti.

the class CancelJobCmd method execute.

@Override
public Object execute(CommandContext commandContext) {
    JobEntity jobToDelete = getJobToDelete(commandContext);
    sendCancelEvent(jobToDelete);
    jobToDelete.delete();
    return null;
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity)

Example 9 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity in project Activiti by Activiti.

the class DeleteJobCmd method execute.

public Object execute(CommandContext commandContext) {
    JobEntity jobToDelete = getJobToDelete(commandContext);
    jobToDelete.delete();
    return null;
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity)

Example 10 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity 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)

Aggregations

JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)17 CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)5 Page (org.activiti.engine.impl.Page)3 AcquiredJobEntities (org.activiti.engine.impl.asyncexecutor.AcquiredJobEntities)3 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)3 Job (org.activiti.engine.runtime.Job)3 Label (com.vaadin.ui.Label)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 ActivitiOptimisticLockingException (org.activiti.engine.ActivitiOptimisticLockingException)2 AcquireTimerJobsCmd (org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)2 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)1 Panel (com.vaadin.ui.Panel)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 Calendar (java.util.Calendar)1 ActivitiException (org.activiti.engine.ActivitiException)1 ProcessEngineConfiguration (org.activiti.engine.ProcessEngineConfiguration)1 ActivitiEventDispatcher (org.activiti.engine.delegate.event.ActivitiEventDispatcher)1 ExecuteAsyncRunnable (org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable)1 DurationHelper (org.activiti.engine.impl.calendar.DurationHelper)1