Search in sources :

Example 6 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class ExecuteJobsRunnable method handleSingleJob.

protected void handleSingleJob() {
    final SingleJobExecutorContext jobExecutorContext = new SingleJobExecutorContext();
    final List<JobEntity> currentProcessorJobQueue = jobExecutorContext.getCurrentProcessorJobQueue();
    final CommandExecutor commandExecutor = jobExecutor.getCommandExecutor();
    currentProcessorJobQueue.add(job);
    Context.setJobExecutorContext(jobExecutorContext);
    try {
        while (!currentProcessorJobQueue.isEmpty()) {
            JobEntity currentJob = currentProcessorJobQueue.remove(0);
            try {
                commandExecutor.execute(new ExecuteJobsCmd(currentJob));
            } catch (Throwable e) {
                log.error("exception during job execution: {}", e.getMessage(), e);
            } finally {
                jobExecutor.jobDone(currentJob);
            }
        }
    } finally {
        Context.removeJobExecutorContext();
    }
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ExecuteJobsCmd(org.activiti.engine.impl.cmd.ExecuteJobsCmd)

Example 7 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class BaseJPARestTestCase method assertAndEnsureCleanDb.

/** Each test is assumed to clean up all DB content it entered.
   * After a test method executed, this method scans all tables to see if the DB is completely clean. 
   * It throws AssertionFailed in case the DB is not clean.
   * If the DB is not clean, it is cleaned by performing a create a drop. */
protected void assertAndEnsureCleanDb() throws Throwable {
    log.debug("verifying that db is clean after test");
    Map<String, Long> tableCounts = managementService.getTableCount();
    StringBuilder outputMessage = new StringBuilder();
    for (String tableName : tableCounts.keySet()) {
        String tableNameWithoutPrefix = tableName.replace(processEngineConfiguration.getDatabaseTablePrefix(), "");
        if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableNameWithoutPrefix)) {
            Long count = tableCounts.get(tableName);
            if (count != 0L) {
                outputMessage.append("  " + tableName + ": " + count + " record(s) ");
            }
        }
    }
    if (outputMessage.length() > 0) {
        outputMessage.insert(0, "DB NOT CLEAN: \n");
        log.error(EMPTY_LINE);
        log.error(outputMessage.toString());
        log.info("dropping and recreating db");
        CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor();
        commandExecutor.execute(new Command<Object>() {

            public Object execute(CommandContext commandContext) {
                DbSqlSession session = commandContext.getSession(DbSqlSession.class);
                session.dbSchemaDrop();
                session.dbSchemaCreate();
                return null;
            }
        });
        if (exception != null) {
            throw exception;
        } else {
            Assert.fail(outputMessage.toString());
        }
    } else {
        log.info("database was clean");
    }
}
Also used : CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) DbSqlSession(org.activiti.engine.impl.db.DbSqlSession)

Example 8 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class AcquireAsyncJobsDueRunnable 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 AcquireAsyncJobsDueCmd(asyncExecutor));
            boolean allJobsSuccessfullyOffered = true;
            for (JobEntity job : acquiredJobs.getJobs()) {
                boolean jobSuccessFullyOffered = asyncExecutor.executeAsyncJob(job);
                if (!jobSuccessFullyOffered) {
                    allJobsSuccessfullyOffered = false;
                }
            }
            // If all jobs are executed, we check if we got back the amount we expected
            // If not, we will wait, as to not query the database needlessly. 
            // Otherwise, we set the wait time to 0, as to query again immediately.
            millisToWait = asyncExecutor.getDefaultAsyncJobAcquireWaitTimeInMillis();
            int jobsAcquired = acquiredJobs.size();
            if (jobsAcquired >= asyncExecutor.getMaxAsyncJobsDuePerAcquisition()) {
                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 async job acquisition. If you have multiple async executors running against the same database, " + "this exception means that this thread tried to acquire a due async job, which already was acquired by another async executor acquisition thread." + "This is expected behavior in a clustered environment. " + "You can ignore this message if you indeed have multiple async executor acquisition threads running against the same database. " + "Exception message: {}", optimisticLockingException.getMessage());
            }
        } catch (Throwable e) {
            log.error("exception during async job acquisition: {}", e.getMessage(), e);
            millisToWait = asyncExecutor.getDefaultAsyncJobAcquireWaitTimeInMillis();
        }
        if (millisToWait > 0) {
            try {
                if (log.isDebugEnabled()) {
                    log.debug("async job acquisition thread sleeping for {} millis", millisToWait);
                }
                synchronized (MONITOR) {
                    if (!isInterrupted) {
                        isWaiting.set(true);
                        MONITOR.wait(millisToWait);
                    }
                }
                if (log.isDebugEnabled()) {
                    log.debug("async job acquisition thread woke up");
                }
            } catch (InterruptedException e) {
                if (log.isDebugEnabled()) {
                    log.debug("async 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) AcquireAsyncJobsDueCmd(org.activiti.engine.impl.cmd.AcquireAsyncJobsDueCmd)

Example 9 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class BpmnDeploymentTest method testDiagramCreationDisabled.

public void testDiagramCreationDisabled() {
    // disable diagram generation
    processEngineConfiguration.setCreateDiagramOnDeploy(false);
    try {
        repositoryService.createDeployment().addClasspathResource("org/activiti/engine/test/bpmn/parse/BpmnParseTest.testParseDiagramInterchangeElements.bpmn20.xml").deploy();
        // Graphical information is not yet exposed publicly, so we need to do some plumbing
        CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
        ProcessDefinitionEntity processDefinitionEntity = commandExecutor.execute(new Command<ProcessDefinitionEntity>() {

            public ProcessDefinitionEntity execute(CommandContext commandContext) {
                return Context.getProcessEngineConfiguration().getDeploymentManager().findDeployedLatestProcessDefinitionByKey("myProcess");
            }
        });
        assertNotNull(processDefinitionEntity);
        assertEquals(7, processDefinitionEntity.getActivities().size());
        // Check that no diagram has been created
        List<String> resourceNames = repositoryService.getDeploymentResourceNames(processDefinitionEntity.getDeploymentId());
        assertEquals(1, resourceNames.size());
        repositoryService.deleteDeployment(repositoryService.createDeploymentQuery().singleResult().getId(), true);
    } finally {
        processEngineConfiguration.setCreateDiagramOnDeploy(true);
    }
}
Also used : CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ProcessDefinitionEntity(org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)

Example 10 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class ProcessInstanceMigrationTest method testSetProcessDefinitionVersionPIIsSubExecution.

@Deployment(resources = { TEST_PROCESS_WITH_PARALLEL_GATEWAY })
public void testSetProcessDefinitionVersionPIIsSubExecution() {
    // start process instance
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("forkJoin");
    Execution execution = runtimeService.createExecutionQuery().activityId("receivePayment").singleResult();
    CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
    SetProcessDefinitionVersionCmd command = new SetProcessDefinitionVersionCmd(execution.getId(), 1);
    try {
        commandExecutor.execute(command);
        fail("ActivitiException expected");
    } catch (ActivitiException ae) {
        assertTextPresent("A process instance id is required, but the provided id '" + execution.getId() + "' points to a child execution of process instance '" + pi.getId() + "'. Please invoke the " + command.getClass().getSimpleName() + " with a root execution id.", ae.getMessage());
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Execution(org.activiti.engine.runtime.Execution) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) SetProcessDefinitionVersionCmd(org.activiti.engine.impl.cmd.SetProcessDefinitionVersionCmd) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Deployment(org.activiti.engine.test.Deployment)

Aggregations

CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)36 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)18 Deployment (org.activiti.engine.test.Deployment)11 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)10 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)8 SetProcessDefinitionVersionCmd (org.activiti.engine.impl.cmd.SetProcessDefinitionVersionCmd)8 CommandConfig (org.activiti.engine.impl.interceptor.CommandConfig)7 Date (java.util.Date)6 Execution (org.activiti.engine.runtime.Execution)6 DbSqlSession (org.activiti.engine.impl.db.DbSqlSession)5 JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)5 TimerEntity (org.activiti.engine.impl.persistence.entity.TimerEntity)5 ActivitiOptimisticLockingException (org.activiti.engine.ActivitiOptimisticLockingException)4 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)4 ActivitiException (org.activiti.engine.ActivitiException)3 ProcessEngineImpl (org.activiti.engine.impl.ProcessEngineImpl)3 AcquireTimerJobsCmd (org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)3 JobEntityManager (org.activiti.engine.impl.persistence.entity.JobEntityManager)3 ProcessDefinitionEntity (org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)3 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)2