Search in sources :

Example 1 with AcquiredJobs

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

the class AcquireJobsCmd method execute.

public AcquiredJobs execute(CommandContext commandContext) {
    String lockOwner = jobExecutor.getLockOwner();
    int lockTimeInMillis = jobExecutor.getLockTimeInMillis();
    int maxNonExclusiveJobsPerAcquisition = jobExecutor.getMaxJobsPerAcquisition();
    AcquiredJobs acquiredJobs = new AcquiredJobs();
    List<JobEntity> jobs = commandContext.getJobEntityManager().findNextJobsToExecute(new Page(0, maxNonExclusiveJobsPerAcquisition));
    for (JobEntity job : jobs) {
        List<String> jobIds = new ArrayList<String>();
        if (job != null && !acquiredJobs.contains(job.getId())) {
            if (job instanceof MessageEntity && job.isExclusive() && job.getProcessInstanceId() != null) {
                // wait to get exclusive jobs within 100ms
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                // acquire all exclusive jobs in the same process instance
                // (includes the current job)
                List<JobEntity> exclusiveJobs = commandContext.getJobEntityManager().findExclusiveJobsToExecute(job.getProcessInstanceId());
                for (JobEntity exclusiveJob : exclusiveJobs) {
                    if (exclusiveJob != null) {
                        lockJob(commandContext, exclusiveJob, lockOwner, lockTimeInMillis);
                        jobIds.add(exclusiveJob.getId());
                    }
                }
            } else {
                lockJob(commandContext, job, lockOwner, lockTimeInMillis);
                jobIds.add(job.getId());
            }
        }
        acquiredJobs.addJobIdBatch(jobIds);
    }
    return acquiredJobs;
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) MessageEntity(org.activiti.engine.impl.persistence.entity.MessageEntity) ArrayList(java.util.ArrayList) AcquiredJobs(org.activiti.engine.impl.jobexecutor.AcquiredJobs) Page(org.activiti.engine.impl.Page)

Aggregations

ArrayList (java.util.ArrayList)1 Page (org.activiti.engine.impl.Page)1 AcquiredJobs (org.activiti.engine.impl.jobexecutor.AcquiredJobs)1 JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)1 MessageEntity (org.activiti.engine.impl.persistence.entity.MessageEntity)1