use of org.activiti.engine.impl.Page in project Activiti by Activiti.
the class SimulationAcquireJobsRunnable method run.
public synchronized void run() {
// if (log.isLoggable(Level.INFO)) {
// log.info(jobExecutor.getName() + " starting to acquire jobs");
// }
final CommandExecutor commandExecutor = jobExecutor.getCommandExecutor();
// while is not needed - repetition is done by event scheduling
// while (!isInterrupted) {
isWaiting.set(false);
int maxJobsPerAcquisition = jobExecutor.getMaxJobsPerAcquisition();
try {
AcquiredJobs acquiredJobs = commandExecutor.execute(jobExecutor.getAcquireJobsCmd());
for (List<String> jobIds : acquiredJobs.getJobIdBatches()) {
jobExecutor.executeJobs(jobIds);
}
// if all jobs were executed
millisToWait = jobExecutor.getWaitTimeInMillis();
int jobsAcquired = acquiredJobs.getJobIdBatches().size();
if (jobsAcquired < maxJobsPerAcquisition) {
isJobAdded = false;
// check if the next timer should fire before the normal sleep time is over
Date duedate = new Date(SimulationRunContext.getClock().getCurrentTime().getTime() + millisToWait);
List<TimerEntity> nextTimers = commandExecutor.execute(new GetUnlockedTimersByDuedateCmd(duedate, new Page(0, 1)));
if (!nextTimers.isEmpty()) {
long millisTillNextTimer = nextTimers.get(0).getDuedate().getTime() - SimulationRunContext.getClock().getCurrentTime().getTime();
if (millisTillNextTimer < millisToWait && millisTillNextTimer != 0) {
millisToWait = millisTillNextTimer;
}
}
} else {
millisToWait = 0;
}
} catch (ActivitiOptimisticLockingException optimisticLockingException) {
// See https://activiti.atlassian.net/browse/ACT-1390
log.trace("Optimistic locking exception during job acquisition. If you have multiple job executors running against the same database, " + "this exception means that this thread tried to acquire a job, which already was acquired by another job executor acquisition 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 (Exception e) {
log.error("exception during job acquisition: " + e.getMessage(), e);
millisToWait = jobExecutor.getWaitTimeInMillis();
}
if ((millisToWait > 0) && (!isJobAdded)) {
try {
log.trace("job acquisition thread sleeping for " + millisToWait + " millis");
synchronized (MONITOR) {
if (!isInterrupted) {
isWaiting.set(true);
SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime() + millisToWait).property(this).build();
SimulationRunContext.getEventCalendar().addEvent(event);
// do not need to wait. - event scheduling is enough
//MONITOR.wait(millisToWait);
}
}
log.trace("job acquisition thread woke up");
} finally {
// isWaiting.set(false);
}
} else {
// schedule run now
SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime()).property(this).build();
SimulationRunContext.getEventCalendar().addEvent(event);
}
// }
// if (log.isLoggable(Level.INFO)) {
// log.info(jobExecutor.getName() + " stopped job acquisition");
// }
}
use of org.activiti.engine.impl.Page in project Activiti by Activiti.
the class AcquireAsyncJobsDueCmd method execute.
public AcquiredJobEntities execute(CommandContext commandContext) {
AcquiredJobEntities acquiredJobs = new AcquiredJobEntities();
List<JobEntity> jobs = commandContext.getJobEntityManager().findAsyncJobsDueToExecute(new Page(0, asyncExecutor.getMaxAsyncJobsDuePerAcquisition()));
for (JobEntity job : jobs) {
lockJob(commandContext, job, asyncExecutor.getAsyncJobLockTimeInMillis());
acquiredJobs.addJob(job);
}
return acquiredJobs;
}
use of org.activiti.engine.impl.Page 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;
}
use of org.activiti.engine.impl.Page in project Activiti by Activiti.
the class AcquireTimerJobsCmd method execute.
public AcquiredJobEntities execute(CommandContext commandContext) {
AcquiredJobEntities acquiredJobs = new AcquiredJobEntities();
List<JobEntity> jobs = commandContext.getJobEntityManager().findNextTimerJobsToExecute(new Page(0, maxJobsPerAcquisition));
for (JobEntity job : jobs) {
if (job != null && !acquiredJobs.contains(job.getId())) {
lockJob(commandContext, job, lockOwner, lockTimeInMillis);
acquiredJobs.addJob(job);
}
}
return acquiredJobs;
}
use of org.activiti.engine.impl.Page in project Activiti by Activiti.
the class DeploymentEntityManager method findNewLatestProcessDefinitionAfterRemovalOf.
protected ProcessDefinition findNewLatestProcessDefinitionAfterRemovalOf(ProcessDefinition processDefinitionToBeRemoved) {
// The latest process definition is not necessarily the one with 'version -1' (some versions could have been deleted)
// Hence, the following logic
ProcessDefinitionQueryImpl query = new ProcessDefinitionQueryImpl();
query.processDefinitionKey(processDefinitionToBeRemoved.getKey());
if (processDefinitionToBeRemoved.getTenantId() != null && !ProcessEngineConfiguration.NO_TENANT_ID.equals(processDefinitionToBeRemoved.getTenantId())) {
query.processDefinitionTenantId(processDefinitionToBeRemoved.getTenantId());
} else {
query.processDefinitionWithoutTenantId();
}
query.processDefinitionVersionLowerThan(processDefinitionToBeRemoved.getVersion());
query.orderByProcessDefinitionVersion().desc();
List<ProcessDefinition> processDefinitions = getProcessDefinitionManager().findProcessDefinitionsByQueryCriteria(query, new Page(0, 1));
if (processDefinitions != null && processDefinitions.size() > 0) {
return processDefinitions.get(0);
}
return null;
}
Aggregations