Search in sources :

Example 1 with AcquireTimerJobsCmd

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

Example 2 with AcquireTimerJobsCmd

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());
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Job(org.activiti.engine.runtime.Job) Date(java.util.Date) ProcessEngineImpl(org.activiti.engine.impl.ProcessEngineImpl) AcquireTimerJobsCmd(org.activiti.engine.impl.cmd.AcquireTimerJobsCmd) Deployment(org.activiti.engine.test.Deployment)

Example 3 with AcquireTimerJobsCmd

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

Aggregations

AcquireTimerJobsCmd (org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)3 CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)3 Date (java.util.Date)2 JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)2 ActivitiException (org.activiti.engine.ActivitiException)1 ActivitiOptimisticLockingException (org.activiti.engine.ActivitiOptimisticLockingException)1 ProcessEngineImpl (org.activiti.engine.impl.ProcessEngineImpl)1 AcquiredJobEntities (org.activiti.engine.impl.asyncexecutor.AcquiredJobEntities)1 ExecuteAsyncJobCmd (org.activiti.engine.impl.cmd.ExecuteAsyncJobCmd)1 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)1 TimerEntity (org.activiti.engine.impl.persistence.entity.TimerEntity)1 Job (org.activiti.engine.runtime.Job)1 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)1 Deployment (org.activiti.engine.test.Deployment)1