use of org.activiti.engine.impl.cmd.AcquireTimerJobsCmd in project Activiti by Activiti.
the class AcquireTimerJobsRunnable 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 AcquireTimerJobsCmd(asyncExecutor.getLockOwner(), asyncExecutor.getTimerLockTimeInMillis(), asyncExecutor.getMaxTimerJobsPerAcquisition()));
boolean allJobsSuccessfullyOffered = true;
for (JobEntity job : acquiredJobs.getJobs()) {
boolean jobSuccessFullyOffered = asyncExecutor.executeAsyncJob(job);
if (!jobSuccessFullyOffered) {
allJobsSuccessfullyOffered = false;
}
}
// if all jobs were executed
millisToWait = asyncExecutor.getDefaultTimerJobAcquireWaitTimeInMillis();
int jobsAcquired = acquiredJobs.size();
if (jobsAcquired >= asyncExecutor.getMaxTimerJobsPerAcquisition()) {
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 timer job acquisition. If you have multiple timer executors running against the same database, " + "this exception means that this thread tried to acquire a timer job, which already was acquired by another timer executor acquisition thread." + "This is expected behavior in a clustered environment. " + "You can ignore this message if you indeed have multiple timer executor acquisition threads running against the same database. " + "Exception message: {}", optimisticLockingException.getMessage());
}
} catch (Throwable e) {
log.error("exception during timer job acquisition: {}", e.getMessage(), e);
millisToWait = asyncExecutor.getDefaultTimerJobAcquireWaitTimeInMillis();
}
if (millisToWait > 0) {
try {
if (log.isDebugEnabled()) {
log.debug("timer job acquisition thread sleeping for {} millis", millisToWait);
}
synchronized (MONITOR) {
if (!isInterrupted) {
isWaiting.set(true);
MONITOR.wait(millisToWait);
}
}
if (log.isDebugEnabled()) {
log.debug("timer job acquisition thread woke up");
}
} catch (InterruptedException e) {
if (log.isDebugEnabled()) {
log.debug("timer job acquisition wait interrupted");
}
} finally {
isWaiting.set(false);
}
}
}
log.info("stopped async job due acquisition");
}
use of org.activiti.engine.impl.cmd.AcquireTimerJobsCmd in project Activiti by Activiti.
the class ManagementServiceTest method testDeleteJobThatWasAlreadyAcquired.
@Deployment(resources = { "org/activiti/engine/test/api/mgmt/timerOnTask.bpmn20.xml" })
public void testDeleteJobThatWasAlreadyAcquired() {
processEngineConfiguration.getClock().setCurrentTime(new Date());
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("timerOnTask");
Job timerJob = managementService.createJobQuery().processInstanceId(processInstance.getId()).singleResult();
// We need to move time at least one hour to make the timer executable
processEngineConfiguration.getClock().setCurrentTime(new Date(processEngineConfiguration.getClock().getCurrentTime().getTime() + 7200000L));
// Acquire job by running the acquire command manually
ProcessEngineImpl processEngineImpl = (ProcessEngineImpl) processEngine;
AcquireTimerJobsCmd acquireJobsCmd = new AcquireTimerJobsCmd("testLockOwner", 60000, 5);
CommandExecutor commandExecutor = processEngineImpl.getProcessEngineConfiguration().getCommandExecutor();
commandExecutor.execute(acquireJobsCmd);
// Try to delete the job. This should fail.
try {
managementService.deleteJob(timerJob.getId());
fail();
} catch (ActivitiException e) {
// Exception is expected
}
// Clean up
managementService.executeJob(timerJob.getId());
}
use of org.activiti.engine.impl.cmd.AcquireTimerJobsCmd in project Activiti by Activiti.
the class JobExecutorCmdHappyTest method testJobCommandsWithTimer.
public void testJobCommandsWithTimer() {
// clock gets automatically reset in LogTestCase.runTest
processEngineConfiguration.getClock().setCurrentTime(new Date(SOME_TIME));
CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
String jobId = commandExecutor.execute(new Command<String>() {
public String execute(CommandContext commandContext) {
TimerEntity timer = createTweetTimer("i'm coding a test", new Date(SOME_TIME + (10 * SECOND)));
commandContext.getJobEntityManager().schedule(timer);
return timer.getId();
}
});
AcquiredJobEntities acquiredJobs = commandExecutor.execute(new AcquireTimerJobsCmd("testLockOwner", 10000, 5));
assertEquals(0, acquiredJobs.size());
processEngineConfiguration.getClock().setCurrentTime(new Date(SOME_TIME + (20 * SECOND)));
acquiredJobs = commandExecutor.execute(new AcquireTimerJobsCmd("testLockOwner", 10000, 5));
assertEquals(1, acquiredJobs.size());
JobEntity job = acquiredJobs.getJobs().iterator().next();
assertEquals(jobId, job.getId());
assertEquals(0, tweetHandler.getMessages().size());
commandExecutor.execute(new ExecuteAsyncJobCmd(job));
assertEquals("i'm coding a test", tweetHandler.getMessages().get(0));
assertEquals(1, tweetHandler.getMessages().size());
}
Aggregations