use of org.activiti.engine.impl.persistence.entity.TimerJobEntity in project Activiti by Activiti.
the class DefaultJobManager method moveTimerJobToExecutableJob.
@Override
public JobEntity moveTimerJobToExecutableJob(TimerJobEntity timerJob) {
if (timerJob == null) {
throw new ActivitiException("Empty timer job can not be scheduled");
}
JobEntity executableJob = createExecutableJobFromOtherJob(timerJob);
boolean insertSuccesful = processEngineConfiguration.getJobEntityManager().insertJobEntity(executableJob);
if (insertSuccesful) {
processEngineConfiguration.getTimerJobEntityManager().delete(timerJob);
triggerExecutorIfNeeded(executableJob);
return executableJob;
}
return null;
}
use of org.activiti.engine.impl.persistence.entity.TimerJobEntity in project Activiti by Activiti.
the class AcquireTimerJobsRunnable method run.
public synchronized void run() {
log.info("{} starting to acquire async jobs due");
Thread.currentThread().setName("activiti-acquire-timer-jobs");
final CommandExecutor commandExecutor = asyncExecutor.getProcessEngineConfiguration().getCommandExecutor();
while (!isInterrupted) {
try {
final AcquiredTimerJobEntities acquiredJobs = commandExecutor.execute(new AcquireTimerJobsCmd(asyncExecutor));
commandExecutor.execute(new Command<Void>() {
@Override
public Void execute(CommandContext commandContext) {
for (TimerJobEntity job : acquiredJobs.getJobs()) {
jobManager.moveTimerJobToExecutableJob(job);
}
return null;
}
});
// if all jobs were executed
millisToWait = asyncExecutor.getDefaultTimerJobAcquireWaitTimeInMillis();
int jobsAcquired = acquiredJobs.size();
if (jobsAcquired >= asyncExecutor.getMaxTimerJobsPerAcquisition()) {
millisToWait = 0;
}
} 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.persistence.entity.TimerJobEntity in project Activiti by Activiti.
the class ProcessInstanceSuspensionTest method testJobsVisibleToAcquisitionIfDefinitionSuspendedWithoutProcessInstances.
@Deployment(resources = { "org/activiti/engine/test/db/oneJobProcess.bpmn20.xml" })
public void testJobsVisibleToAcquisitionIfDefinitionSuspendedWithoutProcessInstances() {
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().singleResult();
runtimeService.startProcessInstanceByKey(pd.getKey());
// now there is one job:
Job job = managementService.createTimerJobQuery().singleResult();
assertThat(job).isNotNull();
makeSureJobDue(job);
// the acquire jobs command sees the job:
List<TimerJobEntity> acquiredJobs = executeAcquireJobsCommand();
assertThat(acquiredJobs).hasSize(1);
// suspend the process instance:
repositoryService.suspendProcessDefinitionById(pd.getId());
// the acquire jobs command still sees the job, because the process instances are not suspended:
acquiredJobs = executeAcquireJobsCommand();
assertThat(acquiredJobs).hasSize(1);
}
use of org.activiti.engine.impl.persistence.entity.TimerJobEntity in project Activiti by Activiti.
the class ProcessInstanceSuspensionTest method testSuspendedProcessTimerExecution.
@Deployment
public void testSuspendedProcessTimerExecution() throws Exception {
// Process with boundary timer-event that fires in 1 hour
ProcessInstance procInst = runtimeService.startProcessInstanceByKey("suspendProcess");
assertThat(procInst).isNotNull();
assertThat(managementService.createTimerJobQuery().processInstanceId(procInst.getId()).count()).isEqualTo(1);
// Roll time ahead to be sure timer is due to fire
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DAY_OF_YEAR, 1);
processEngineConfiguration.getClock().setCurrentTime(tomorrow.getTime());
// Check if timer is eligible to be executed, when process in not yet suspended
CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
List<TimerJobEntity> jobs = commandExecutor.execute(new Command<List<TimerJobEntity>>() {
@Override
public List<TimerJobEntity> execute(CommandContext commandContext) {
return processEngineConfiguration.getTimerJobEntityManager().findTimerJobsToExecute(new Page(0, 1));
}
});
assertThat(jobs).hasSize(1);
// Suspend process instance
runtimeService.suspendProcessInstanceById(procInst.getId());
// Check if the timer is NOT acquired, even though the duedate is reached
jobs = commandExecutor.execute(new Command<List<TimerJobEntity>>() {
@Override
public List<TimerJobEntity> execute(CommandContext commandContext) {
return processEngineConfiguration.getTimerJobEntityManager().findTimerJobsToExecute(new Page(0, 1));
}
});
assertThat(jobs).hasSize(0);
}
use of org.activiti.engine.impl.persistence.entity.TimerJobEntity 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));
AsyncExecutor asyncExecutor = processEngineConfiguration.getAsyncExecutor();
CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
String jobId = commandExecutor.execute(new Command<String>() {
public String execute(CommandContext commandContext) {
TimerJobEntity timer = createTweetTimer("i'm coding a test", new Date(SOME_TIME + (10 * SECOND)));
commandContext.getJobManager().scheduleTimerJob(timer);
return timer.getId();
}
});
AcquiredTimerJobEntities acquiredJobs = commandExecutor.execute(new AcquireTimerJobsCmd(asyncExecutor));
assertThat(acquiredJobs.size()).isEqualTo(0);
processEngineConfiguration.getClock().setCurrentTime(new Date(SOME_TIME + (20 * SECOND)));
acquiredJobs = commandExecutor.execute(new AcquireTimerJobsCmd(asyncExecutor));
assertThat(acquiredJobs.size()).isEqualTo(1);
TimerJobEntity job = acquiredJobs.getJobs().iterator().next();
assertThat(job.getId()).isEqualTo(jobId);
assertThat(tweetHandler.getMessages()).hasSize(0);
Job executableJob = managementService.moveTimerToExecutableJob(jobId);
commandExecutor.execute(new ExecuteAsyncJobCmd(executableJob.getId()));
assertThat(tweetHandler.getMessages().get(0)).isEqualTo("i'm coding a test");
assertThat(tweetHandler.getMessages()).hasSize(1);
}
Aggregations