Search in sources :

Example 21 with TimerJobEntity

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;
}
Also used : DeadLetterJobEntity(org.activiti.engine.impl.persistence.entity.DeadLetterJobEntity) SuspendedJobEntity(org.activiti.engine.impl.persistence.entity.SuspendedJobEntity) AbstractJobEntity(org.activiti.engine.impl.persistence.entity.AbstractJobEntity) JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) TimerJobEntity(org.activiti.engine.impl.persistence.entity.TimerJobEntity) ActivitiException(org.activiti.engine.ActivitiException)

Example 22 with TimerJobEntity

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");
}
Also used : CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException) TimerJobEntity(org.activiti.engine.impl.persistence.entity.TimerJobEntity) AcquireTimerJobsCmd(org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)

Example 23 with TimerJobEntity

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);
}
Also used : ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) Job(org.activiti.engine.runtime.Job) TimerJobEntity(org.activiti.engine.impl.persistence.entity.TimerJobEntity) Deployment(org.activiti.engine.test.Deployment)

Example 24 with TimerJobEntity

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);
}
Also used : CommandContext(org.activiti.engine.impl.interceptor.CommandContext) Command(org.activiti.engine.impl.interceptor.Command) Calendar(java.util.Calendar) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) List(java.util.List) Page(org.activiti.engine.impl.Page) TimerJobEntity(org.activiti.engine.impl.persistence.entity.TimerJobEntity) Deployment(org.activiti.engine.test.Deployment)

Example 25 with TimerJobEntity

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);
}
Also used : CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ExecuteAsyncJobCmd(org.activiti.engine.impl.cmd.ExecuteAsyncJobCmd) Job(org.activiti.engine.runtime.Job) TimerJobEntity(org.activiti.engine.impl.persistence.entity.TimerJobEntity) Date(java.util.Date) AsyncExecutor(org.activiti.engine.impl.asyncexecutor.AsyncExecutor) AcquiredTimerJobEntities(org.activiti.engine.impl.asyncexecutor.AcquiredTimerJobEntities) AcquireTimerJobsCmd(org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)

Aggregations

TimerJobEntity (org.activiti.engine.impl.persistence.entity.TimerJobEntity)28 JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)8 SuspendedJobEntity (org.activiti.engine.impl.persistence.entity.SuspendedJobEntity)7 DeadLetterJobEntity (org.activiti.engine.impl.persistence.entity.DeadLetterJobEntity)6 Deployment (org.activiti.engine.test.Deployment)6 AbstractJobEntity (org.activiti.engine.impl.persistence.entity.AbstractJobEntity)5 Job (org.activiti.engine.runtime.Job)5 ActivitiException (org.activiti.engine.ActivitiException)4 JobManager (org.activiti.engine.impl.asyncexecutor.JobManager)4 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)4 CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)4 Date (java.util.Date)3 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)3 Calendar (java.util.Calendar)2 FlowElement (org.activiti.bpmn.model.FlowElement)2 VariableScope (org.activiti.engine.delegate.VariableScope)2 Page (org.activiti.engine.impl.Page)2 AcquiredTimerJobEntities (org.activiti.engine.impl.asyncexecutor.AcquiredTimerJobEntities)2 AcquireTimerJobsCmd (org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)2 NoExecutionVariableScope (org.activiti.engine.impl.el.NoExecutionVariableScope)2